# Generating Normals

I can’t seem to get the code right for generating normals from vertex info…

Please could somebody post a snippet of code for generating vertex normals.

Thanks

Matt

ok. let’s say a triangle is composed of vertices v0, v1 and v2 in a counter-clockwise order. make 2 vectors: vec1 = (v1 - v0) and vec2 = (v2 - v0). now take the cross product: vec1 X vec2. normalize the resultant vector (divide each component by the vector’s length) and you have your normal!

b

[This message has been edited by coredump (edited 09-11-2002).]

/*_FILE:norm.c
*--------------------------------------------------------------
*

• Module Name : facet3norm
• Purpose :
• ``````       compute a unit vector normal to a
``````
• ``````       three sided facet
``````

-------------------------------------------------------------/
void norm(vertices,normal)
float vertices;
float normal;
{
float s0,s1;
float n0,n1;
float m0;
register short i;

/* get two side vectors */

for (i=0; i<=2; i++)
{
s0[i] = vertices[i] - vertices[i];
s1[i] = vertices[i] - vertices[i];
}

/* compute surface normal by crossing s0 with s1 */

`````` cross(n0,s0,s1);
``````

/* compute magnitude of normal */

m0 = magnitude(n0);

/* normalize to unity and copy to returned array */

for (i=0; i<=2; i++)
{
normal[i] = n0[i]/m0;
}
}