Browse Source

Sache funktioniered imfall

manuel5975p 4 năm trước cách đây
mục cha
commit
ee6dd16012
3 tập tin đã thay đổi với 41 bổ sung27 xóa
  1. 14 14
      camera.cpp
  2. 23 10
      main.cpp
  3. 4 3
      material.hpp

+ 14 - 14
camera.cpp

@@ -28,7 +28,7 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
             Eigen::Vector3d accum(0,0,0);
             std::vector<hit> hits;
             hits.reserve(6);
-            for(size_t smp = 0;smp < 512;smp++){
+            for(size_t smp = 0;smp < 256;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;
@@ -58,10 +58,10 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                             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>());
                             for(auto it = hits.rbegin();it != hits.rend();it++){
                                 transport.array() *= (it->mat.m_bsdf->eval(
-                                    it->outdir,
-                                    it->indir,
+                                    -it->outdir,
+                                    -it->indir,
                                     it->normal
-                                ).array().cast<double>());
+                                ).array().cast<double>()) * it->mat.tex->eval(0,0).array().cast<double>();
                                 for(const auto& pl : sc.pointlight){
                                     struct RTCIntersectContext sh_context;
                                     rtcInitIntersectContext(&sh_context);
@@ -83,7 +83,7 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                                     raycasts++;
                                     rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
                                     if(rsh_ray.tfar > 0){
-                                        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));
+                                        transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * it->mat.tex->eval(0,0).array().cast<double>() * (1.0 / double(sh_ray_length * sh_ray_length));
                                     }
                                 }
                             }
@@ -115,10 +115,10 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                         Eigen::Vector3d transport(0,0,0);
                         for(auto it = hits.rbegin();it != hits.rend();it++){
                             transport.array() *= it->mat.m_bsdf->eval(
-                                it->outdir,
-                                it->indir,
+                                -it->outdir,
+                                -it->indir,
                                 it->normal
-                            ).array().cast<double>();
+                            ).array().cast<double>() * it->mat.tex->eval(0,0).array().cast<double>();
                             for(const auto& pl : sc.pointlight){
                                 struct RTCIntersectContext sh_context;
                                 rtcInitIntersectContext(&sh_context);
@@ -140,7 +140,7 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                                 raycasts++;
                                 rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
                                 if(rsh_ray.tfar > 0){
-                                    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));
+                                    transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * it->mat.tex->eval(0,0).array().cast<double>() * (1.0 / double(sh_ray_length * sh_ray_length));
                                 }
                             }
                         }
@@ -152,10 +152,10 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                 Eigen::Vector3d transport(0,0,0);
                 for(auto it = hits.rbegin();it != hits.rend();it++){
                     transport.array() *= (it->mat.m_bsdf->eval(
-                        it->outdir,
-                        it->indir,
+                        -it->outdir,
+                        -it->indir,
                         it->normal
-                    ).array().cast<double>());
+                    ).array().cast<double>()) * it->mat.tex->eval(0,0).array().cast<double>();
                     for(const auto& pl : sc.pointlight){
                         struct RTCIntersectContext sh_context;
                         rtcInitIntersectContext(&sh_context);
@@ -177,7 +177,7 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                         raycasts++;
                         rtcOccluded1(sc.m_scene, &sh_context, &rsh_ray);
                         if(rsh_ray.tfar > 0){
-                            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));
+                            transport.array() += pl.color.cast<double>().array() * it->mat.m_bsdf->eval(-sh_ray.normalized(), it->indir, it->normal).cast<double>().array() * it->mat.tex->eval(0,0).array().cast<double>() * (1.0 / double(sh_ray_length * sh_ray_length));
                         }
                     }
                 }
@@ -186,7 +186,7 @@ std::vector<Eigen::Vector3f> camera::get_image(const scene& sc, size_t n, sample
                 inner:
                 (void)0;
             }
-            img[j * n + i] = (accum.cast<float>() / 512.0f).array().pow(0.5f).matrix();
+            img[j * n + i] = (accum.cast<float>() / 128.0f).array().pow(0.5f).matrix();
         }
     }
     }

+ 23 - 10
main.cpp

@@ -22,6 +22,8 @@ RTCDevice initializeDevice(){
     return device;
 }
 int main(){
+    using std::cos;
+    using std::sin;
     using namespace Eigen;
     RTCDevice dev = initializeDevice();
 
@@ -33,24 +35,35 @@ int main(){
     objl::Mesh ajax_mesh = ajax.LoadedMeshes[0];
     objl::Mesh quad_mesh = quad.LoadedMeshes[0];
     Matrix4f trf;
-    trf << 1,0,0,0,
-           0,1.5,0,0,
-           0,0,1,0,
-           0,0,0,1;
-    unsigned aji = scene.add_object(ajax_mesh, trf, false);
-    scene.added_objects[scene.geom_id_to_object_index_map.find(aji)->second]->mat.m_bsdf = std::make_unique<microfacet_bsdf>(0.1);
+    std::uniform_real_distribution<float> angles(0, 2 * M_PI);
+    std::uniform_real_distribution<float> o1(0, 1);
+    xoshiro_256 gangles(42);
+    for(float ax = -10;ax < 10;ax += 5){
+        for(float ay = -10;ay < 10;ay += 5){
+            float angle = angles(gangles);
+            trf << cos(angle),0,-sin(angle),ax,
+                   0,1.6,0,0,
+                   sin(angle),0,cos(angle),ay,
+                   0,0,0,1;
+            scene.add_object(ajax_mesh, trf, false);
+            scene.added_objects.back()->mat.tex = std::make_unique<uni_texture>(Color(o1(gangles), o1(gangles), o1(gangles)));
+            scene.added_objects.back()->mat.m_bsdf = std::make_unique<microfacet_bsdf>(0.8);
+        }
+    }
+    
     trf << 40,0,0,0,
-           0,1,0,-1.5,
-           0,0,4,0,
+           0,1,0,-1.6,
+           0,0,40,0,
            0,0,0,1;
     scene.add_object(quad_mesh, trf, false);
 
     //scene.add_sphere(Vector3f(-1.5,0,0),0.5, true);
     scene.pointlight.push_back(pointlight{Eigen::Vector3f(2,0,0), Eigen::Vector3f(1,1,0)});
+    scene.pointlight.push_back(pointlight{Eigen::Vector3f(2,0,2), Eigen::Vector3f(0,1,1)});
     scene.build();
-    camera cam(Vector3f(0, 0.6, 2.4),Vector3f(0,0,0), Vector3f(0,1,0));
+    camera cam(Vector3f(0, 0.6 * 5, 2.4 * 5),Vector3f(0,0,0), Vector3f(0,1,0));
     sampler simp;
-    size_t n = 500;
+    size_t n = 1000;
     auto t1 = nanoTime();
     auto x = cam.get_image(scene, n, simp);
     auto t2 = nanoTime();

+ 4 - 3
material.hpp

@@ -18,6 +18,7 @@ struct uni_texture : texture{
 };
 struct bsdf{
     virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const{
+        throw 5;
         return Eigen::Vector3f(1,1,1);
     }
 };
@@ -32,11 +33,11 @@ struct microfacet_bsdf : bsdf{
     microfacet_bsdf() : alpha(0.1f){}
     microfacet_bsdf(float _alpha) : alpha(_alpha){}
     virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const override{
-        float x = in.dot(-normal);
-        return Eigen::Vector3f(x,x,x);
+        //float x = in.dot(-normal);
+        //return Eigen::Vector3f(x,x,x);
         Eigen::Vector3f mirror_ref = in + normal * (in.dot(normal));
         //float x = beckmann_eval(mirror_ref, alpha);
-        float xd = 3.0f * std::exp(-mirror_ref.dot(out));
+        float x = 1.0f * std::exp(mirror_ref.dot(out) - 1);
         return Color(x,x,x);
     }
 };