# problem with normals

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

## Recommended Posts

Hey, I'm simply loading a OBJ model, created in blender. This is what I get: Now, GL_SMOOTH IS enabled, but still the sphere looks as if GL_FLAT is on. So, this can only be a normal calculation problem. The above sphere is using the normals generated by blender. However, using the code below, I get the same result:
void Model::buildNormals()
{
for (int j = 0; j &lt;_vn.size(); j++)
{
_vn[j][0] = 0;
_vn[j][1] = 0;
_vn[j][2] = 0;
}

for (int j = 0; j &lt; _numfaces; j++)
{
face &f = _faces[j];

Vector3f &v0 = _v[f.v[0]];
Vector3f &v1 = _v[f.v[1]];
Vector3f &v2 = _v[f.v[2]];

Vector3f &vn0 = _vn[f.vn[0]];
Vector3f &vn1 = _vn[f.vn[1]];
Vector3f &vn2 = _vn[f.vn[2]];

float Ax = v1[0] - v0[0];
float Ay = v1[1] - v0[1];
float Az = v1[2] - v0[2];

float Bx = v2[0] - v0[0];
float By = v2[1] - v0[1];
float Bz = v2[2] - v0[2];

float nx = Ay * Bz - By * Az;
float ny = -(Ax * Bz - Bx * Az);
float nz = Ax * By - Bx * Ay;

vn0[0] += nx;
vn0[1] += ny;
vn0[2] += nz;

vn1[0] += nx;
vn1[1] += ny;
vn1[2] += nz;

vn2[0] += nx;
vn2[1] += ny;
vn2[2] += nz;
}

for (int j = 0; j &lt; _vn.size(); j++)
{
Vector3f &vn = _vn[j];

float len = float(sqrt(vn[0] * vn[0] + vn[1] * vn[1] + vn[2] * vn[2]));

if (len)
{
vn[0] /= len;
vn[1] /= len;
vn[2] /= len;
}
}
}


Any help? Huge thanks!

##### Share on other sites
Without looking at your code I guess that the problem is you're calculating each Faces normal and then applying it to the vertices in that face.
In order to get a Smooth Shading you'll need to do an avarage of normals surrounding a vertex

Assuming you have a Vertex that is located in 4 faces

you compute each normal of those faces , you sum them up and then do an avarage:

(each n is a normal of a nearby face of a vertex)
n1,n2,n3,n4

VertexNormal=(n1+n2+n3+n4)/4.0f;

I hope This Helped

##### Share on other sites
Solved. Discovered the 'Set smooth' option in blender and normals are being exported fine

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• ### Who's Online (See full list)

There are no registered users currently online

×