AVasK

2

Nov 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1.  importer(main_scene);
  2.     // Construct of type: unsigned int geomID = rtcNewTriangleMesh(main_scene, numTriangles, numVertices, 1);
  3.     //unsigned int geomID = rtcNewTriangleMesh(new_scene, size_t(num_of_faces), size_t(num_of_vertices), 1);
  4.     // Commit changes
  5.     rtcCommit(main_scene);
  6.     // Raytrace
  7.     float x, y, z;
  8.     x = 3;
  9.     y = 3;
  10.     z = 3;
  11.    
  12.     glm::vec3 camera_pos(x, y, z);
  13.     glm::vec3 dx(-y, x, 0);
  14.     glm::vec3 dy = glm::cross(dx, camera_pos);
  15.     glm::vec3 camera_dir = -camera_pos;
  16.     //---------------
  17.     glm::vec3 light(1, 3, 2);
  18.    
  19.     //----------------
  20.    
  21.     dx = glm::normalize(dx);
  22.     dy = glm::normalize(dy);
  23.    
  24.     float fov = 0.001;
  25.     auto len = glm::length(camera_dir);
  26.     dx *= len * fov;
  27.     dy *= len * fov;
  28.    
  29.     // OUTPUT:
  30.     const int H = 1000;
  31.     const int W = 1000;
  32.    
  33.     std::ofstream out("out.ppm");
  34.     out << "P3\n" << W << ' ' << H << ' ' << "255\n";
  35.    
  36.     for (int y = 0; y < H; ++y) {
  37.         for (int x = 0; x < W; ++x) {
  38.             RTCRay ray;
  39.             float i = x - W/2.0 + 0.5;
  40.             float j = y - H/2.0 + 0.5;
  41.            
  42.             auto direction = camera_dir + j * dx + i * dy;
  43.             memcpy(ray.org, &camera_pos, sizeof(camera_pos)); //ray.org = camera_pos;
  44.             memcpy(ray.dir, &direction, sizeof(direction)); //ray.dir = camera_dir;
  45.             ray.tnear = 0.00001f;
  46.             ray.tfar = 100000000.0f;
  47.             ray.geomID = RTC_INVALID_GEOMETRY_ID;
  48.             rtcIntersect(main_scene, ray);
  49.             //-----
  50.            
  51.             glm::vec3 norm(ray.Ng[0], ray.Ng[1], ray.Ng[2]);
  52.             if (norm.x*norm.x + norm.y*norm.y + norm.z*norm.z != 0) {
  53.                 norm = glm::normalize(norm);
  54.                 norm = -norm;
  55.             }
  56.             auto worldLight = 0.25f;
  57.             auto intensity = -glm::dot(norm, light);
  58.             intensity = glm::clamp(intensity, 0.0f, 1.0f);
  59.             auto white = glm::vec3(255.0f, 255.0f, 255.0f);
  60.             auto color = white * (worldLight + intensity) / (worldLight + 1.0f);
  61.             color = glm::clamp(color, 0, 255);
  62.             auto depthmap = white * ray.tfar;
  63.             if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
  64.                 depthmap.x = 0;
  65.                 depthmap.y = 0;
  66.                 depthmap.z = 0;
  67.             }
  68.            
  69.            
  70.             if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
  71.                 //out << 255 << " " << 255 << " " << 255 << "\n";
  72.                 out << color.x << " " << color.y << " " << color.z << "\n";
  73.                 std::cout << color.x << " " << color.y << " " << color.z << " ";
  74.                
  75.                 //out << depthmap.x << ' ' << depthmap.y << ' ' << depthmap.z << '\n';
  76.             }
  77.             else { out << 0 << " " << 0 << " " << 0 << "\n"; }
  78.         }
  79.         std::cout << "\n";
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment