1

I'm trying to create a shader with bump-mapping.

I have the real normal of the object, and I have the bump-map sampled normal. What I would like to do is rotate the sampled normal, so that "up" on the sampled normal points in the direction of the real normal.

I've been working at the math and I can't seem to get it right... Y is up in this world.

// Vertex shader sends this matrix to the pixel shader
OUT.normal[0] = mul((float3x3)world, normal);
float3 temptan = cross(normal, float3(0, 0, 1));
temptan = normalize(temptan);
OUT.normal[2] = mul((float3x3)world, temptan);
OUT.normal[1] = cross(OUT.normal[0], OUT.normal[2]); calculating binormal (in world space)

// Pixel Shader:
// Get normal from bump texture
float3 normal = tex2D(normalmap, texCoord);
normal = normal * 2 - 1;
// Swap Z and Y, since Z is up on the texture, but Y is up in this world.
float temp = normal[1];
normal[1] = -normal[2];
normal[2] = temp;
// Multiply the sampled normal and the normal-basis matrix together.
normal = mul(normalMat, normal);
Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
yellow
  • 463
  • 1
  • 6
  • 16

1 Answers1

5

When you want to use normal mapping (this is what you actually mean by "bump mapping") you need two additional vectors for what you intend to do: The tangent and the binormal. Tangent and binormal are calculated from the geometry normals and the texture space of the "bump map".

I did explain how to do this in →my answer to StackOverflow question "How to calculate Tangent and Binormal?"

Normal, tangent and binormal form a coordinate system, expressible as a 3×3 matrix. This matrix is actually the rotation matrix required to transform the normal map into model local space, so that you can use it for illumination calculations. In short you multiply with the matrix formed by the tripod of those three vectors.

Community
  • 1
  • 1
datenwolf
  • 149,702
  • 12
  • 167
  • 273
  • Thanks! I'll read over your article. I think I've been trying to do what you've said above, but it hasn't been entirely clear to me. – yellow Apr 07 '13 at 03:18
  • Note that this is only the case if you're talking about tangentspace normalmaps, if you have your normals in world or objectspace, this is not needed. – Robert J. Apr 25 '13 at 15:36
  • @RobertJ.: World space normals are very unusual and object space normals create tons of issues as soon as you use skeletal animation. But you're right of course. – datenwolf Apr 25 '13 at 16:32