2

I am trying to implement reflection but I have problems with formulas I have found.

Calculate the Standards N and V, to make these 2 vectors unitary. Next, calculate the scalar product N.V. To finish with the forumulas below calculate R coordinate : Rx, Ry and Rz.

R = -2N * V.N + V.

At the moment I have this but it does not look great:

enter image description here

And this is my function of reflection :

t_color         reflection(t_rt *rt, t_obj *obj, t_lvector *vec, t_color tmp_color)
{
  static int    number_loop;
  t_vector      new_vec;
  t_vector      eye;
  t_vector      normal;
  t_color       color;
  float         scalaire;
  t_obj         *new_obj;

  if (++number_loop == 200)
    {
      number_loop = 0;
      return (tmp_color);
    }
  color.r = tmp_color.r;
  color.g = tmp_color.g;
  color.g = tmp_color.b;
  find_normal(&normal, vec, obj);
  scalaire = (rt->vec->x * normal.x) +
    (rt->vec->y * normal.y) + (rt->vec->z * normal.z);
  eye.x = vec->px;
  eye.y = vec->py;
  eye.z = vec->pz;
  new_vec.x = -2 * normal.x * scalaire + rt->vec->x;
  new_vec.y = -2 * normal.y * scalaire + rt->vec->y;
  new_vec.z = -2 * normal.z * scalaire + rt->vec->z;
  rt->vec = &new_vec;
  rt->eye = &eye;
  new_obj = find_nearest_obj(rt->obj, &eye, &new_vec);
  if (new_obj)
    color = calculate_light(rt, new_obj);
  return (color);
}

vec->pXYZ is the coordinate of where the ray hits an object.

Thanks !

PS : Sorry for my bad english, I am french.

Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45
  • 2
    Hey :D ! Thought I'd never see one from Epitech around here. Your `reflection` function does not respect the norm (too many lines). Apart from that, reflection is replacing your hit coordinate with the eye and cast a new ray you get from the hitting ray and the vector perpendicular to the surface hit – Eregrith Jun 03 '15 at 12:16
  • And seeing your capture it seems you might have a rounding problem, or a small problem calculating that second ray's orientation OR you can have problems along "calculate_light" and returning black instead of the actual reflected object's color – Eregrith Jun 03 '15 at 12:23
  • 1
    And GG for your 42 reputation :P – Eregrith Jun 03 '15 at 12:33
  • 1
    either your vectors are not what they should be or they are not in the same coordinate system , try to add debug draw for single ray drawing the vectors and see if they are really what they should (V reflected ray,N surface normal,R incident ray) beware R and V should be opposite directions !!! Also you could have problem with color mixing of each ray together they depend on the dot product too, shininess, etc ... the `number_loop ` looks suspicious where it is reseted ? I see only reset after overflow and 200 recursions seem like too much I use 3-12 tops – Spektre Jun 03 '15 at 16:16
  • It looks like you've got some fuzz on your sphere - you may be able to fix this by moving the intersection point by a small epsilon in the direction of the surface normal before casting your shadow or reflection rays. – Daniel A. Thompson Aug 29 '15 at 14:47
  • @DanielA.Thompson hmm still not solved? maybe this [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) will help (for comparison). The noisy sphere is most likely due to low precision (`float` is not enough try `double` instead) – Spektre Sep 30 '18 at 09:30

0 Answers0