View Full Version : Generating Normals

playworker

09-11-2002, 08:45 AM

Sorry in advance for asking something that has probably been asked many times before.

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

coredump

09-11-2002, 08:58 AM

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

pleopard

09-11-2002, 09:08 AM

/*_FILE:norm.c

*--------------------------------------------------------------

*

* Module Name : facet3norm

*

* Purpose :

* compute a unit vector normal to a

* three sided facet

*

*

*-------------------------------------------------------------*/

void norm(vertices,normal)

float vertices[4][3];

float normal[3];

{

float s0[3],s1[3];

float n0[3],n1[3];

float m0;

register short i;

/* get two side vectors */

for (i=0; i<=2; i++)

{

s0[i] = vertices[0][i] - vertices[3][i];

s1[i] = vertices[1][i] - vertices[0][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;

}

}

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.