camera.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. const material& mat;
  11. };
  12. std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sampler _rng)const{
  13. using namespace Eigen;
  14. std::vector<Eigen::Vector3f> img(n * n, Eigen::Vector3f(0,0,0));
  15. const Eigen::Vector3f lookray = (look_at - pos).normalized();
  16. const Eigen::Vector3f left = lookray.cross(up).normalized();
  17. const Eigen::Vector3f realup = left.cross(lookray);
  18. const int n2 = n / 2;
  19. size_t raycasts = 0;
  20. #pragma omp parallel
  21. {
  22. sampler rng;
  23. std::uniform_real_distribution<float> pert_dis(-0.5,0.5);
  24. #pragma omp for collapse(2) schedule(guided) reduction(+:raycasts)
  25. for(int j = 0;j < n;j++){
  26. for(int i = 0;i < n;i++){
  27. Eigen::Vector3d accum(0,0,0);
  28. std::vector<hit> hits;
  29. hits.reserve(6);
  30. for(size_t smp = 0;smp < 512;smp++){
  31. 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);
  32. di.normalize();
  33. struct RTCRayHit rayhit;
  34. rayhit.ray.org_x = pos.x();
  35. rayhit.ray.org_y = pos.y();
  36. rayhit.ray.org_z = pos.z();
  37. rayhit.ray.dir_x = di.x();
  38. rayhit.ray.dir_y = di.y();
  39. rayhit.ray.dir_z = di.z();
  40. Vector3f hitpos = pos;
  41. Vector3f transport(1,1,1);
  42. hits.clear();
  43. for(size_t bounces = 0;bounces < 6;bounces++){
  44. struct RTCIntersectContext context;
  45. rtcInitIntersectContext(&context);
  46. rayhit.ray.tnear = 0.001f;
  47. rayhit.ray.tfar = std::numeric_limits<float>::infinity();
  48. rayhit.ray.mask = -1;
  49. rayhit.ray.flags = 0;
  50. rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
  51. rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
  52. rtcIntersect1(sc.m_scene, &context, &rayhit);
  53. raycasts++;
  54. if(rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID){
  55. if(sc.emitters.contains(rayhit.hit.geomID)){
  56. Eigen::Vector3d transport(sc.added_objects[sc.geom_id_to_object_index_map.find(rayhit.hit.geomID)->second]->mat.tex->eval(0,0).cast<double>());
  57. for(auto it = hits.rbegin();it != hits.rend();it++){
  58. transport.array() *= (it->mat.m_bsdf->eval(
  59. it->outdir,
  60. it->indir,
  61. it->normal
  62. ).array().cast<double>());
  63. for(const auto& pl : sc.pointlight){
  64. struct RTCIntersectContext sh_context;
  65. rtcInitIntersectContext(&sh_context);
  66. Vector3f sh_ray(pl.pos - it->pos);
  67. if(sh_ray.dot(it->normal) < 0)continue;
  68. float sh_ray_length = sh_ray.norm();
  69. sh_ray /= sh_ray_length;
  70. RTCRay rsh_ray;
  71. rsh_ray.org_x = it->pos.x();
  72. rsh_ray.org_y = it->pos.y();
  73. rsh_ray.org_z = it->pos.z();
  74. rsh_ray.dir_x = sh_ray.x();
  75. rsh_ray.dir_y = sh_ray.y();
  76. rsh_ray.dir_z = sh_ray.z();
  77. rsh_ray.tnear = 0.001f;
  78. rsh_ray.tfar = sh_ray_length;
  79. rsh_ray.mask = -1;
  80. rsh_ray.flags = 0;
  81. raycasts++;
  82. rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
  83. if(rsh_ray.tfar > 0){
  84. transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * (1.0 / double(sh_ray_length * sh_ray_length));
  85. }
  86. }
  87. }
  88. accum += transport;
  89. goto inner;
  90. }
  91. rayhit.ray.org_x += rayhit.ray.dir_x * rayhit.ray.tfar;
  92. rayhit.ray.org_y += rayhit.ray.dir_y * rayhit.ray.tfar;
  93. rayhit.ray.org_z += rayhit.ray.dir_z * rayhit.ray.tfar;
  94. Vector3f hitnormal = Vector3f(rayhit.hit.Ng_x, rayhit.hit.Ng_y, rayhit.hit.Ng_z).normalized();
  95. Vector3f newdir = uniform_sphere(rng.next2D());
  96. if(newdir.dot(hitnormal) <= 0){
  97. newdir *= -1.0f;
  98. }
  99. hits.push_back(hit{
  100. Vector3f(rayhit.ray.org_x, rayhit.ray.org_y, rayhit.ray.org_z),
  101. hitnormal,
  102. Vector3f(rayhit.ray.dir_x, rayhit.ray.dir_y, rayhit.ray.dir_z),
  103. newdir,
  104. (sc.added_objects[sc.geom_id_to_object_index_map.find(rayhit.hit.geomID)->second]->mat)
  105. });
  106. transport *= newdir.dot(hitnormal);
  107. rayhit.ray.dir_x = newdir.x();
  108. rayhit.ray.dir_y = newdir.y();
  109. rayhit.ray.dir_z = newdir.z();
  110. }
  111. else{
  112. Eigen::Vector3d transport(0,0,0);
  113. for(auto it = hits.rbegin();it != hits.rend();it++){
  114. transport.array() *= it->mat.m_bsdf->eval(
  115. it->outdir,
  116. it->indir,
  117. it->normal
  118. ).array().cast<double>();
  119. for(const auto& pl : sc.pointlight){
  120. struct RTCIntersectContext sh_context;
  121. rtcInitIntersectContext(&sh_context);
  122. Vector3f sh_ray(pl.pos - it->pos);
  123. if(sh_ray.dot(it->normal) < 0)continue;
  124. float sh_ray_length = sh_ray.norm();
  125. sh_ray.normalize();
  126. RTCRay rsh_ray;
  127. rsh_ray.org_x = it->pos.x();
  128. rsh_ray.org_y = it->pos.y();
  129. rsh_ray.org_z = it->pos.z();
  130. rsh_ray.dir_x = sh_ray.x();
  131. rsh_ray.dir_y = sh_ray.y();
  132. rsh_ray.dir_z = sh_ray.z();
  133. rsh_ray.tnear = 0.001f;
  134. rsh_ray.tfar = sh_ray_length;
  135. rsh_ray.mask = -1;
  136. rsh_ray.flags = 0;
  137. raycasts++;
  138. rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
  139. if(rsh_ray.tfar > 0){
  140. transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * (1.0 / double(sh_ray_length * sh_ray_length));
  141. }
  142. }
  143. }
  144. accum += transport;
  145. goto inner;
  146. }
  147. }
  148. {
  149. Eigen::Vector3d transport(0,0,0);
  150. for(auto it = hits.rbegin();it != hits.rend();it++){
  151. transport.array() *= (it->mat.m_bsdf->eval(
  152. it->outdir,
  153. it->indir,
  154. it->normal
  155. ).array().cast<double>());
  156. for(const auto& pl : sc.pointlight){
  157. struct RTCIntersectContext sh_context;
  158. rtcInitIntersectContext(&sh_context);
  159. Vector3f sh_ray(pl.pos - it->pos);
  160. if(sh_ray.dot(it->normal) < 0)continue;
  161. float sh_ray_length = sh_ray.norm();
  162. sh_ray.normalize();
  163. RTCRay rsh_ray;
  164. rsh_ray.org_x = it->pos.x();
  165. rsh_ray.org_y = it->pos.y();
  166. rsh_ray.org_z = it->pos.z();
  167. rsh_ray.dir_x = sh_ray.x();
  168. rsh_ray.dir_y = sh_ray.y();
  169. rsh_ray.dir_z = sh_ray.z();
  170. rsh_ray.tnear = 0.001f;
  171. rsh_ray.tfar = sh_ray_length;
  172. rsh_ray.mask = -1;
  173. rsh_ray.flags = 0;
  174. raycasts++;
  175. rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
  176. if(rsh_ray.tfar > 0){
  177. transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * (1.0 / double(sh_ray_length * sh_ray_length));
  178. }
  179. }
  180. }
  181. accum += transport;
  182. }
  183. inner:
  184. (void)0;
  185. }
  186. img[j * n + i] = (accum.cast<float>() / 512.0f).array().pow(0.5f).matrix();
  187. }
  188. }
  189. }
  190. std::cout << std::to_string(raycasts) + " Raycasts\n";
  191. return img;
  192. }
  193. camera::camera(const Eigen::Vector3f& loc, const Eigen::Vector3f& look, const Eigen::Vector3f& u) : pos(loc), look_at(look), up(u){
  194. }