Advertisement
Guest User

Untitled

a guest
May 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. double intersect(ray *r, double *current_color, int level){
  2.  
  3. double t;
  4. t = getIntersectingT(r);
  5. if(t<=0){
  6.  
  7. return -1;
  8. }
  9. if(level == 0)
  10. {
  11.  
  12. return t;
  13. }
  14.  
  15. vector3 interSectionPoint;
  16. interSectionPoint = r->start + r->dir*t;
  17.  
  18.  
  19. double colorAt[3];
  20.  
  21. //getColor
  22. for(int i = 0;i<3;i++){
  23. colorAt[i] = color[i];
  24. }
  25.  
  26.  
  27.  
  28. //setColor
  29. for(int i = 0;i<3;i++){
  30. current_color[i] = colorAt[i]*co_efficients[0];
  31. }
  32.  
  33. //normalize
  34.  
  35. vector3 normal;
  36. normal = getNormal(interSectionPoint);
  37.  
  38.  
  39.  
  40. double sum_normal = sqrt(pow(normal.x,2)+pow(normal.y,2)+pow(normal.z,2));
  41.  
  42.  
  43. normal.normalize();
  44.  
  45.  
  46.  
  47.  
  48. for(int i = 0;i<lights.size();i++){
  49. vector3 lightDir;
  50. vector3 lightStart;
  51.  
  52. lightDir = lights.at(i) - interSectionPoint;
  53.  
  54.  
  55.  
  56.  
  57.  
  58. double sum = sqrt(pow(lightDir.x,2)+pow(lightDir.y,2)+pow(lightDir.z,2));
  59.  
  60. lightDir.normalize();
  61. lightStart = interSectionPoint + lightDir;
  62.  
  63.  
  64. ray *newL = new ray(lightStart,lightDir);
  65. //ray newL(,tempDir);
  66.  
  67. bool set_flag = false;
  68.  
  69. for(int j = 0;j<objects.size();j++){
  70. double tempT;
  71. tempT = objects.at(j)->getIntersectingT(newL);
  72.  
  73. if(tempT>0 && tempT<sum){
  74. set_flag = true;
  75. break;
  76.  
  77.  
  78. }
  79. }
  80.  
  81. if(set_flag!=true){
  82.  
  83.  
  84. double lambertValue = dot_product(lightDir,normal);
  85. if(lambertValue<0){
  86. lambertValue = 0;
  87.  
  88. }
  89.  
  90. vector3 a;
  91. vector3 tempB;
  92.  
  93. a = r->start - interSectionPoint ;
  94. a.normalize();
  95.  
  96. tempB = (normal*(dot_product(lightDir,normal)*2)) - lightDir;
  97.  
  98. tempB.normalize();
  99.  
  100.  
  101. double phongValue = dot_product(a,tempB);
  102. if(phongValue<0){
  103. phongValue = 0;
  104. }
  105.  
  106.  
  107. //cout<<"Lmabert value is "<<lambertValue<<"phong value is "<<phongValue<<endl;
  108.  
  109.  
  110. for(int p= 0;p<3;p++){
  111. current_color[p]+=lambertValue*co_efficients[1]*colorAt[p];
  112. current_color[p]+= pow(phongValue,Shine)*co_efficients[2]*colorAt[p];
  113.  
  114. }
  115. }
  116.  
  117.  
  118. if(level<recursion_level)
  119. {
  120. vector3 reflection= r->dir - (normal*(dot_product(r->dir,normal)*2));
  121.  
  122. vector3 newLightStart = interSectionPoint + reflection*1 ;
  123. ray *reflectionRay = new ray(newLightStart,reflection);
  124.  
  125. double nearest = -1;
  126.  
  127. double t_min = 999999;
  128. double dummyColor[3];
  129.  
  130. for(int k = 0;k<objects.size();k++){
  131. double t = objects[k] -> intersect(reflectionRay,dummyColor,0);
  132.  
  133.  
  134. if(t<=0)
  135. {
  136. continue;
  137. }
  138.  
  139.  
  140. if(t<t_min){
  141. t_min = t;
  142. nearest = k;
  143. }
  144. }
  145.  
  146. double reflected_color [3];
  147. if(nearest!=-1){
  148.  
  149. t = objects[nearest]->intersect(reflectionRay,reflected_color,level+1);
  150. for(int p = 0;p<3;p++){
  151. current_color[p]+=reflected_color[p]*co_efficients[3];
  152.  
  153. }
  154.  
  155.  
  156. }
  157.  
  158.  
  159.  
  160. for(int h = 0;h<3;h++){
  161. if(current_color[h]>1){
  162. current_color[h] = 1;
  163. }
  164. }
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement