# Normals Calculations [Pics]

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

## Recommended Posts

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: And this is my version, with an algorithm to calculate the 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?

##### Share on other sites
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

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
So what was the error?

And thanks ;)

##### Share on other sites
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...

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46