Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

OpenGL tangent vector calculate, is this correct?

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

after alot of hardwork.. ive finally come up with some code for calcualting tangents for my terrain mesh... id rly aprecieate if someone took alook at it and told me if its right or not... thx im suing the default coordinate system of opengl.. cant remember if its right ahnded or left handed
void CTERRAIN::CalculateTangents( )
{
	float s1;
	float s2;

	float t1;
	float t2;

	float det;

	CVECTOR A, B, C, D, E;
	CVECTOR CA, CB, CD, CE;
	CVECTOR AverageTangent; 
	CVECTOR TangentACB,TangentBCE,TangentECD,TangentDCA;
	
	for (int z = 1; z < m_iSize; z++)
	{
		for (int x = 1; x < m_iSize; x++)
		{	
			A.Set( float(x  ),GetHeight(x, z+1), float(z+1) ); 
			B.Set( float(x-1),GetHeight(x-1, z) , float(z  ) ); 
			C.Set( float(x  ),GetHeight(x, z) , float(z  ) ); 
			D.Set( float(x+1),GetHeight(x+1, z) , float(z  ) ); 
			E.Set( float(x  ),GetHeight(x, z-1), float(z-1) ); 
			
			CA = A - C;
			CB = B - C;
			CD = D - C;
			CE = E - C;

			/////////////////////////////////////////////

			s1 = A.x/m_iSize - C.x/m_iSize; // tex coords
			s2 = B.x/m_iSize - C.x/m_iSize;

			t1 = A.z/m_iSize - C.z/m_iSize;
			t2 = B.z/m_iSize - C.z/m_iSize;

			det = 1.f / (s1 * t2 - t1 * s2);

			TangentACB = (det * ((CA*t2) - (CB*t1)));

                        /////////////////////////////////////////////
                        /////////////////////////////////////////////

			s1 = B.x/m_iSize - C.x/m_iSize;
			s2 = E.x/m_iSize - C.x/m_iSize;

			t1 = B.z/m_iSize - C.z/m_iSize;
			t2 = E.z/m_iSize - C.z/m_iSize;

			det = 1.f / (s1 * t2 - t1 * s2);

			TangentBCE = (det * ((CB*t2) - (CE*t1)));

                        /////////////////////////////////////////////
                        /////////////////////////////////////////////

			s1 = E.x/m_iSize - C.x/m_iSize;
			s2 = D.x/m_iSize - C.x/m_iSize;

			t1 = E.z/m_iSize - C.z/m_iSize;
			t2 = D.z/m_iSize - C.z/m_iSize;

			det = 1.f / (s1 * t2 - t1 * s2);

			TangentECD = (det * ((CE*t2) - (CD*t1)));

                        /////////////////////////////////////////////
                        /////////////////////////////////////////////

			s1 = D.x/m_iSize - C.x/m_iSize;
			s2 = A.x/m_iSize - C.x/m_iSize;

			t1 = D.z/m_iSize - C.z/m_iSize;
			t2 = A.z/m_iSize - C.z/m_iSize;

			det = 1.f / (s1 * t2 - t1 * s2);

			TangentDCA = (det * ((CD*t2) - (CA*t1)));

                        /////////////////////////////////////////////


			AverageTangent = (TangentACB + TangentBCE + TangentECD + TangentDCA)*0.25f;
	
			AverageTangent.Normalize();

			TangentData[(x+z*m_iSize)*4+0] = 0.5f*(AverageTangent.x+1.0f);
			TangentData[(x+z*m_iSize)*4+1] = 0.5f*(AverageTangent.y+1.0f);
			TangentData[(x+z*m_iSize)*4+2] = 0.5f*(AverageTangent.z+1.0f);
		}
	}
}


[Edited by - Dragon_Strike on October 7, 2006 12:15:28 PM]

Share this post


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