I'm trying to do bump mapping, and I'm getting fairly close at achieving my goal. I miss one crucial piece of my puzzle to have bump mapped effect in my scene; I need to transform the normals from my bump map to match the direction my faces are pointing.
This is my scene rendered with my vertex normals:
This is my scene rendered with the bump map:
The last image is wrong, because all samples from the bump map are rendered relative to as if they are pointing upforwards from the ground. What I need to do, is transform the samples from the bump map to be relative to the face normals.
Now, I have tried researching how to achieve this for a while - I've been told that if I create a TNB matrix, and multiply the sample with it - things should behave like I want. This is however not the behaviour I am getting.
This is my vertex shader:
uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;
uniform mat4 u_modelMatrix;
attribute vec3 a_position;
attribute vec2 aTextureCoord;
attribute vec3 aNormalCoord;
attribute vec3 aTangentCoord;
attribute vec3 aBitangentCoord;
varying vec2 vTextureCoord;
varying vec3 vNormalCoord;
varying vec3 vVertexCoord;
varying mat3 vTNBMatrix;
void main()
{
vec4 pos = vec4(a_position, 1.0);
vTNBMatrix = mat3(
aTangentCoord,
aNormalCoord,
aBitangentCoord
);
gl_Position = u_projectionMatrix * u_viewMatrix * pos;
vTextureCoord = aTextureCoord;
vNormalCoord = aNormalCoord;
vVertexCoord = a_position;
}
And this is my fragment shader:
varying vec2 vTextureCoord;
varying vec3 vNormalCoord;
varying vec3 vVertexCoord;
varying mat3 vTNBMatrix;
uniform sampler2D mytex;
uniform sampler2D mynormal;
void main()
{
vec3 nt = normalize(texture2D(mynormal, vTextureCoord.st).rgb * 2.0 - 1.0);
vec3 lightpos = vec3(15.0, 26.5, 0.5);
vec3 L = normalize(lightpos - vVertexCoord);
vec3 finalNormal = nt * vTNBMatrix;
vec3 ld = vec3(1.0, 1.0, 1.0) * max(dot(finalNormal, L), 0.0);
ld = clamp(ld, 0.0, 1.0);
gl_FragColor = vec4(texture2D(mytex, vTextureCoord.st).rgb * ld, 1.0);
}
Which are giving me the following, undesireable result:
Would anyone be so kind and give me some pointers at what I'm doing wrong?
Update:
These are the textures I'm using for this
Normal map:
Texture:
0, 0, 1
. But I haven't implemented normal mapping myself, so this is only my assumption. But the are many tutorial on the net. \$\endgroup\$