Advertisement

proper code/maths for generating a normal

Started by April 24, 2003 12:38 PM
1 comment, last by jollyjeffers 21 years, 9 months ago
hi, I''ve just found out I have a ''flaw'' in one of my core assumptions regarding calculating vertex normals for 3D rendering... which is a bit of a bummer, cos that could mean loads of previous code is "broken" (however, visual results seem to contradict this)... ANYWAY... Is this process the correct way to generate a normal for a triangle: 1. take the vector from v0 to v1 2. take the vector from v0 to v2 3. take the cross product of these 2 vectors 4. normalize the result or in (d3d) code:
  
//Variables

D3DXVECTOR3 vN, v01, v02;

//calculate the normal

v01 = p1.p - p0.p;
v02 = p2.p - p0.p;

D3DXVec3Cross(&vN,&v01,&v02);
D3DXVec3Normalize(&vN,&vN);
  
is there anything wrong with this?!?! The reason I bring this up is because my lighting (in C++ only, under VB6/7 its fine) is a bit dull. If I use the NORMALIZENORMALS (makes all normals 1.0f in length) state then the lighting looks correct. Jack DirectX 4 VB: All you need for multimedia programming in Visual Basic Formula 1 Championship Manager, My Game Project.

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

That would find the normal for the polygon, but if the vertex is joined to multiple polygons, then each vertex would have multiple normals, which you need to average out if you want smooth shading.
Advertisement
hi,

I have implemented smoothing groups where I average normals for all relevent triangles.

so, bottom line is that the above code is correct?

Jack

DirectX 4 VB: All you need for multimedia programming in Visual Basic
Formula 1 Championship Manager, My Game Project.

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement