Sign in to follow this  
Hulag

Bumpmapping GLSL shader.

Recommended Posts

Can someone help me develop a bumpmapping shader? The information I have available to pass to the shader is the following: 1. Position of the light. (Multiplied by any matrix if needed) 2. Position of the camera. (Multiplied by any matrix if needed) 3. Texture unit 0 as base map. 4. Texture unit 1 as normal map. 5. gl_MultiTexCoord2 for the tangents. 6. gl_MultiTexCoord3 for the bitangents. Now, here is my best attempt but it doesn't work. Vertex shader:
uniform vec3 vertex_parameter0; //Light position.
uniform vec3 vertex_parameter1; //Camera position.

varying vec2  vTexCoord;
varying vec3  vLightVecDot;
varying vec3  vViewVec;
varying vec3  vNormal;

void main(void)
{   
   gl_Position = ftransform();

   vTexCoord = vec2(gl_MultiTexCoord0);
   vNormal = gl_NormalMatrix * gl_Normal;

   // Transform light vector into tangent space
   vec3 lightVec = vertex_parameter0.xyz - gl_Vertex.xyz;
   vLightVecDot.x = dot(lightVec, gl_MultiTexCoord2.xyz);
   vLightVecDot.y = dot(lightVec, gl_MultiTexCoord3.xyz);
   vLightVecDot.z = dot(lightVec, gl_Normal);
}
Fragment shader:
uniform sampler2D texture0;
uniform sampler2D texture1;

varying vec2  vTexCoord;
varying vec3  vLightVecDot;
varying vec3  vNormal;

void main(void)
{
   vec4 base = texture2D(texture0, vTexCoord);
   vec3 bump = texture2D(texture1, vTexCoord) * 2.0 - 1.0;

   bump          = normalize(bump);
   vec3 lightVec = normalize(vLightVecDot);

   float diffuse = clamp(dot(lightVec, bump), 0.0, 1.0);

   gl_FragColor = base * diffuse;
}
I would really appreciate some help with this since I have been trying to get this working for a week already

Share this post


Link to post
Share on other sites

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