Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with Bump Mapping

Started by 51mon Jul 26, 2007 at 6:02 AM 2 replies 600+ views
Original Post
51mon
51mon
Hi I got problems with bump mapping and would be very pleased if someone could help me. Here is a screenshot from what my rendering looks like. As you can see the normals is opposite in left and right side and washed out in the middle. My shader looks like this.

struct TEST_VS_OUT
{
	float4 Position : SV_Position;
	float3 WorldNormal : NORMAL;
	float3 LightVector : TEXCOORD4;
	float2 TextureUV : TEXCOORD5;
};

TEST_VS_OUT RenderTestVS( float4 vPos : POSITION, float3 vNormal : NORMAL, float2 vTexCoord0 : TEXCOORD0)
{
	TEST_VS_OUT Output;
	
	Output.Position = mul( vPos, g_MatWorldViewProj_C);
	Output.WorldNormal = vNormal;
	
	Output.LightVector = g_vLightPosition - vPos.xyz;
	
	Output.TextureUV = vTexCoord0;
    
	return Output;    
}

float4 RenderTestPS( TEST_VS_OUT Input) : SV_Target0
{
	float4 vBump = g_BumpTexture.Sample( MeshTextureSampler, Input.TextureUV);
	vBump *= 2.0f;
	vBump -= float4( 1.0f, 1.0f, 1.0f, 0.0f);

	float3 Normal = normalize(Input.WorldNormal);
	float3 vUp = float3( 0.0f, 1.0f, 0.0f);
	if( abs(Normal.x) < abs(Normal.y))
		vUp = float3( 1.0f, 0.0f, 0.0f);
	float3 Tangent = cross( vUp, Normal);
	float3 Binormal = cross( Normal, Tangent);
	
	float3x3 BTNMatrix = float3x3( Binormal, Tangent, Normal);
	float3 TransBump = mul( vBump.xyz, BTNMatrix);
	float Shading = dot( TransBump, normalize(Input.LightVector));
	return Shading * g_MeshTexture.Sample( MeshTextureSampler, Input.TextureUV);
}


g_vLightPosition is given by g_Camera2.GetEyePt(). Does anybody have a clue of what’s wrong? I suspect that something has to be done with the light vector? Thanks
blakedev
blakedev
Many examples I see make a 3x3 matrix out with float3x3(tangent, binormal, normal).
In your shader you have the tangent and binormal backwards relative to them, but I'm not sure if that would make a difference. Try reversing them and see what happens.
51mon
51mon
Quote:
Original post by blakedev
Many examples I see make a 3x3 matrix out with float3x3(tangent, binormal, normal).
In your shader you have the tangent and binormal backwards relative to them, but I'm not sure if that would make a difference. Try reversing them and see what happens.


No that didn't work, the error just appeared in another form :(
remigius
remigius
I'm no math wiz, but I'm not sure you can just calculate your tangent and binormal that way. It might work for a number of cases, but it doesn't look like a valid approach for all tangent space calculations. In you screenshot it looks like you're kinda rendering a pretransformed quad, which is one case where your tangents & binormals would be off, since vUp points along the z-axis in this case.

I might be way off, but I don't see anything wrong with the other lighting calculations at first glance. You could always try and move the light around a bit in your scene, that might give you a better idea of how the lighting is working out. But I'd try and calculate a valid tangent frame using the D3D utility functions and using that in the shader first.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.