Thursday, January 8, 2015

Refraction

According to Snell's Law we have:
Sin Qt / Sin Qi = ni/nt = nr
Derivation of T: Assuming T is a unit vector, so the projection of T in the direction of M and -N is a and b respectively
T = a + b
T = (Sin Qt) * M - (Cos Qt) * N
M = (N * Cos Qi - V) / Sin Qi
Substituting M in T:
ALGORITHM:
1. Check whether object is transparent or not.

2. Check whether ray is going into the medium or going out of the medium (i.e. Check N.V < 0 then ray is going into the medium else going out)

3. If ray is going into the medium calculate nr and substitute into the equation.

4. Most important V is calculated as (intersection point - ray start) but when it is used into the equation we use -V.

5. If ray is going out of the medium we use nr as 1/nr and normal will be -N.

6. Recursively call shade method and compute the color from the objects behind the transparent object. 
C++ CODE
if(object.getTransparency() > 0){
 SbVec3f vVec = point - rayStart;
 vVec.normalize();
 if(normal.dot(vVec) < 0){ // going into the sphere
   float nr = 0.66;
   float rootContent = sqrtf(1 - nr * nr * (1 - (normal.dot(-vVec) *
                       (normal.dot(-vVec))))); 
   if(rootContent >= 0.0){
    transmisiveRay = (nr*(normal.dot(-vVec))-rootContent)*normal-(nr*-vVec);  
    pixelColor += object.getTransparency()*shade((point + 0.0009 *
                  vVec),transmisiveRay,recursionDepth + 1);
   }
 }
 else { // going out of sphere
   float nr = 1.5;
   float rootContent = sqrtf(1 - nr * nr * (1-(-normal.dot(-rayDirection)*
                       (-normal.dot(-rayDirection)))));
   if(rootContent >= 0.0){
      transmisiveRay = (nr * (-normal.dot(-rayDirection)) - rootContent) 
                        * -normal - ( nr * -rayDirection );    
      pixelColor += object.getTransparency() * shade((point + 0.0009 *
                    rayDirection),transmisiveRay, recursionDepth + 1);       
   }
 }
}
NOTE: vVec is code is my V in algorithm and normal is N in algorithm

No comments:

Post a Comment