PDA

View Full Version : Trouble getting vertex normals


HalfVector
01-13-2006, 09:49 AM
Hi.

I want to add normals to my geometry exporter taking into account the smoothing groups. I thought it would be as easy as this (just a sample code):

for i in 1 to msh.numVerts do local n = getNormal msh i

but I'm having unexpected normals.

For example, I've created a cube and deleted all faces but two. Then I've smooth normals with 90 and finally I've executed a script that creates a tape object for every normal to visualize them. This is the result:

[img=http://img206.imageshack.us/img206/2745/normals8sy.th.jpg] (http://img206.imageshack.us/my.php?image=normals8sy.jpg)

The blue lines are the normals drawn by the Edit_Normals (they are correct) modifier and the green lines are the tapers I've created. As you can see, one of the normals doesn't match with the "MAX normal".

So the question is, Do I have to do something more to get the correct normals?.

Thanks.

Bobo
01-13-2006, 09:27 PM
Hi.

I want to add normals to my geometry exporter taking into account the smoothing groups. I thought it would be as easy as this (just a sample code):

for i in 1 to msh.numVerts do local n = getNormal msh i

but I'm having unexpected normals.

For example, I've created a cube and deleted all faces but two. Then I've smooth normals with 90 and finally I've executed a script that creates a tape object for every normal to visualize them. This is the result:

[img=http://img206.imageshack.us/img206/2745/normals8sy.th.jpg] (http://img206.imageshack.us/my.php?image=normals8sy.jpg)

The blue lines are the normals drawn by the Edit_Normals (they are correct) modifier and the green lines are the tapers I've created. As you can see, one of the normals doesn't match with the "MAX normal".

So the question is, Do I have to do something more to get the correct normals?.

Thanks.

The trouble is that the Edit_Normals modifier turns the incoming mesh into PolyMesh internally and then shows the correct normals. The EditableMesh code in the SDK for calculating vertex normals used to have a bug until Max 5 (notice the "Use Legacy R4 Normals" in the Customize>Preferences>General tab). I *assume* that the MAXScript vertex normals return the same wrong results as Max 1,2,3 and 4.

Basically, if you flip the invisible edges of the "polygons" in the editable mesh in your example, the wrong normal will move to the other vertex...
The problem, if I remember correctly, was somehow related to how much influence a face has on the vertex based on its area vs. its angle at the vertex. Of course, you could calculate the vertex normal from the adjacent faces' normals using the smoothing groups yourself. But it is probably better to temporarily drop an Edit_Normals modifier and read the correct data from there.

HalfVector
01-13-2006, 10:30 PM
Of course, you could calculate the vertex normal from the adjacent faces' normals using the smoothing groups yourself.
Yeah, that's exactly what I've done. It's a bit slow but works fine.

But it is probably better to temporarily drop an Edit_Normals modifier and read the correct data from there.
I've taken a look at that and effectively I can access to the correct normals (getNormal function), but I don't know how to access to the indices that make up the normal faces. I mean, in the object I've shown in the image above, I have 8 vertices and 12 faces while the Edit_Normals modifier gives me 6 normals but I can't find any function to get the normal faces (as triangles).

Thanks much.

Bobo
01-13-2006, 10:51 PM
Yeah, that's exactly what I've done. It's a bit slow but works fine.


I've taken a look at that and effectively I can access to the correct normals (getNormal function), but I don't know how to access to the indices that make up the normal faces. I mean, in the object I've shown in the image above, I have 8 vertices and 12 faces while the Edit_Normals modifier gives me 6 normals but I can't find any function to get the normal faces (as triangles).

Thanks much.

There are no triangles in the Edit_Normals data structure.

Here is a piece of code from a script I wrote a while back:


theEditNormalsModifier = Knots_Reference_Object.modifiers[1] --get the modifier
theNumFaces = theEditNormalsModifier.getNumFaces() --get the faces in the modifier
for f = 1 to theNumFaces do --loop through the faces in the modifier
(
for i = 1 to theEditNormalsModifier.getFaceDegree f do --get the vertex number of the face f and loop
(
theNormalId = theEditNormalsModifier.GetNormalID f i --get the ID of the vertex normal by index (f,i)
theVertexId = theEditNormalsModifier.GetVertexID f i --get the vertex ID
theNormal = theEditNormalsModifier.GetNormal theNormalId --get the normal by ID
theVertex = theEditNormalsModifier.GetVertex theVertexId --get the vertex by ID
)--end i
)--end f




So basically you take polygon F, ask for the degree (number of vertices) in it and get the IDs of each normal and vertex. Using the IDs, you can get the actual normal and vertex data.

The main similarity between TriMesh and PolyMesh is that in general, the vertex indices are the same. (When working with an EditablePoly, be sure to run a polyOp.collapseDeadStructs first).
So if you ask the Poly object about the vertices used by the first polygon, you should get the same vertex indices as in the TriMesh.

This should give you the correspondence between the normals from Edit_Normals and the TriMesh vertices.

HalfVector
01-13-2006, 11:12 PM
I'll try that, thank you very much.

CGTalk Moderation
01-13-2006, 11:12 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.