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)
2 Answers
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.
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$ Commented Nov 3, 2019 at 20:33
-