Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. vec4 castRay(vec4 p0, vec4 E, Object *lastHitObject, int depth){
  2.  
  3. std::vector<Object:: IntersectionValues> results (sceneObjects.size());
  4. Object::IntersectionValues min_intersect = sceneObjects[0]->intersect(p0,E);
  5. vec4 color = vec4 (0., 0., 0., 0.) ;
  6. min_intersect.t_w = std::numeric_limits< double >::infinity();
  7.  
  8. if(depth > maxDepth){
  9. return color;
  10. }
  11.  
  12. for(unsigned int i=0; i < sceneObjects.size(); i++) {
  13. Object::IntersectionValues intersect =sceneObjects[i]->intersect(p0,E);
  14. results[i] = sceneObjects[i] -> intersect(p0, E);
  15. min_intersect.ID_ = i;
  16. results[i].ID_ = i;
  17. if (min_intersect.t_w > intersect.t_w) {
  18. min_intersect = intersect;
  19. }
  20. }
  21.  
  22. std::sort(results.begin(),results.end(),intersectionSort);
  23. if(min_intersect.t_w != std::numeric_limits<double>::infinity() ){
  24.  
  25. int id = results[0].ID_;
  26. float material_shininess= sceneObjects[id]->shadingValues.Kn;
  27. color4 material_ambient(sceneObjects[id]->shadingValues.color.x*sceneObjects[id]->shadingValues.Ka,
  28. sceneObjects[id]->shadingValues.color.y*sceneObjects[id]->shadingValues.Ka,
  29. sceneObjects[id]->shadingValues.color.z*sceneObjects[id]->shadingValues.Ka, 1.0 );
  30.  
  31. color4 material_diffuse(sceneObjects[id]->shadingValues.color.x,
  32. sceneObjects[id]->shadingValues.color.y,
  33. sceneObjects[id]->shadingValues.color.z, 1.0 );
  34.  
  35. color4 material_specular(sceneObjects[id]->shadingValues.Ks,
  36. sceneObjects[id]->shadingValues.Ks,
  37. sceneObjects[id]->shadingValues.Ks, 1.0 );
  38.  
  39.  
  40. color4 ambient_product = GLState::light_ambient * material_ambient;
  41. ambient_product.w = 1.0;
  42. color4 diffuse_product = GLState::light_diffuse * material_diffuse;
  43. diffuse_product.w = 1.0;
  44. color4 specular_product = GLState::light_specular * material_specular;
  45. specular_product.w = 1.0;
  46.  
  47. vec4 L = normalize(lightPosition - min_intersect.P_w);
  48. L.w = 0.0;
  49.  
  50. vec4 N= normalize(min_intersect.N_w);
  51. vec4 R= normalize(reflect(L, min_intersect.N_w));
  52. vec4 V= normalize(E);
  53. float Kd = fmax( dot(L, N), 0.0 );
  54. float Ks = pow( fmax(dot(R,V), 0.0), material_shininess );
  55.  
  56. if (sceneObjects[id] -> shadingValues.Ks>0){
  57. vec4 reflectance = normalize(E - 2 * dot(E, N) * N);
  58. vec4 specular = Ks * specular_product * castRay(p0, reflectance, sceneObjects[id], depth+1);;
  59.  
  60. }
  61.  
  62.  
  63. vec4 ambient= ambient_product;
  64. vec4 diffuse= Kd * diffuse_product;
  65. vec4 specular = Ks * specular_product;
  66. color= ambient+diffuse+specular;
  67. if(shadowFeeler(min_intersect.P_w, sceneObjects[id]) != 0){
  68. color = vec4(0.0,0.0,0.0,1.0);
  69. }
  70. }
  71.  
  72. if(color.x > 1.0){ color.x = 1.0; }
  73. if(color.y > 1.0){ color.y = 1.0; }
  74. if(color.z > 1.0){ color.z = 1.0; }
  75.  
  76. color.w =1.0;
  77. return color;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement