Jump to content
  • Advertisement
Sign in to follow this  

tangentmap calculations

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

ive done some code to calculate the tangents of a height map... but it doesn't seem right... anyone spot the error?
void CMESH::CalculateTangentMap()
{

	float tA,tB,tC,tD,tE;
	float tCA,tCB,tCD,tCE;

	float sA,sB,sC,sD,sE;
	float sCA,sCB,sCD,sCE;
	CVECTOR TangentACB,TangentBCE,TangentECD,TangentDCA;

	float det;

	CVECTOR A, B, C, D, E;
	CVECTOR CA, CB, CD, CE;

	CVECTOR AverageTangent; 

	
	for (int z = 1; z < m_iSize; z++)
	{
		for (int x = 1; x < m_iSize; x++)
		{	
			A.Set( float(x  ),GetHeight(x, z+1)*0.0025*0.5*2, float(z+1) ); 
			B.Set( float(x-1),GetHeight(x-1, z)*0.0025*0.5*2 , float(z  ) ); 
			C.Set( float(x  ),GetHeight(x, z)*0.0025*0.5*2 , float(z  ) ); 
			D.Set( float(x+1),GetHeight(x+1, z)*0.0025*0.5*2 , float(z  ) ); 
			E.Set( float(x  ),GetHeight(x, z-1)*0.0025*0.5*2, float(z-1) ); 
			
			CA = A - C;
			CB = B - C;
			CD = D - C;
			CE = E - C;

			tA = (z+1)/m_iSize;
			tB = (z)/m_iSize;
			tC = (z)/m_iSize;
			tD = (z)/m_iSize;
			tE = (z-1)/m_iSize;

			tCA = tA - tC;
			tCB = tB - tC;
			tCD = tD - tC;
			tCE = tE - tC;

			sA = (x)/m_iSize;
			sB = (x-1)/m_iSize;
			sC = (x)/m_iSize;
			sD = (x+1)/m_iSize;
			sE = (x)/m_iSize;

			sCA = sA - sC;
			sCB = sB - sC;
			sCD = sD - sC;
			sCE = sE - sC;

			TangentACB = CVECTOR(tCB*CA.x-tCA*CB.x, tCB*CA.y-tCA*CB.y, tCB*CA.z-tCA*CB.z)*1.0f/(sCA*tCB-sCB*tCA);
			TangentBCE = CVECTOR(tCE*CB.x-tCB*CE.x, tCE*CB.y-tCB*CE.y, tCE*CB.z-tCB*CE.z)*1.0f/(sCB*tCE-sCE*tCB);
			TangentECD = CVECTOR(tCD*CE.x-tCE*CD.x, tCD*CE.y-tCE*CD.y, tCD*CE.z-tCE*CD.z)*1.0f/(sCE*tCD-sCD*tCE);
			TangentDCA = CVECTOR(tCA*CD.x-tCD*CA.x, tCA*CD.y-tCD*CA.y, tCA*CD.z-tCD*CA.z)*1.0f/(sCD*tCA-sCA*tCD);


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

			AverageTangent.Normalize();
	
			TangentMap.SetColor(x,z,(unsigned char)((AverageTangent.x+1.0f)*0.5f*256),	(unsigned char)((AverageTangent.y+1.0f)*0.5f*256),	(unsigned char)((AverageTangent.z+1.0f)*0.5f*256));
		}
	}
}


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!