problem with normals

Started by
1 comment, last by FreJa 15 years, 5 months ago
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 <_vn.size(); j++)
		{
			_vn[j][0] = 0;
			_vn[j][1] = 0;
			_vn[j][2] = 0;
		}
		
		for (int j = 0; j < _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 < _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!
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Advertisement
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.
That's how you get Flat-Shading.
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
Solved. Discovered the 'Set smooth' option in blender and normals are being exported fine
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."

This topic is closed to new replies.

Advertisement