1

I'm now working on the ray tracer, reflection part. I have everything working correctly, including creating a sphere with shadow. Now, I'm implementing the reflection part. However, I couldn't get it. My algorithm is below:

traceRay(Ray ray, int counter){
// look through the intersection between ray and list of objects
// find the final index aka the winning index, (if final index == -1, return background color)
// then calculate the intersection point

// perform reflection calculation here
if(counter > 1  && winning object's reflectivity > 1 ){
  //get the intersection normal, vector N
  //Calculate the reflection ray, R
  // let I is the inverse of direction of incoming ray
  //Calculate R = 2aN - I (a = N dotProduct I)

  // the reflection ray is origin at the point of intersection between incoming ray and sphere with the R direction
  Ray reflecRay (intersection_poisition, R);

  Color reflection = traceRay(reflecRay, counter + 1);
  // multiply by fraction ks
  reflection = reflection * ks;
}


// the color of the sphere calculated using phong formula in shadeRay function
Color prefinal = shadeRay();


// return the total color of prefinal + reflection

}

I trying to get the reflection but couldn't get it, can anyone please let me know if my algorithm for traceRay function is correct?

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
user3382260
  • 15
  • 2
  • 7
  • You have outlined what your code is supposed to do, but we have no idea what it actually does. – n. m. could be an AI Mar 05 '14 at 07:17
  • 1
    how are you calculating your reflected vector? also, make sure you offset it from the object you initially colliding with to prevent self intersection (generally you offset along the reflected vector or the surface normal). – Necrolis Mar 05 '14 at 07:17
  • Printing out your original ray data and intermediste calculations result could prove somewhat useful. – n. m. could be an AI Mar 05 '14 at 07:21
  • The direction of reflected vector is calculated by: 2aN - I where N is the normal vector to the sphere and intersection point. a is N dot product I, and I is the inverse of the direction of the incoming ray – user3382260 Mar 05 '14 at 07:22
  • I don't have time to check whether that is algebraically equivalent to this: http://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector but I get the suspicion you've inverted the ray direction unintentionally. – Necrolis Mar 05 '14 at 07:28
  • Thanks for your link. I have checked. It's the same as what I did. In fact, the formula is given by my professor, so, I'm pretty sure that's correct. – user3382260 Mar 05 '14 at 07:33

1 Answers1

4

When reflecting a ray, you need to move it along the reflector's normal to avoid intersection with the reflector itself. For example:

 const double ERR = 1e-12;
 Ray reflecRay (intersection_poisition + normal*ERR, R);
concept3d
  • 2,248
  • 12
  • 21