Normals Calculations [Pics]

Started by
5 comments, last by Prozak 19 years, 7 months ago
I've been trying to code a filter into my Mesh Loading functions, that detects when a resource was loaded, but has no Normals. If such a case occurs, this filter is responsable for generating those normals. Here is an example, with a teapot, the original: The Original Teapot Normals And this is my version, with an algorithm to calculate the normals: The Calculated Teapot Normals Something is obviously happening in the calculations that is skewing the normal vectors onto the Y Axis, for some reason... This is how I calculate the cross product, im afraid something may be wrong with it: res.x = (v1.y * v2.z) - (v1.z * v2.y); res.y = (v1.z * v2.x) - (v1.z * v2.z); res.z = (v1.x * v2.y) - (v1.y * v2.z); The pseudo-algorithm i use to calculate the normals is: + Calculate face normals for all faces + Create (Vn) vector, that will hold current vertice's normal + For all (V) Vertices, for all (F) Faces, if F uses V, then add F's normal to Vn + Normalize Vn. It now holds the normal for that vertice. So... what could I be doing wrong here?
Advertisement
res.x = (v1.y * v2.z) - (v1.z * v2.y);
res.y = (v1.z * v2.x) - (v1.z * v2.z);
res.z = (v1.x * v2.y) - (v1.y * v2.z);

should be:

res.x = (v1.y * v2.z) - (v1.z * v2.y);
res.y = (v1.z * v2.x) - (v1.x * v2.z); <-- changed
res.z = (v1.x * v2.y) - (v1.y * v2.x); <-- changed
Thank you for the quick reply rick_appleton.

I've changed the code, after reading your post, and doing some deep digging on some math books.

None the less the visual result remais the same, the vertex normals still point upwards...
Could you post the code where you loop through everything and calculate the normals?

It looks like the algorithm starts at the top, where the normals are calculated correctly, and then goes down to the bottom withouth clearing the results for each vertex. Therefore the previous normal (point up) keeps on getting used along with the correct normals.
Thank you rick_appleton. I was able to track down the bug in my algorithm.


In the above image you can see a teapot exported from 3D Studio Max.

It is being rendered with a shader that visually shows us the normals on the model. A visible "seam" runs across the handle area.

On the lower area of the image you have the same model, but with normals generated by the algorithm. No seams are visible [wink]

Rate+ Rick_appleton
So what was the error?

And thanks ;)
Nothing big... in fact it was so small it was well hidden.
I was just making a mistake when normalising the normal, which gave the "y" axis more "weight" than it should have...

Traced the error using a plane mesh, hahaha.

The normals calculations takes a good deal of time, but its all dumped to file afterwards anyways, so...

This topic is closed to new replies.

Advertisement