Jump to content
  • Advertisement
Sign in to follow this  
Caste

Problems with tangent and bitangent

This topic is 4307 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 implemented normalmapping into my engine yesterdy and I at first didn't use the handedness calculation because I thought I wouldn't need it. Then I had my cube look like that: As you see, the two triangles representing a quad are lighted differently. That is where handedness comes in, so I added it and: it doesn't change anything.. Can anyone tell me where the error is? Vertex shader
varying vec3 halfAngle;
varying vec3 lightDirection;

attribute vec4 tangent;

void main(void)
{
	vec3 normal=gl_Normal;
	vec2 texcoord=gl_MultiTexCoord0.st;

	vec4 position=gl_ModelViewMatrix * gl_Vertex;
	normal=gl_NormalMatrix*normal;
	
	vec3 tang=gl_NormalMatrix*tangent.xyz;
	vec3 bitangent=cross(normal,tang)*tangent.w;//use handedness

	mat3 TBN_Matrix=mat3(tang,bitangent,normal);

	vec3 lightPosition=gl_LightSource[0].position.xyz;
	vec3 eyeDirection=TBN_Matrix*normalize(-position.xyz);
	lightDirection=TBN_Matrix*normalize(lightPosition-position.xyz);
	halfAngle=normalize(eyeDirection+lightDirection);

	gl_TexCoord[0].st=texcoord;
	gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
}

Frgment shader
varying vec3 halfAngle;
varying vec3 lightDirection;

uniform sampler2D tex;
uniform sampler2D bump;

uniform float ambient;

void main(void)
{
	vec3 normal=texture2D(bump,gl_TexCoord[0].st).xyz;
	normal=vec3(2.0)*(normal-vec3(0.5));
	
	float diffuse=smoothstep(0.0,1.0,dot(normal,lightDirection));
	float specular=smoothstep(0.0,1.0,dot(normal,halfAngle));

	vec4 diffuseColor=texture2D(tex, gl_TexCoord[0].st);

	gl_FragColor=(ambient+diffuse)*diffuseColor;
}

and here's where I generate my tangents:
for(int x=0;x<mc_vertices.size();x+=3)
	{
		//Texcoord and Vector have overlaoded "-" operator
		CTexCoord u12=mc_texcoords[x+1]-mc_texcoords[x];
		CTexCoord u13=mc_texcoords[x+2]-mc_texcoords[x];

		CVector v12=mc_vertices[x+1]-mc_vertices[x];
		CVector v13=mc_vertices[x+2]-mc_vertices[x];

		float det_inv=1.0f / (u12.s * u13.t - u13.s * u12.t);

		CVector p=det_inv*(u13.t * v12 - u12.t * v13);
		p.Normalize();//bitangent
		CVector q=det_inv*(u13.s * v13 - u12.s * v12);
		q.Normalize();//tangent
		
		float w=1.0f;
		//Handness calculation
                //if(dotproduct(bitangent,crossproduct(tangent,normal))<0.0)
		if(p.dot(q*mc_normals[x])<0.0)
			w=-1.0f;
		
		CVector4 tangent(q-(mc_normals[x]*mc_normals[x].dot(q)),w);

		//store for each vertex(nothing shared!)
		mc_tangents.push_back(tangent);
		mc_tangents.push_back(tangent);
		mc_tangents.push_back(tangent);
		m_useTangents=true;
	}

And then I render everything with vertex arrays and the tangent with vertex attribute arrays... I tried to invert tangent, bitangent and everything before, Irecalculated the normals and used the one I loaded from the .obj model and the output looks alike...?

Share this post


Link to post
Share on other sites
Advertisement
are u sure youre generating the tangents etc correctly?
check out eric lenygels code
also your vertex shader looks over complicated
i believe its better to use texture coordinates to send the tangent/binormal/normal + not gl_Normal etc

Share this post


Link to post
Share on other sites
note that I had a real hard time with this as well and never quite understood it all deeply, but my question is, how are you storing yer normals?

I see you decompress them with:
normal=vec3(2.0)*(normal-vec3(0.5));

but how are they compressed or stored?

Sorry about the edits, I noticed I started to make no sense... anywho I compress with (0.5 * normal + 0.5) and then decompress with (2 * normal - 1.0) it works for me...


[Edited by - xsirxx on January 30, 2007 2:35:11 PM]

Share this post


Link to post
Share on other sites
Another thing, how are you getting:
vec3 eyeDirection=TBN_Matrix*normalize(-position.xyz);

What is position? are you using eye - world?

Share this post


Link to post
Share on other sites
@xsirxx:
I'm storing the normals in a normal map generated with gimp-normalmap-plugin and AFAIK I have to extract them this way, but it's a good hint, I'll try to figure out..

position in the vertex shader was defined before:vec4 position=gl_ModelViewMatrix * gl_Vertex;
so this should work..

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!