View Full Version : glNormal - Surface/Vertex normals
Jones
2006.07.17, 05:00 PM
When using glNormal3fv to define a surface normal, do I give the normalized cross product of two of the surfaces vertices (vectors) or something else? This confusion arose when I looked at an example for drawing complex models from triangles. They called glNormal3fv every vertex, and not once for the entire surfarce. I thought one normal would correspond to the entire model. If I do have to specify it for every vertex, would it be the same for each one?
Thanks!
TomorrowPlusX
2006.07.17, 05:09 PM
Generally you submit one normal for each vertex of a primitive ( such as a triangle, or quad ). Which is to say, one normal per-vertex.
I thought one normal would correspond to the entire model.
The normal refers to the normal at a point on a surface. Unless your model is a perfectly flat plane, it's likely to have more than one normal.
Jones
2006.07.17, 05:21 PM
Generally you submit one normal for each vertex of a primitive ( such as a triangle, or quad ). Which is to say, one normal per-vertex.
The normal refers to the normal at a point on a surface. Unless your model is a perfectly flat plane, it's likely to have more than one normal.
Actually, that was a mistype. What I meant was one entire surface**, not an entire model. :wacko:
Ok, but are those normals still derived from the normalized cross product of *any* two of the surfaces vertices? (In this case, triangle, strip-triangles and such.) Or should it be the two that form that corner?
I'll check what the Red-Book says about this again...
*reads*
Well, the example in the book is annoyingly messy-like. It shows normal calculation for an icosahedron where the vertices are pointed to by indices which make up the corners. And they form up to vectors and then normal-cross-product them to form one to pass to glNormal. For reference, this code is on page 90 of the Red Book (fourth edition).
Could anybody please give me an example of glNormal for drawing say a pyramid or cube?
Thanks!
On a slightly less related note:
Is it worth buying the next release of the red book (2.0)? I have version 1.4 (ed 4th).
OneSadCookie
2006.07.17, 05:36 PM
If you have a faceted model like a pyramid or a cube, then you'll have face normals. You could specify them each once for the face, but it's still best to specify them for each vertex.
If you have a smooth model like a sphere, then you'll have vertex normals.
Jones
2006.07.17, 07:44 PM
If you have a faceted model like a pyramid or a cube, then you'll have face normals. You could specify them each once for the face, but it's still best to specify them for each vertex.
If you have a smooth model like a sphere, then you'll have vertex normals.
What if it's smooth and faceted? Like a human body or something. I guess vertex normals then. So how does one calculated a vertex normal... is it different from a face normal?
OneSadCookie
2006.07.17, 08:13 PM
Imagine sticking a nail through a piece of wood, then running the piece of wood over the surface of your object. The direction the nail is the normal to the surface.
For a smooth surface, the nail will move in a nice curve as you move the wood around.
For a faceted surface, the nail will suddenly flick from one direction to another as the wood moves from face to face.
A human body is solidly in the "smooth" camp. A cylinder is an exampe of something with some smooth and some faceted. A cube is all facets.
Jones
2006.07.17, 09:52 PM
Imagine sticking a nail through a piece of wood, then running the piece of wood over the surface of your object. The direction the nail is the normal to the surface.
For a smooth surface, the nail will move in a nice curve as you move the wood around.
For a faceted surface, the nail will suddenly flick from one direction to another as the wood moves from face to face.
A human body is solidly in the "smooth" camp. A cylinder is an exampe of something with some smooth and some faceted. A cube is all facets.
I understand surface normals. I did NOT know about the difference for smooth and faceted objects. Thanks for clarifying that!
imikedaman
2006.07.17, 11:12 PM
If you want vertex normals for a model, you take the average of the face normals for each facet that shares that vertex. Pseudocode would be like this:
for each vertex
set a temporary vector to (0, 0, 0)
for each polygon
if one of the polygon's vertices is equal to the current vertex,
add that polygon's normal to the temporary vector
next
normalize the temporary vector
(no need to divide by the total polygons)
this is now the vertex normal
next
OneSadCookie
2006.07.18, 12:01 AM
You can get better results by taking into account the area of the triangle providing each normal into account.
You should never need to do this, though -- your modeling program should analytically determine the normals (eg. by differentiating a bezier patch), and you should load them from a file.
imikedaman
2006.07.18, 12:17 AM
You should never need to do this, though -- your modeling program should analytically determine the normals (eg. by differentiating a bezier patch), and you should load them from a file.
I'm sorta getting off topic here, but I wanted to point out that the 3DS file format doesn't store any normals, most likely to save space. It just stores the vertices for the polygons in a specific order to make it possible to generate the facet and vertex normals after loading in the model. Most other formats seem to save the normals though.
OneSadCookie
2006.07.18, 12:21 AM
Then the 3DS file format is broken, and you should use a sensible one ^_^
Jones
2006.07.18, 01:59 AM
But what if the format doesn't, and it's not a 3DS. It uses some form of pregenerated normal table, which used to be up for download, but now appears to be unatainable. So I'm re-writing the system. I'm making a program to generate the normals, from the model. Then... I'll make my *own* model format based of this one, but with the normals and all texture data built in. It will be... brilliant! ;)
Jones
2006.07.18, 02:01 AM
If you want vertex normals for a model, you take the average of the face normals for each facet that shares that vertex. Pseudocode would be like this:
for each vertex
set a temporary vector to (0, 0, 0)
for each polygon
if one of the polygon's vertices is equal to the current vertex,
add that polygon's normal to the temporary vector
next
normalize the temporary vector
(no need to divide by the total polygons)
this is now the vertex normal
next
Ah so just normalizing a vertice (directly) is silly, and I should slap myself for even thinking of trying that? :p
Thanks for the tip!
Fenris
2006.07.18, 02:42 AM
If you normalize the vertex itself, then you will get a /model/ that is of size 1. :)
OneSadCookie
2006.07.18, 05:11 AM
The only shape where the normals are normalized versions of the vertices is a sphere centered on the origin :p
The MD2 normal table is still available, under the GPL:
http://www.idsoftware.com/business/techdownloads/
Fenris
2006.07.18, 05:33 AM
The only shape where the normals are normalized versions of the vertices is a sphere centered on the origin
...that is of size 1, just as I said. :-P
Gosh darn, I'm out of whack. I need to get off this vacation quick! ;)
OneSadCookie
2006.07.18, 06:05 AM
Anyway, I lied... any spheroid will do, as long as it's centered on the origin :)
Jones
2006.07.18, 12:22 PM
But why would I want to use a precalculated table that may no even correspond to the model I have?
Silly... is it not? It would look better if I grabbed all the normals myself, with calculations and such. (Either generated into a file, or system memory.)
No?
Thanks for the link tho, I'll try it with this table thingy...
Ahh.. "anorms.h" must be what I'm looking for.
Jones
2006.07.18, 03:49 PM
A question regarding performance:
If I store all my interpolated normals and vertices in memory, would it really help performance? (So I don't have to calculate them again.)
A float is 4 bytes, correct? If it takes 3 floats per vertice, and the model has say... 1000 vertices, for... dunno, like 199 frames. It would be...
2 and some mb per model... * 2 for the normals too... 4 mb per model.
I think... :wacko:
imikedaman
2006.07.18, 04:45 PM
A float is 4 bytes, correct? If it takes 3 floats per vertice, and the model has say... 1000 vertices, for... dunno, like 199 frames. It would be...
2 and some mb per model... * 2 for the normals too... 4 mb per model.
4 bytes/float * 3 floats = 12 bytes per vertex
1000 vertices = 12,000 bytes = 11.7 kb
1000 vertices * 2 for normals = 23.4 kb
199 frames = 23.4 kb still (not really sure why the number of frames changes anything...?)
So instead of 4 MB, it's 23 kilobytes. Don't forget the polygons too, which would be three ints for each one for 12 bytes per polygon.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.