Sign in to follow this  

bump mapping

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

righty, i've just got shaders working recently in my engine, so now im trying my hand at doing a bump mapping shader. the problem i have is this: http://users.tpg.com.au/thetwins/fab/bump.JPG L - shows the light position in my scene. A is the vertex lets say (-1, -1), B is (1,1) i'v left out the z-axis, to make things simpler. so left positive x-axis is left, my positive y-axis is down. now, here is my shader code:
struct VS_INPUT
{
    float3 position 	: POSITION;
	float3 normal 		: NORMAL;
	float3 tangent 		: TEXCOORD0;
    float3 binormal 	: TEXCOORD1;
	float2 uvBump		: TEXCOORD2;	
};

struct VS_OUTPUT
{
	float4 position : POSITION;
	float2 uvBump	: TEXCOORD0;	
	float3 lightVec : TEXCOORD1;
	float3 eyeVec 	: TEXCOORD2;
	float3 normal 	: TEXCOORD3;
};

struct PS_OUTPUT
{
	float4 colour	: COLOR;
};

//-----------------------------------------------------------------------------
// Vertex Shader
//-----------------------------------------------------------------------------

VS_OUTPUT myvs( const VS_INPUT IN,
			uniform float4x4 modelViewProjection,
			uniform float4x4 worldView,
			uniform float4x4 worldInv,
			uniform float3   lightPos,
			uniform float3   eyePos )
{
    VS_OUTPUT OUT;
   	OUT.position = mul( modelViewProjection, float4(IN.position, 1.0) );
	OUT.uvBump = IN.uvBump;
	OUT.normal = IN.normal;

	// convert from object space to texture(tangent) space...
	float3x3 texSpaceTrans = float3x3(IN.tangent,
					IN.binormal,
					IN.normal);
					
	float3 lightModelPos = mul( worldInv, float4(lightPos, 1.0) ).xyz;
	lightModelPos = lightModelPos - IN.position;
	OUT.lightVec = mul( texSpaceTrans, lightModelPos );
	
	float3 eyeModelPos = mul( worldInv, float4(eyePos, 1.0) ).xyz;
	eyeModelPos = eyeModelPos - IN.position;
	OUT.eyeVec = mul( texSpaceTrans, eyeModelPos );
	
	return OUT;
}

//-----------------------------------------------------------------------------
// Pixel Shader
//-----------------------------------------------------------------------------

PS_OUTPUT myps( VS_OUTPUT IN )
{
	PS_OUTPUT OUT;

	// normals in bump map are in texture(tangent) space....
	float3 bumpTexNormal = tex2D( BumpSampler, IN.uvBump );

	// at this stage, should be in tangent(texture) space
	float3 eyeVec = normalize( IN.eyeVec );
	float3 lightVec = normalize( IN.lightVec );

	//OUT.colour = float4(bumpTexNormal, 1.0);
	//OUT.colour = float4(lightVec, 1.0);
	
	OUT.colour = max( dot(bumpTexNormal, lightVec), 0.0);

	return OUT;
}


note: eye position and ligh position are passed in in world space. it appears that my tangent space light position is all correct, as in the Diagram at the point L is a line comming out, which shows the light position in world space, im not quite sure what the problem is, it seem related to the diagonal line through along the vector (1, -1) :-/ any ideas what i may be doing wrong?

Share this post


Link to post
Share on other sites
right seems my problem is that a normal comming strait out has an RGB colour of (128, 128, 128) so i somehow have to offset this to become (0,0,1) :-/

edit: hrmm actually, i tried fixing the normal value to be 0,0,1 and it was all black. so im confused now .... but when i change the normal to (0.5, 0.5, 0.5) i get roughly the same effect/problem i have with my normal map texture

edit2: okay i had to move my light out a bit from the surface *ops*
also i found this which seems to do the trick:

bumpTexNormal = 2 * (bumpTexNormal - 0.5);

but im not sure why?

[Edited by - supagu on May 15, 2005 4:33:26 AM]

Share this post


Link to post
Share on other sites
It's simple: Normal maps are color textures that encode normals in their RGB channels. Each channel ranges from [0,1]. But of course each normal coordinate ranges from [-1,1]. When creating a normalmap, you "compress" the normal coordinates by using Nmap=(Norm*0.5+0.5). When reading values of the normalmap, you use Norm=(NMap*2.0-1.0) to expand them into [-1,1].

So, assuming your normalmaps are encoded correctly, it should be:
float3 bumpTexNormal = tex2D( BumpSampler, IN.uvBump )*2.0-1.0;


Share this post


Link to post
Share on other sites

This topic is 4600 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this