main.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <embree3/rtcore.h>
  2. #include <cstdio>
  3. #include <limits>
  4. #include <iostream>
  5. #include <OBJ_Loader.h>
  6. #include <bmp.hpp>
  7. #include "scene.hpp"
  8. #include "camera.hpp"
  9. #include <time.hpp>
  10. #include <lodepng.h>
  11. #include <algorithm>
  12. #include <Eigen/Geometry>
  13. using std::printf;
  14. void errorFunction(void* userPtr, enum RTCError error, const char* str){
  15. printf("error %d: %s\n", error, str);
  16. }
  17. RTCDevice initializeDevice(){
  18. RTCDevice device = rtcNewDevice("threads=24,user_threads=24");
  19. if (!device)
  20. printf("error %d: cannot create device\n", rtcGetDeviceError(NULL));
  21. rtcSetDeviceErrorFunction(device, errorFunction, NULL);
  22. return device;
  23. }
  24. int main(){
  25. using std::cos;
  26. using std::sin;
  27. using namespace Eigen;
  28. RTCDevice dev = initializeDevice();
  29. scene scene(dev);
  30. objl::Loader ajax;
  31. objl::Loader quad;
  32. ajax.LoadFile("ajax.obj");
  33. quad.LoadFile("quad.obj");
  34. objl::Mesh ajax_mesh = ajax.LoadedMeshes[0];
  35. objl::Mesh quad_mesh = quad.LoadedMeshes[0];
  36. Matrix4f trf;
  37. std::uniform_real_distribution<float> angles(0, 2 * M_PI);
  38. std::uniform_real_distribution<float> o1(0, 1);
  39. xoshiro_256 gangles(42);
  40. for(float ax = -10;ax < 10;ax += 5){
  41. for(float ay = -10;ay < 10;ay += 5){
  42. float angle = angles(gangles);
  43. trf << cos(angle),0,-sin(angle),ax,
  44. 0,1.6,0,0,
  45. sin(angle),0,cos(angle),ay,
  46. 0,0,0,1;
  47. scene.add_object(ajax_mesh, trf, false);
  48. scene.added_objects.back()->mat.tex = std::make_unique<uni_texture>(Color(o1(gangles), o1(gangles), o1(gangles)));
  49. //scene.added_objects.back()->mat.m_bsdf = std::make_unique<microfacet_bsdf>(0.8);
  50. }
  51. }
  52. trf << 40,0,0,0,
  53. 0,1,0,-1.6,
  54. 0,0,40,0,
  55. 0,0,0,1;
  56. scene.add_object(quad_mesh, trf, false);
  57. scene.added_objects.back()->mat.tex = std::make_unique<quad_checkerboard_texture>(Color(1,0,0), Color(0,1,0), Color(0,0,1), Color(0,0,0));
  58. //scene.add_sphere(Vector3f(-1.5,0,0),0.5, true);
  59. scene.pointlight.push_back(pointlight{Eigen::Vector3f(2,0,0), Eigen::Vector3f(1,0,0)});
  60. scene.pointlight.push_back(pointlight{Eigen::Vector3f(2,0,1), Eigen::Vector3f(0,1,0)});
  61. scene.pointlight.push_back(pointlight{Eigen::Vector3f(2,0,2), Eigen::Vector3f(0,0,1)});
  62. scene.build();
  63. camera cam(Vector3f(0, 0.6 * 5, 2.4 * 5),Vector3f(0,0,0), Vector3f(0,1,0));
  64. sampler simp;
  65. size_t n = 1000;
  66. auto t1 = nanoTime();
  67. auto x = cam.get_image(scene, n, simp);
  68. auto t2 = nanoTime();
  69. std::cout << (t2 - t1) / 1000 / 1000.0 << " miliseconds\n";
  70. std::vector<unsigned char> lodepng_image(x.size() * 4);
  71. for(size_t i = 0;i < x.size();i++){
  72. lodepng_image[i * 4] = (unsigned char)(std::min(x[x.size() - i - 1].x() * 256,255.f));
  73. lodepng_image[i * 4 + 1] = (unsigned char)(std::min(x[x.size() - i - 1].y() * 256,255.f));
  74. lodepng_image[i * 4 + 2] = (unsigned char)(std::min(x[x.size() - i - 1].z() * 256,255.f));
  75. lodepng_image[i * 4 + 3] = 255;
  76. }
  77. lodepng::encode("bild.png", lodepng_image, n, n);
  78. //drawbmp("bild.bmp", x, n, n);
  79. //std::cout << (rayhit.ray.tfar) << "\n";
  80. //std::cout << (rayhit.hit.Ng_x) << "\n";
  81. return 0;
  82. }