# Computing Normals

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

## Recommended Posts

I am creating a few line lists, line strips, and point lists. These do not require face normals but they do require vertex normals. When lighting is disabled, my lines do not appear without the normals. The thing is, I have no idea of the math behind normals. I read MSDN on calculating normals for faces, which is really easy. It's a perpendicular vector of the face. But the direction for vertex normals seems a bit unpredictable. I can't see a pattern. If someone could show me how to calculate normals for my vertices I would greatly appreciate it! Thank you!

##### Share on other sites
The normal calculations you see there do have a pattern...its the winding of them: the direction in which you define the vertices. Normally you define in a clockwise direction, and the cross product of the 3 vertices gives you the normal to the plane which the triangle defines. Cross products are like matrices: order is important. Just read the MSDN article again and look at the winding...it'll make more sense i hope.

##### Share on other sites
hi there MrDoomMaster,
How are you doing?

The Problem
Calculating the per vertex normals.

The Solution
What I do is normally lots of fun, I try and use the D3DX helper classes to do all the messy work for me. D3DXComputeNormals is such a class that will help you compute normals for each vertex in a mesh. So you might ask, a Mesh.. I have line lists, point lists, line strips.

1) What you do is you create a new ID3DXMesh object.
2) Lock it's vertex/index buffers
3) Use the D3DXComputeNormals on the mesh to calculate the per vertex normals.
4) Extract the data from the ID3DXMesh.

I hope this helps. Take care buddy.

##### Share on other sites
Thank you for your valuable replies everyone.

I think using ID3DXMesh is an excellent idea, yet I am a college student. Learning how to do this manually will benefit me greatly when I get into the industry, which is only in 4 months approximately.

I did go back over to that MSDN explanation of Face and Vertex Normals, and it didn't help at all. I'm just not seeing how the vertex normals are created. I don't see the pattern.

Can someone provide a tutorial for normals, something fairly easy to understand (not too wordy)? I would like to learn how to do the math :)

Thanks again everyone!

##### Share on other sites
I've never found a tutorial on the subject personally but this is some generic code I use (normally I work with OpenGL but can't see a reason why this shouldn't work for DirectX too)

struct vector surface_normal(float vertex1[3], float vertex2[3], float vertex3[3]){	struct vector surface_vector1, surface_vector2, surface_normal;	/* Calculate 2 vectors from the vertices provided */	surface_vector1.x = vertex1[0] - vertex2[0];	surface_vector1.y = vertex1[1] - vertex2[1];	surface_vector1.z = vertex1[2] - vertex2[2];	surface_vector2.x = vertex1[0] - vertex3[0];	surface_vector2.y = vertex1[1] - vertex3[1];	surface_vector2.z = vertex1[2] - vertex3[2];	/* Calculate cross product of vectors to give surface normal. Not normalised. */	surface_normal.x = (surface_vector1.y * surface_vector2.z) - (surface_vector1.z * surface_vector2.y);	surface_normal.y = (surface_vector1.z * surface_vector2.x) - (surface_vector1.x * surface_vector2.z);	surface_normal.z = (surface_vector1.x * surface_vector2.y) - (surface_vector1.y * surface_vector2.x);return surface_normal;

edit: I should really learn to read. This is normally (no pun intended) what I use for face normals though with good results, although you'll need to normalise it first.

You can specify this for each vertex on a polygon to get a flat-shaded looking normal (with hard edges).
If you're looking for smoother graduations between faces, here's how:
Taking a triangle A-B-C: to find the vertex normal for point A, find all the faces sharing this vertex and simply average the x, y and z components of the surface normals for this set of faces. Do the same for B and C. Hard edges can still be obtained if necessary by giving each face normal a weight according to angle or something.

[Edited by - DrewGreen on September 3, 2005 1:06:34 AM]

##### Share on other sites
Quote:
 Original post by MrDoomMasterI am creating a few line lists, line strips, and point lists. These do not require face normals but they do require vertex normals. When lighting is disabled, my lines do not appear without the normals.

Something is wrong. If lighting is disabled, then there is no need for normals. When lighting is disabled you need to set the color for each vertex (setting the "diffuse" color in the vertex data). Here is a tutorial showing how to draw a point list: Drunken Hyena: DirectX9 Tutorials - C++ - Points

##### Share on other sites
Quote:
Original post by JohnBolton
Quote:
 Original post by MrDoomMasterI am creating a few line lists, line strips, and point lists. These do not require face normals but they do require vertex normals. When lighting is disabled, my lines do not appear without the normals.

Something is wrong. If lighting is disabled, then there is no need for normals. When lighting is disabled you need to set the color for each vertex (setting the "diffuse" color in the vertex data). Here is a tutorial showing how to draw a point list: Drunken Hyena: DirectX9 Tutorials - C++ - Points

Lighting is ENABLED though. The lines appear perfectly fine when I DISABLE the lighting.

The thing is, I'm drawing a "grid" for each axis, so the lines should appear regardless of lighting or not. On top of this grid I will have many meshes which take advantage of lighting.

The lines do NOT appear when the lights are enabled. This is my problem. The lines are NOT in triangle form, since they are line strips. So figuring out how to calculate normals for individual vertices that have no actual form (such as a triangle) is unknown to me.

##### Share on other sites
Turn off lighting before you draw the grid. Turn it back on when you are done.

##### Share on other sites
Well I think it would be cool for the grid to appear when lighting comes in range. For example, if you have a POINT light that is slowly moving across the grid, the lines will slowly appear and disappear as they come in range of the light.

##### Share on other sites
Since you are drawing a grid, compute the vertex normals the same way you would if you were drawing a polygon mesh. That is done by adding the normals of all the faces associated with the vertex and then normalizing the result. Assuming you are drawing a flat grid, the vertex normal is simply the normal of the plane of the grid.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009662
• ### Who's Online (See full list)

There are no registered users currently online

×