Jump to content
  • Advertisement
Sign in to follow this  
FreJa

problem with normals

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

If you intended to correct an error in the post then please contact us.

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 <_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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!