AVasK

nex_hex

Nov 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1.     /*____        ___       ___  __ _________  _____       ___       _____    _____
  2.      ||  \\      /  .\      \ \  // |___*___|  ||  \\     /  .\     / ---_\  |*****|
  3.      ||__//     / ___.\      \  //     |*|     ||__//    / ___.\    | |      |**-/
  4.      ||--\\    / -----.\     / //      |*|     ||--\\   / -----.\   | |__/'  |**-\
  5.      ||   \\  /        .\   /_//       |*|     ||   \\ /        .\  \_____/  |*****|
  6.      */
  7.     // RAYTRACE CYCLE:
  8.     RTCRay ray;
  9.     //constants:
  10.     float max_rad = 0;
  11.     auto worldLight = 0.5f;
  12.     auto white = glm::vec3(255.0f, 255.0f, 255.0f);
  13.     auto red = glm::vec3(255.0f, 2.0f, 2.0f);
  14.     auto alpha = 0.05f;
  15.     // main cycle:
  16.     for (int y = 0; y < H; ++y) {
  17.         for (int x = 0; x < W; ++x) {
  18.             float radiance = 0;
  19.             glm::vec3 voxel_color(0,0,0);
  20.             glm::vec3 color(0, 0, 0);
  21.             float j = x - W/2.0 + 0.5f;
  22.             float i = y - H/2.0 + 0.5f;
  23.            
  24.             auto direction = camera_dir + j * dx + i * dy;
  25.             memcpy(ray.org, &camera_pos, sizeof(camera_pos)); //ray.org = camera_pos;
  26.             memcpy(ray.dir, &direction, sizeof(direction)); //ray.dir = camera_dir;
  27.             ray.tnear = 0.00001f;
  28.             ray.tfar = 1000.0f;
  29.             ray.geomID = RTC_INVALID_GEOMETRY_ID;
  30.             rtcIntersect(main_scene, ray);
  31.            
  32.             // RayTrace itself:
  33.             if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
  34.                 glm::vec3 norm(ray.Ng[0], ray.Ng[1], ray.Ng[2]);
  35.                 norm = glm::normalize(norm);
  36.                 norm = -norm;
  37.                
  38.                 auto intensity = -glm::dot(norm, light);
  39.                 intensity = glm::clamp(intensity, 0.0f, 1.0f);
  40.                
  41.                 color = white * (worldLight + intensity) / (worldLight + 1.0f);
  42.                
  43.                 if (ray.geomID == 2 || ray.geomID == 0) {
  44.                     //color += glm::vec3(white.x/40, white.y/4, white.z/40);
  45.                     ray = continue_ray(ray);
  46.                    
  47.                     rtcIntersect(main_scene, ray);
  48.                    
  49.                     if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
  50.                         glm::vec3 norm(ray.Ng[0], ray.Ng[1], ray.Ng[2]);
  51.                         norm = glm::normalize(norm);
  52.                         norm = -norm;
  53.                        
  54.                         intensity = -glm::dot(norm, light);
  55.                         intensity = glm::clamp(intensity, 0.0f, 1.0f);
  56.                         color = white * (worldLight + intensity) / (worldLight + 1.0f);
  57.                     }
  58.                     else {
  59.                         color.x = 0;
  60.                         color.y = 0;
  61.                         color.z = 0;
  62.                     }
  63.                 }
  64.            
  65.            
  66.              // Voxel part:
  67.              
  68.              glm::vec3 org(ray.org[0], ray.org[1], ray.org[2]);
  69.              glm::vec3 dir(ray.dir[0], ray.dir[1], ray.dir[2]);
  70.            
  71.             if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
  72.              auto hit = org + ray.tfar * dir;
  73.              
  74.              auto segment = hit - toVec3(ray.org);
  75.              auto step = glm::normalize(segment);
  76.        
  77.              for (float len = glm::length(segment); len > 0; len -= voxel_size) {
  78.                  auto point = toVec3(ray.org) +  step * len;
  79.                  int x = (point.x - bounds.lower_x) / voxel_size;
  80.                  int y = (point.y - bounds.lower_y) / voxel_size;
  81.                  int z = (point.z - bounds.lower_z) / voxel_size;
  82.                  
  83.                  if ((z*Qy+y)*Qx+x < size && (z*Qy+y)*Qx+x >= 0) {
  84.                      radiance = (voxels[(z*Qy+y)*Qx+x]);
  85.                      voxel_color = converter(radiance);
  86.                      color = alpha * voxel_color + (1.0f - alpha) * color;
  87.                  }
  88.              }
  89.             }
  90.              // Voxel part ends.
  91.    
  92.                 // Color output:
  93.                 color = glm::clamp(color, glm::vec3(0,0,0), glm::vec3(255,255,255));
  94.                 out << (int)color.x << " " << (int)color.y << " " << (int)color.z << "\n";
  95.             }
  96.             else { out << 0 << " " << 0 << " " << 0 << "\n";
  97.             }
  98.         }
Advertisement
Add Comment
Please, Sign In to add comment