Advertisement
Guest User

Untitled

a guest
May 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.42 KB | None | 0 0
  1.  
  2. #include <limits>
  3.  
  4. #include "Scene.h"
  5.  
  6. #include "task1b.h"
  7. #include <iostream>
  8. constexpr float epsilon = 0.0001f;
  9.  
  10. const triangle_t* findClosestHit(
  11.     const float3& p, const float3& d,
  12.     const triangle_t* triangles, std::size_t num_triangles,
  13.     const float3* vertices,
  14.     float& t, float& lambda_1, float& lambda_2)
  15. {
  16.     // TODO: implement intersection test between a ray and a set of triangles.
  17.     // This function should find the CLOSEST intersection with a triangle along the ray.
  18.     // The ray is given by its start point p and direction d.
  19.     // A triangle is represented as an array of three vertex indices.
  20.     // The position of each vertex can be looked up from the vertex array via the vertex index.
  21.     // triangles points to the first element of an array of num_triangles triangles.
  22.     // If an intersection is found, set t to the ray parameter and
  23.     // lambda_1 and lambda_2 to the barycentric coordinates corresponding to the
  24.     // closest point of intersection, and return a pointer to the hit triangle.
  25.     // If no intersection is found, return nullptr.
  26.     for(int i = 0; i < num_triangles; i++)
  27.     {
  28.  
  29.         float3 e = vertices[triangles[i][1]] - vertices[triangles[i][0]];
  30.         float3 e2 = vertices[triangles[i][2]] - vertices[triangles[i][0]];
  31.         float3 q = p - vertices[triangles[i][0]];
  32.         //float3 e = normalize(e_temp);
  33.         //float3 e2 = normalize(e2_temp);
  34.         //float3 q = normalize(q_temp);
  35.         float check_intersect = (dot(cross(d,e2),e));
  36.  
  37.         if(check_intersect > 0.000000f)
  38.         {
  39.  
  40.         t = (dot(cross(q,e),e2))/ (dot(cross(d,e2),e));
  41.         lambda_1 = (dot(cross(d,e2),q))/ (dot(cross(d,e2),e));
  42.         lambda_2 = (dot(cross(q,e),d))/ (dot(cross(d,e2),e));
  43.          if(lambda_1  >= 0.000000f && lambda_2 >= 0.0000000f && (lambda_1+lambda_2)  <= 1.000000f)
  44.          {
  45.            
  46.             return &triangles[i];
  47.          }
  48.         }
  49.  
  50.     }
  51.  
  52.     //std::cout << triangles[0][1] << std::endl;
  53.    
  54.     return nullptr;
  55. }
  56.  
  57. bool intersectsRay(
  58.     const float3& p, const float3& d,
  59.     const triangle_t* triangles, std::size_t num_triangles,
  60.     const float3* vertices,
  61.     float t_min, float t_max)
  62. {
  63.  
  64.     // TODO: implement intersection test between a ray and a set of triangles.
  65.     // This method only has to detect whether there is an intersection with ANY triangle
  66.     // along the given subset of the ray.
  67.     // The ray is given by its start point p and direction d.
  68.     // A triangle is represented as an array of three vertex indices.
  69.     // The position of each vertex can be looked up from the vertex array via the vertex index.
  70.     // triangles points to an array of num_triangles.
  71.     // If an intersection is found that falls on a point on the ray between
  72.     // t_min and t_max, return true.
  73.     // Otherwise, return false.
  74.     for(int i = 0; i < num_triangles; i++)
  75.     {
  76.         float3 e = vertices[triangles[i][1]] - vertices[triangles[i][0]];
  77.         float3 e2 = vertices[triangles[i][2]] - vertices[triangles[i][0]];
  78.         float3 q = p - vertices[triangles[i][0]];
  79.         float check_intersect = (dot(cross(d,e2),e));
  80.  
  81.         if(check_intersect > 0.000000f)
  82.         {
  83.         float t = (dot(cross(q,e),e2))/ (dot(cross(d,e2),e));
  84.     //t = (dot(cross(q,e),e2))/ (dot(cross(d,e2),e));
  85.         float lambda_1 = (dot(cross(d,e2),q))/ (dot(cross(d,e2),e));
  86.         float lambda_2 = (dot(cross(q,e),d))/ (dot(cross(d,e2),e));
  87.      
  88.         if(lambda_1  >= 0.000000f && lambda_2 >= 0.000000f && (lambda_1+lambda_2)  <= 1.000000f && t_min < t  && t < t_max )
  89.             return true;
  90.  
  91.         }
  92.     }
  93.  
  94.     return false;
  95. }
  96.  
  97. float3 shade(
  98.     const float3& p, const float3& d,
  99.     const HitPoint& hit,
  100.     const Scene& scene,
  101.     const Pointlight* lights, std::size_t num_lights)
  102. {
  103.     // TODO: implement phong shading.
  104.     // hit represents the surface point to be shaded.
  105.     // hit.position, hit.normal, and hit.k_d and hit.k_s contain the position,
  106.     // surface normal, and diffuse and specular reflection coefficients,
  107.     // hit.m the specular power.
  108.     // lights is a pointer to the first element of an array of num_lights
  109.     // point light sources to consider.
  110.     // Each light contains a member to give its position and color.
  111.     // Return the shaded color.
  112.  
  113.     float3 light_color = {0.0f, 0.0f,0.0f};  
  114.     float3 cd = {0.0f, 0.0f,0.0f};
  115.     float3 cs = {0.0f, 0.0f,0.0f};
  116.     //float3 temp = {0.0f, 0.0f,0.0f};
  117.     for(int i = 0; i < num_lights; i++)
  118.     {
  119.         //float3 p_new = normalize(p);
  120.         float3 light_dir = lights[i].position - hit.position;
  121.         float3 light_dir_n = normalize(light_dir);
  122.         float3 n = normalize(hit.normal);
  123.        
  124.         float cos_ = dot(light_dir_n,n);
  125.          cd = (hit.k_d * std::fmax(cos_,0.0f));
  126.         //float3 cd = normalize(cd_tmp);
  127.         float3 r =    light_dir_n - (2*(dot(light_dir_n, n)) * n) ;///I - 2 * dotProduct(I, N) * N
  128.         //*d = normalize(d);
  129.         //float3 r = normalize(light_dir + p_new);
  130.         float3 r_n = normalize(r);
  131.         float3 d_new = normalize(d);
  132.         float cos_r = dot(r_n, d_new);
  133.        
  134.      
  135.      
  136.          
  137.          
  138.           //cd.x = cd.x * lights[i].color.x;
  139.           //cd.y = cd.y * lights[i].color.y;
  140.           //cd.z = cd.z * lights[i].color.z;
  141.        
  142.          
  143.          // temp = temp+ cs + cd;
  144.          //temp.x = temp.x *  lights[i].color.x;
  145.          //temp.y = temp.y *  lights[i].color.y;
  146.          //temp.z = temp.z *  lights[i].color.z;
  147.          
  148.          //float light_color_temp =  dot((cs+cd),lights[i].color);
  149.          
  150.           //light_color = light_color + cs + cd + lights[i].color;
  151.          ///light_color = light_color * light_color_temp;
  152.            //float3 light_color_1 =  (cs + cd) * lights[i].color.x;
  153.             //float3 light_color_2 =       (cs + cd) *  lights[i].color.y;
  154.             //float3 light_color_3 =     (cs+cd) * lights[i].color.z;
  155.           //light_color = light_color + (cs + cd) * (lights[i].color.x * lights[i].color.y * lights[i].color.z);
  156.           //light_color =  (cs + cd) * lights[i].color.y;
  157.           //light_color =  (cs + cd) * lights[i].color.z;
  158.         // t_min = (hit.position.x + epsilon) * hit.normal.x;
  159.            
  160.            //light_color =  light_color +  (cs + cd);
  161.          float3 start = hit.position + (hit.normal * epsilon);
  162.          if(scene.intersectsRay(start, light_dir,epsilon, std::numeric_limits<float>::infinity()))
  163.             continue;
  164.  
  165.          
  166.                 if(cos_ + epsilon > 0.0000f)
  167.                 {
  168.            
  169.                   cs = (hit.k_s * std::pow(std::fmax(cos_r,0.0f),hit.m));
  170.           // cs.x = (hit.k_s.x * std::pow(std::fmax(cos_r,0.0f),hit.m));
  171.           // cs.x = cs.x *  lights[i].color.x;
  172.           //cs.y = cs.y * lights[i].color.y;
  173.           //cs.z = cs.z * lights[i].color.z;
  174.        
  175.            
  176.                 }
  177.                 else
  178.                 {
  179.                     cs = {0.f,0.f,0.f};
  180.                 }
  181.            light_color.x += (cs.x + cd.x) * lights[i].color.x;
  182.            light_color.y +=  (cs.y + cd.y)* lights[i].color.y;
  183.            light_color.z += (cs.z + cd.z) * lights[i].color.z;
  184.          
  185.          //else
  186.             //light_color = light_color + (cs + cd);
  187.          //return light_color;
  188.     }
  189. //scene.intersectsRay(p,d,0.0f,20.f);
  190.  
  191.  return light_color;
  192.  
  193.     // To implement shadows, use scene.intersectsRay(p, d, t_min, t_max) to test
  194.     // whether a ray given by start point p and direction d intersects any
  195.     // object on the section between t_min and t_max.
  196.     //scene.intersectsRay(p,d,0,0);
  197.  
  198. //  return hit.k_d;
  199. }
  200.  
  201. void render(
  202.     image2D<float3>& framebuffer,
  203.     int left, int top, int right, int bottom,
  204.     const Scene& scene,
  205.     const Camera& camera,
  206.     const Pointlight* lights, std::size_t num_lights,
  207.     const float3& background_color,
  208.     int max_bounces)
  209. {
  210.     // TODO: implement raytracing, render the given portion of the framebuffer.
  211.     // left, top, right, and bottom specify the part of the image to compute
  212.     // (inclusive left, top and exclusive right and bottom).
  213.     // camera.eye, camera.lookat, and camera.up specify the position and
  214.     // orientation of the camera, camera.w_s the width of the image plane,
  215.     // and camera.f the focal length to use.
  216.     // Use scene.findClosestHit(p, d) to find the closest point where the ray
  217.     // hits an object.
  218.     // The method returns an std::optional<HitPoint>.
  219.     // If an object is hit, call the function shade to compute the color value
  220.     // of the HitPoint illuminated by the given array of lights.
  221.     // If the ray does not hit an object, use background_color.
  222.  
  223.     // BONUS: extend your implementation to recursive ray tracing.
  224.     // max_bounces specifies the maximum number of secondary rays to trace.
  225.    
  226.    float aspect_ratio = width(framebuffer) / (float)height(framebuffer);
  227.    float h_s = camera.w_s/aspect_ratio;
  228.  
  229.  //  float x = camera.eye;
  230.  //  float3 x = (camera.eye - camera.lookat);
  231.    //float3 y = (normalize(camera.eye - camera.lookat));
  232.  
  233.     float3 w = normalize(camera.eye-camera.lookat);
  234.    // float3 w;
  235.    // w.x = x.x/check;
  236.    // w.y = x.y/check;
  237.    // w.z = x.z/check;
  238.     float3 u = normalize(cross(camera.up, w));
  239.     //float leng = length(u_temp);
  240.      //float3 u;
  241.      //u.x = u_temp.x/leng;
  242.     // u.y = u_temp.y/leng;
  243.      //u.z = u_temp.z/leng;
  244.      float3 v = cross(w,u);
  245.     //float3 p_origin = camera.eye;
  246.  
  247.  
  248.    //Camera y =  normalize(camera.eye-camera.lookat);
  249.     for (int y = top; y < bottom; ++y)
  250.     {
  251.         float y_position =  - h_s*(y+0.5f)/height(framebuffer) + h_s/2.0f;
  252.         for (int x = left; x < right; ++x)
  253.         {
  254.             float x_position = - camera.w_s/2.0f + camera.w_s*(x + 0.5f)/width(framebuffer);
  255.             //float f = -camera.f;
  256.             float3 d = (-(camera.f*w)) + x_position*u + y_position*v;
  257.             //float3 d = {camera.f*w,(float)x*u,(float)y*w};
  258.             std::optional<HitPoint> hit = scene.findClosestHit(camera.eye,d);
  259.             max_bounces--;
  260.  
  261.  
  262.             if(hit)
  263.             {
  264.                
  265.  
  266.                 ///std::cout<<"inside\n";
  267.                
  268.                 framebuffer(x,y) =  shade(camera.eye,d,*hit,scene,lights,num_lights);
  269.             }
  270.             else
  271.             {
  272.                
  273.                 framebuffer(x, y) = background_color;
  274.             }
  275.        
  276.            
  277.            
  278.            
  279.         }
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement