# ogl lighting normals check

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

## 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);

return;
}

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 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.

1. 1
2. 2
3. 3
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632911
• Total Posts
3009187
• ### Who's Online (See full list)

There are no registered users currently online

×