I am having the following issue. I am working on a ray tracer for school and I am trying to render a sphere where the radiance of a intersection point is the dot product of the ray direction and the normal at the point of intersection. This works fine if return the inverted normal ( i.e. a vector going from the intersection point to the center of the sphere but not the other way around as it should be ) and I can't figure out what I messed up. Anyone have a intuitive clue on what I could have messed up? Can't really post code here as it's a school task but feel free to ask for further information and I am trying to provide as best as I can. (left: inverted normals, right: right normals)

inverted_normalsright normals


The issue is, that you considered that the rays coming from the camera to be the light carrying rays. Instead, the rays bounce off the surface, and return to the light source (which you have decided to position at the camera). This means that your light direction is not $\pmb{d}$ but rather $-\pmb{d}$, then $\cos\theta = - \pmb{d} \cdot \pmb{n}$, provided that both vectors are normalized.

Also, you really want to try to retirn the correct normal, as it is, your current code will return the wrong normal if the camera is inside the sphere. So your normal should be: n = dot(n,d)<0 ? n : -n;, where n is the normal returned by your surface, and d is the ray direction for the ray that intersected the surface.

  • $\begingroup$ thanks, very much! :) $\endgroup$ – Yamahari Nov 3 at 20:37

When you take the dot product of two unit vectors, you are essentially calculating the cosine of the angle between them. This value can become negative when the two vectors are less than 90 degrees apart (which they are when you have outward normals).

Negate the normal before dotting it. That way you can have outward facing normals and also correct shading.

  • 1
    $\begingroup$ His normals are already outwards facing. The issue is that he took the wrong light direction. $\endgroup$ – lightxbulb Nov 3 at 20:33
  • $\begingroup$ I stand corrected. $\endgroup$ – AnnoyinC Nov 3 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.