Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

OpenGL MD5 Model Tangent Problem

This topic is 3981 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

Im having problems with getting the right tangents for a MD5 model im loading into my OpenGL app.... this is how it looks: (the model in the pic are rotated -90 x and -90 z AFTER the tangents are calculated... ImageHost.org as u can see the tangents are (seem) correct on the left half and inverted on the other... the change seem to be at the texture seams... (the tangents in the pic at [-1,1] which explains the black parts) whats causing this?
	void MD5::MeshParser::CalculateNormals() 
	{

		size_t triCount = triangles_.size();
		for(size_t triIndex = 0; triIndex < triCount; ++triIndex) 
		{			

			vec3 Vertices[] = {vertices_[triangles_[triIndex].x].position,
							   vertices_[triangles_[triIndex].y].position,
							   vertices_[triangles_[triIndex].z].position};

			vec2 TexCoords[] ={vertices_[triangles_[triIndex].x].texCoord,
							   vertices_[triangles_[triIndex].y].texCoord,
							   vertices_[triangles_[triIndex].z].texCoord};
			
			vec3 Normal = FindNormal(Vertices);
			vec3 Tangent = FindTangent(Vertices, TexCoords);

		  	vertices_[triangles_[triIndex].x].normal += Normal;
			vertices_[triangles_[triIndex].y].normal += Normal;
			vertices_[triangles_[triIndex].z].normal += Normal;

			vertices_[triangles_[triIndex].x].tangent += Tangent;
			vertices_[triangles_[triIndex].y].tangent += Tangent;
			vertices_[triangles_[triIndex].z].tangent += Tangent;			
		}

		for(size_t Index = 0; Index < triangles_.size(); ++Index) 
		{
			vertices_[triangles_[Index].x].normal.Normalize();
			vertices_[triangles_[Index].y].normal.Normalize(); 
			vertices_[triangles_[Index].z].normal.Normalize();

			vertices_[triangles_[Index].x].tangent.Normalize();
			vertices_[triangles_[Index].y].tangent.Normalize(); 
			vertices_[triangles_[Index].z].tangent.Normalize();			
		}
	}

vec3 FindNormal(const vec3* Vertices)
{
   vec3 v2v1 = Vertices[0] - Vertices[2];
   vec3 v3v1 = Vertices[1] - Vertices[2];
   vec3 Normal = v3v1.CrossProduct(v2v1);
   return Normal.Normalize();
}

vec3 FindTangent(const vec3* Vertices, const vec2* TexCoords)
{
	vec3 Tangent;
	vec3 v2v1 = Vertices[0] - Vertices[2];
    vec3 v3v1 = Vertices[1] - Vertices[2];

	GLfloat c2c1_T = TexCoords[0].x - TexCoords[2].x;
	GLfloat c2c1_B = TexCoords[0].y - TexCoords[2].y;

	GLfloat c3c1_T = TexCoords[1].x - TexCoords[2].x;
	GLfloat c3c1_B = TexCoords[1].y - TexCoords[2].y;

	GLfloat fDenominator = c2c1_T * c3c1_B - c3c1_T * c2c1_B;  

	if (fDenominator > -0.001f && fDenominator < 0.001f) 
	{                      
		   Tangent = vec3(1.0f, 0.0f, 0.0f);	
	}
	else
	{
		GLfloat fScale1 = 1.0f / fDenominator;

		Tangent = (v2v1 * c3c1_B - v3v1 * c2c1_B) * fScale1;
	}
	Tangent.Normalize();


	return Tangent;
}






[Edited by - Dragon_Strike on January 16, 2008 10:32:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
screwed up matrix concatenation or a mirrored model. So the geometry is mirrored in the middle? I don't know if that works for a whole body but it is done for the head in the id software games.

Share this post


Link to post
Share on other sites
screwed up matrix concatenation or a mirrored model. So the geometry is mirrored in the middle? I don't know if that works for a whole body but it is done for the head in the id software games.
So what you can do is generate the tangent or bi-normal (bi-tangent ... in case TomF is around) differently.

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!