12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #include "camera.hpp"
- #include <Eigen/Dense>
- #include "warping.hpp"
- std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sampler _rng)const{
- using namespace Eigen;
- std::vector<Eigen::Vector3f> img(n * n, Eigen::Vector3f(0,0,0));
- const Eigen::Vector3f lookray = (look_at - pos).normalized();
- const Eigen::Vector3f left = lookray.cross(up).normalized();
- const Eigen::Vector3f realup = left.cross(lookray);
- const int n2 = n / 2;
- size_t raycasts = 0;
- #pragma omp parallel
- {
- sampler rng;
- std::uniform_real_distribution<float> pert_dis(-0.5,0.5);
- #pragma omp for collapse(2) schedule(guided) reduction(+:raycasts)
- for(int j = 0;j < n;j++){
- for(int i = 0;i < n;i++){
-
- Eigen::Vector3d accum(0,0,0);
-
- for(size_t smp = 0;smp < 4096;smp++){
- 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);
- di.normalize();
- struct RTCRayHit rayhit;
- rayhit.ray.org_x = pos.x();
- rayhit.ray.org_y = pos.y();
- rayhit.ray.org_z = pos.z();
- rayhit.ray.dir_x = di.x();
- rayhit.ray.dir_y = di.y();
- rayhit.ray.dir_z = di.z();
- Vector3f hitpos = pos;
- Vector3f transport(1,1,1);
- for(size_t bounces = 0;bounces < 5;bounces++){
- struct RTCIntersectContext context;
- rtcInitIntersectContext(&context);
- rayhit.ray.tnear = 0.00002f;
- rayhit.ray.tfar = std::numeric_limits<float>::infinity();
- rayhit.ray.mask = -1;
- rayhit.ray.flags = 0;
- rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
- rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
- rtcIntersect1(sc.m_scene, &context, &rayhit);
- raycasts++;
- if(rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID){
- if(sc.emitters.contains(rayhit.hit.geomID)){
- accum += (Eigen::Vector3d(0,1,1).array() * transport.cast<double>().array()).matrix();
- //std::cout << "sd" << std::endl;
- goto inner;
- }
- //std::cout << std::to_string(rayhit.ray.tfar) + "\n";
- rayhit.ray.org_x += rayhit.ray.dir_x * rayhit.ray.tfar;
- rayhit.ray.org_y += rayhit.ray.dir_y * rayhit.ray.tfar;
- rayhit.ray.org_z += rayhit.ray.dir_z * rayhit.ray.tfar;
- Vector3f hitnormal = Vector3f(rayhit.hit.Ng_x, rayhit.hit.Ng_y, rayhit.hit.Ng_z).normalized();
- //std::cout << hitnormal << "\n\n";
- //Vector3f newdir = cosine_hemisphere(hitnormal, rng.next2D()).normalized();
- Vector3f newdir = uniform_sphere(rng.next2D());
- if(newdir.dot(hitnormal) <= 0){
- newdir *= -1.0f;
- }
- transport *= newdir.dot(hitnormal);
- //std::cout << std::to_string(newdir.dot(Vector3f(rayhit.hit.Ng_x, rayhit.hit.Ng_y, rayhit.hit.Ng_z).normalized())) + std::string("\n");
- rayhit.ray.dir_x = newdir.x();
- rayhit.ray.dir_y = newdir.y();
- rayhit.ray.dir_z = newdir.z();
- }
- else{
- goto inner;
- }
- }
- inner:
- (void)0;
- }
- img[j * n + i] = (accum.cast<float>() / 512.0f).array().pow(0.6f).matrix();
- }
- }
- }
- std::cout << std::to_string(raycasts) + " Raycasts\n";
- return img;
- }
- camera::camera(const Eigen::Vector3f& loc, const Eigen::Vector3f& look, const Eigen::Vector3f& u) : pos(loc), look_at(look), up(u){
-
- }
|