Notifications
Clear all

[Closed] compare 2 normals

hi,

I was wondering how to compare 2 normals.

An example: 3 objects in the scene, a teapot, box and light.

and now i would like to align the box on the teapot faces only if their faces have the same normal as the light.

2 Replies

A light doesn’t have a ‘normal’, if it is directional type of light (spot, direction, etc.) it will have a direction. Both of these are vectors, and are thus comparable. However, you need to determine whether you want to test against the light’s direction, or against the vector from face/vertex to light, I cannot tell from your question (the results will be quite different).

You can use a dot product to compare two normalized vectors. A dot product of 1 means they are facing each other perfectly, 0 means they are perpendicular, -1 means they are parallel (ie, the normal is facing directly away from the light).

So for each vert, you can run:

nDotL = dot (getNormal <mesh> <vert_index_integer>) theLight.dir
--nDotL = dot (getNormal <mesh> <vert_index_integer>) ((normalize (getVert <mesh> <vert_index_integer>)) - normalize (theLight.pos))  --would use the vector from light to vert pos instead

if nDotL > 0 then ()
else if nDotL < 0 then ()
else ()

You’d need to use faces instead of verts though.

thx for your answer