Jump to content
  • Advertisement
Sign in to follow this  

Verification of create tangent code

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

Hi all, I have been working on shaders recently, and am trying to get normal mapping to work in a DX app. I've got a pretty basic shader that I've been using to implement the effect, but without much luck - so to see if it is the shader, or my app causing the problem, I have ported it in to nVidia's FX Composer, where I've got it working fine. I can only assume therefore, that the problem is down to what I'm inputting in to the vertex shader, and the most likely problem therefore is my Tangent generating code (of which I have two versions, that produce different results, put together using resources from different websites, including the infamous Terathon). My app is simply putting a single Triangle on to the screen, pointing at a forward facing camera (so the triangle normal is (0,0,-1)). My Tangent code is as follows (the Terathon version):
/*Function takes triangle as 3 vertices, and the UV tex coords for each vertex (A,B,C)*/
D3DXVECTOR3 Tangent(D3DXVECTOR3 Tri[], float AU, float AV, float BU, float BV, float CU, float CV)
	D3DXVECTOR3 Tangent;
	D3DXVECTOR3 SideA = Tri[1] - Tri[0];
	D3DXVECTOR3 SideB = Tri[2] - Tri[0];

	float s1 = BU - AU;
	float s2 = CU - AU;
	float t1 = BV - AV;
	float t2 = CV - AV;

	float r = 1.0F / (s1 * t2 - s2 * t1);
	D3DXVECTOR3 sdir((t2 * SideA.x - t1 * SideB.x) * r, (t2 * SideA.y - t1 * SideB.y) * r, (t2 * SideA.z - t1 * SideB.z) * r);

	D3DXVECTOR3 tdir((s1 * SideB.x - s2 * SideA.x) * r, (s1 * SideB.y - s2 * SideA.y) * r, (s1 * SideB.z - s2 * SideA.z) * r);
	const D3DXVECTOR3& n = Normal(Tri);
	const D3DXVECTOR3& t = sdir;
	// Gram-Schmidt orthogonalize
	Tangent = Normalize( (t - n * Dot(n, t)) );

	return Tangent;

I would be extraordinarily grateful if someone could confirm that this is correct :) It generates a Tangent of (1,0,0) for a normal of (0,0,-1), with "standard" tex coords, 0,0 at top left, 1,1 at bottom right. Thanks in advance, Dach

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!