Vertex Normal Generation

Hello,
I realize this has probably been asked a hundred times before (I found the exact question at least once, but unfortunately the solution didn’t help me.) I have I function to generate facet normals, which works fabulously. I want to now create per-vertex normals, but that doesn’t work. All of the triangles get shaded wierd. The model looks like it is in camoflouge. It’s splotchy looking. I have pasted the relevant code below. It really didn’t show up well in this forum I don’t think, but hopefully that won’t matter. GL_SMOOTH is enabled, and the facenormals are calculated and working correctly. I’ve checked that the faces really do contain the vertex they are supposed to. I really don’t konw what else it could be, mathematically this is correct I am fairly certain. The drawing code is just a simple set of calls to glColor, glNormal, glVertex in that order. GL drawing mode is GL_TRIANGLES. Any help would be appreciated.

for (i = 0; i < Object->vertexcount; i++){
numfaces = 0;
tempnorm.x = 0.0;
tempnorm.y = 0.0;
tempnorm.z = 0.0;

for (i2 = 0; i2 < Object->facecount; i2++){
if (Object->vertexlist[i2].a == i | | Object->vertexlist[i2].b == i | | Object->vertexlist[i2].c == i){
numfaces++;
tempnorm.x += facenormals[i2].x;
tempnorm.y += facenormals[i2].y;
tempnorm.z += facenormals[i2].z;
}
}

tempnorm = i3dNormalize(tempnorm.x, tempnorm.y, tempnorm.z);
Object->vertices[i].normal = tempnorm;
}

a couple of examples http://nate.scuzzy.net/ http://www.xmission.com/%7Enate/smooth.html

Well, I looked at those (I had looked at the second one earlier.) and I appear to be doing exactly what they are. I’ve also looked at two other examples. The only difference is that some of them divide the sum of the facet normals by the number of facets, which I have tried with no luck. I’ve went over everything to insure that the data is correct and the normals are added to the correct vertex. I guess I’ll have a look over it all again.

Hi again, I have a rather strange update to make. I was just playing around with the code trying to see what would happen if I did various things. I decided to see what would happen if I only used one face normal, so I added “&& numfaces==0” to the if statement which checks for common faces. I ran the program expecting the flat shading look to be back, but it wasn’t it was smooth! I’ve tested with various numbers and 3 seems to be about the best. If I use 5 faces some edges are darker than they should be, and if I set it up to 15 the whole thing is camoflouge looking again. What’s going on here? Why should I limit the number of face normals (and to only one?!)? I’m thinking maybe that if 15 has an effect I should check and make sure that no false positives are being made, eh? I don’t really think that a significant enough amount of my triangles have 15 common faces for it to make a big difference. I’ll go back in check the data again, but if anyone else has any suggestions please share! :wink:

Just a guess on my part, but are you certain you want to use vertex normals for the mesh? Facet normals are more appropriate for meshes that have extended facets. Use vertex normals for ‘smooth’ meshes. Sounds like you may be using vertex normals when facet normals may be more appropriate. Of course I could be wrong since I have no idea of the shape of your mesh.

Yes, I need vertex normals for the mesh, because it has alot of round surfaces. I’ve gotten the code working now, almost. Apparently the problem is limited to the model I was loading. I loaded in a model of a bust of Mozart and noticed that I could see through him. I turned GlCulling of and that fixed the problem. When I loaded the original model in half of the polygons were dark, so I guess that was screwing with the normal generation and glCulling was hiding that fact somehow. Limiting the number of faces to below three managed to coincidentally block the screwed up polygons from getting in. (I’m guessing). I don’t know what was wrong with the model, but every other one I’ve tested works. I plan on adding an adjustable angle threshold so that objects which would look bad without edges can still have their edges.