Jump to content
  • Advertisement
Sign in to follow this  

Bump Mapping with HLSL

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

Hello everybody! Recently I've tried to make pixel and vertex shaders to apply bump mapping ( normal map approach like in Dot3Product Sampler in DX SDK ), but thay do not work correctly: VertexShader - Just get Transform Matrix and TransViewProj Matrix so like this matrix TVP_mat; matrix T_mat; vector lightpos; struct VS_INPUT { vector pos : POSITION; vector normal : NORMAL; float2 tex : TEXCOORD0; }; struct VS_OUTPUT { vector position : POSITION; vector light : COLOR0;// like TFACTOR in Fixed Pipeline float2 tex1 : TEXCOORD0; }; VS_OUTPUT Main(VS_INPUT input) { VS_OUTPUT output = (VS_OUTPUT)0; output.position = mul(input.pos,TVP_mat); output.tex1 = input.tex; // Try to compute tangent space operator float3 normal = mul( input.normal , T_mat ); float3 binormal = cross( float3(1,0,0), normal ); float3 tang = cross( normal , binormal ); float3x3 t; t[0] = tang; t[1] = binormal; t[2] = normal; // Move light direction in tangent space float3 light = mul( lightpos - input.pos, t ); // Packed result light direction in color representation output.light.xyz = light*0.5+0.5; return output; } That I try to use next pixel shader: sampler color_tex; sampler normal_tex; struct PS_INPUT { float2 b1: TEXCOORD0; vector light:COLOR0; }; struct PS_OUTPUT { vector col : COLOR0; }; PS_OUTPUT Main(PS_INPUT input) { PS_OUTPUT output = (PS_OUTPUT)0; // pv 1_4 I can use input.b1 twice vector col = tex2D( color_tex , input.b1 ); vector normal = tex2D( normal_tex , input.b1 ); normal = ( normal - 0.5 )*2; float3 lf = ( input.light - 0.5 )*2; float light = dot( normal ,lf); output.col = col*light; return output; } // To make normal map I use D3DXComputeNormalMap

Share this post

Link to post
Share on other sites
Some possible problems, gotchas, and/or things to improve:
- Make sure lightpos is given in the model's object space and not in world space (not a problem if your object has an identity world matrix, but if it doesn't...)
- Your tangent space light vector in your vertex shader should be in the [-1,1] range before [0,1] range compression. If your tangent space vectors are all normalized, then you can either normalize(lightPos-input.Pos) or just normalize the result, light
- Depending on how you've got your T/B/N vectors defined, you might want to do:
float3 objSpaceLightDir = lightPos - input.pos;
light.x = dot(objSpaceLightDir,Tangent);
light.y = dot(objSpaceLightDir,Binormal);
light.z = dot(objSpaceLightDir,Normal);
Instead of the matrix version
- By using COLOR0 to pass the light vector, it's clamped to the [0,1] range. Passing it as a TEXCOORD would allow it to be in the range +/- MaxTextureRepeat, but this really shouldn't be a problem if the [0,1] clamping doesn't chop anything off.
- Your per-vertex tangent/binormal should probably be provided by the program (calculated based on the actual geometry) for correctness.
- In the pixel shader, you use the decompressed light vector directly. Assuming it were normalized per vertex, it becomes denormalized as it's interpolated across the triangle. The larger the triangle, the more noticable the denormalization. This can be fixed using a normalization cubemap.

If I had to guess what the biggest problem was though it'd be the lack of normalization.

Good luck

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!