Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. Vector4 Renderer::traceColor(Ray ray, Scene* scene, unsigned int recDepth) {
  2.     Vector4 color;
  3.    
  4.     IntersectionData* iData = scene->intersect(ray);
  5.    
  6.     if (iData) { // sucessful intersection test
  7.  
  8.         //shade color
  9.         color = m_shader->shade(iData, scene);
  10.        
  11.         if (recDepth < m_recursionDepth) {
  12.            
  13.             color = color.clamp01() * (1-iData->reflectionPercentage - iData->refractionPercentage);
  14.  
  15.             //normal direction
  16.             Vector3 n = iData->normal;
  17.            
  18.             //view direction
  19.             Vector3 w = -ray.direction;
  20.  
  21.             //origin of refraction/reflection ray
  22.             Vector3 o_r = iData->position;
  23.  
  24.             //any reflection?
  25.             if (iData->reflectionPercentage) {
  26.                
  27.                 //direction of reflection ray
  28.                 Vector3 d_refl = (n*(w.dot(n))*2-w);
  29.                 Ray reflRay = Ray(o_r,d_refl);
  30.                 reflRay.min_t = reflRay.epsilon_t;
  31.  
  32.                 color += traceColor(reflRay,scene,recDepth+1) * iData->reflectionPercentage;
  33.             }
  34.  
  35.             //any refraction?
  36.             if (iData->refractionPercentage) {
  37.  
  38.                 //just one object for now
  39.                 double refrFrac = scene->getRefractionIndex() / iData->refractionIndex;
  40.  
  41.                 if (!iData->rayEntersObject)
  42.                     refrFrac = 1/refrFrac;
  43.  
  44.                 //angle between view and normal
  45.                 double angle = w.dot(n);
  46.                
  47.                 //refraction direction
  48.                 Vector3 d_refr;
  49.                 if (angle >= asin(1/refrFrac)) { //case of total reflection
  50.                     d_refr = (n*(w.dot(n))*2-w);
  51.                 } else {
  52.                     d_refr = (n*w.dot(n)-w)*(refrFrac) - n*sqrt(1-refrFrac*refrFrac*(1-w.dot(n)*w.dot(n)));
  53.                 }
  54.  
  55.                 double checkangle = d_refr.normalize().dot(ray.direction);
  56.  
  57.                 Ray refrRay = Ray(o_r,d_refr);
  58.                 refrRay.min_t = refrRay.epsilon_t;
  59.  
  60.                 color += traceColor(refrRay,scene,recDepth+1) * iData->refractionPercentage;  
  61.  
  62.             }
  63.            
  64.         } else {
  65.             color = color.clamp01();
  66.         }
  67.     }
  68.     else {
  69.         color = scene->getBackground(); // background color
  70.     }
  71.  
  72.  
  73.     // clean
  74.     if (iData!=NULL){
  75.         delete(iData);
  76.     }
  77.  
  78.     return color.clamp01();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement