• Advertisement
Sign in to follow this  

ogl lighting normals check

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wish to calculate normals for ogl lighting. I just want to check if the following is correct. May some one double check "CalculateNormal" for me...?
struct marker
   float x, y, z;

void CalculateNormal(marker* normal, marker o, marker b, marker c)
   b.x = b.x - o.x;
   b.y = b.y - o.y;
   b.z = b.z - o.z;

   c.x = c.x - o.x;
   c.y = c.y - o.y;
   c.z = c.z - o.z;

/* Cross Product. */
   normal->x = (b.y * c.z) - (b.z * c.y);
   normal->y = (b.z * c.x) - (b.x * c.z);
   normal->z = (b.x * c.y) - (b.y * c.x);

And example in action: marker model[30000] = {0}; marker normal[30000] = {0}; int n = 0; ... CalculateNormal(&normal[n], m[n + 0], m[n + 1], m[n + 2]);

Share this post

Link to post
Share on other sites
your CalculateNormal function code looks ok, but it has an odd
function signature. this function calculates the normal for a triangle
correct? that means that the outputing normal is the normal for that face
not each vertex. (because verticies can be shadered by multiple faces
its normal must be smoothed otherwise thelighting looks very poly-gonish)

what you should do (if you want to quickly test out the function, and see what I mean) is:

marker model [30000] = {0};
marker normal [30000] = {0};
int n = 0;
marker temp_norm;
CalculateNormal(&temp_norm, m[n + 0], m[n + 1], m[n + 2]);
normal[n+0] = temp_norm;
normal[n+1] = temp_norm;
normal[n+2] = temp_norm;
if you're using C you may want to use memcpy, if using C++ and the struct/class "marker" has functions overload the copy operator.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement