camera.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "camera.hpp"
  2. #include <Eigen/Dense>
  3. #include "warping.hpp"
  4. using namespace Eigen;
  5. struct hit{
  6. Vector3f pos;
  7. Vector3f normal;
  8. Vector3f indir;
  9. Vector3f outdir;
  10. };
  11. std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sampler _rng)const{
  12. using namespace Eigen;
  13. std::vector<Eigen::Vector3f> img(n * n, Eigen::Vector3f(0,0,0));
  14. const Eigen::Vector3f lookray = (look_at - pos).normalized();
  15. const Eigen::Vector3f left = lookray.cross(up).normalized();
  16. const Eigen::Vector3f realup = left.cross(lookray);
  17. const int n2 = n / 2;
  18. size_t raycasts = 0;
  19. #pragma omp parallel
  20. {
  21. sampler rng;
  22. std::uniform_real_distribution<float> pert_dis(-0.5,0.5);
  23. #pragma omp for collapse(2) schedule(guided) reduction(+:raycasts)
  24. for(int j = 0;j < n;j++){
  25. for(int i = 0;i < n;i++){
  26. Eigen::Vector3d accum(0,0,0);
  27. for(size_t smp = 0;smp < 128;smp++){
  28. Eigen::Vector3f di = lookray + (left * ((pert_dis(rng.m_rng) + float(i - n2)) / n2 / 1.5)) + realup * ((pert_dis(rng.m_rng) + float(j - n2)) / n2 / 1.5);
  29. di.normalize();
  30. struct RTCRayHit rayhit;
  31. rayhit.ray.org_x = pos.x();
  32. rayhit.ray.org_y = pos.y();
  33. rayhit.ray.org_z = pos.z();
  34. rayhit.ray.dir_x = di.x();
  35. rayhit.ray.dir_y = di.y();
  36. rayhit.ray.dir_z = di.z();
  37. Vector3f hitpos = pos;
  38. Vector3f transport(1,1,1);
  39. std::vector<hit> hits;
  40. hits.reserve(6);
  41. for(size_t bounces = 0;bounces < 6;bounces++){
  42. struct RTCIntersectContext context;
  43. rtcInitIntersectContext(&context);
  44. rayhit.ray.tnear = 0.001f;
  45. rayhit.ray.tfar = std::numeric_limits<float>::infinity();
  46. rayhit.ray.mask = -1;
  47. rayhit.ray.flags = 0;
  48. rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
  49. rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
  50. rtcIntersect1(sc.m_scene, &context, &rayhit);
  51. raycasts++;
  52. if(rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID){
  53. if(sc.emitters.contains(rayhit.hit.geomID)){
  54. Eigen::Vector3d transport(0,1,1);
  55. for(auto it = hits.rbegin();it != hits.rend();it++){
  56. transport.array() *= it->outdir.dot(it->normal);
  57. for(const auto& pl : sc.pointlight){
  58. struct RTCIntersectContext sh_context;
  59. rtcInitIntersectContext(&sh_context);
  60. Vector3f sh_ray(pl.pos - it->pos);
  61. if(sh_ray.dot(it->normal) < 0)continue;
  62. float sh_ray_length = sh_ray.norm();
  63. sh_ray.normalize();
  64. RTCRay rsh_ray;
  65. rsh_ray.org_x = it->pos.x();
  66. rsh_ray.org_y = it->pos.y();
  67. rsh_ray.org_z = it->pos.z();
  68. rsh_ray.dir_x = sh_ray.x();
  69. rsh_ray.dir_y = sh_ray.y();
  70. rsh_ray.dir_z = sh_ray.z();
  71. rsh_ray.tnear = 0.001f;
  72. rsh_ray.tfar = sh_ray_length;
  73. rsh_ray.mask = -1;
  74. rsh_ray.flags = 0;
  75. raycasts++;
  76. rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
  77. if(rsh_ray.tfar > 0){
  78. transport += pl.color.cast<double>() * std::abs(sh_ray.dot(it->normal)) * (1.0 / double(sh_ray_length * sh_ray_length));
  79. }
  80. }
  81. }
  82. accum += transport;
  83. goto inner;
  84. }
  85. rayhit.ray.org_x += rayhit.ray.dir_x * rayhit.ray.tfar;
  86. rayhit.ray.org_y += rayhit.ray.dir_y * rayhit.ray.tfar;
  87. rayhit.ray.org_z += rayhit.ray.dir_z * rayhit.ray.tfar;
  88. Vector3f hitnormal = Vector3f(rayhit.hit.Ng_x, rayhit.hit.Ng_y, rayhit.hit.Ng_z).normalized();
  89. Vector3f newdir = uniform_sphere(rng.next2D());
  90. if(newdir.dot(hitnormal) <= 0){
  91. newdir *= -1.0f;
  92. }
  93. hits.push_back(hit{
  94. Vector3f(rayhit.ray.org_x, rayhit.ray.org_y, rayhit.ray.org_z),
  95. hitnormal,
  96. Vector3f(rayhit.ray.dir_x, rayhit.ray.dir_y, rayhit.ray.dir_z),
  97. newdir
  98. });
  99. transport *= newdir.dot(hitnormal);
  100. rayhit.ray.dir_x = newdir.x();
  101. rayhit.ray.dir_y = newdir.y();
  102. rayhit.ray.dir_z = newdir.z();
  103. }
  104. else{
  105. Eigen::Vector3d transport(0,0,0);
  106. for(auto it = hits.rbegin();it != hits.rend();it++){
  107. transport.array() *= it->outdir.dot(it->normal);
  108. for(const auto& pl : sc.pointlight){
  109. struct RTCIntersectContext sh_context;
  110. rtcInitIntersectContext(&sh_context);
  111. Vector3f sh_ray(pl.pos - it->pos);
  112. if(sh_ray.dot(it->normal) < 0)continue;
  113. float sh_ray_length = sh_ray.norm();
  114. sh_ray.normalize();
  115. RTCRay rsh_ray;
  116. rsh_ray.org_x = it->pos.x();
  117. rsh_ray.org_y = it->pos.y();
  118. rsh_ray.org_z = it->pos.z();
  119. rsh_ray.dir_x = sh_ray.x();
  120. rsh_ray.dir_y = sh_ray.y();
  121. rsh_ray.dir_z = sh_ray.z();
  122. rsh_ray.tnear = 0.001f;
  123. rsh_ray.tfar = sh_ray_length;
  124. rsh_ray.mask = -1;
  125. rsh_ray.flags = 0;
  126. raycasts++;
  127. rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
  128. if(rsh_ray.tfar > 0){
  129. transport += pl.color.cast<double>() * std::abs(sh_ray.dot(it->normal)) * 1 / (sh_ray_length * sh_ray_length);
  130. }
  131. }
  132. }
  133. accum += transport;
  134. goto inner;
  135. }
  136. }
  137. inner:
  138. (void)0;
  139. }
  140. img[j * n + i] = (accum.cast<float>() / 32.0f).array().pow(0.5f).matrix();
  141. }
  142. }
  143. }
  144. std::cout << std::to_string(raycasts) + " Raycasts\n";
  145. return img;
  146. }
  147. camera::camera(const Eigen::Vector3f& loc, const Eigen::Vector3f& look, const Eigen::Vector3f& u) : pos(loc), look_at(look), up(u){
  148. }