1
votes

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?

1
You have outlined what your code is supposed to do, but we have no idea what it actually does.n. 1.8e9-where's-my-share m.
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
Printing out your original ray data and intermediste calculations result could prove somewhat useful.n. 1.8e9-where's-my-share m.
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 rayuser3382260
I don't have time to check whether that is algebraically equivalent to this: math.stackexchange.com/questions/13261/… but I get the suspicion you've inverted the ray direction unintentionally.Necrolis

1 Answers

3
votes

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);