Results 1 to 10 of 18

Thread: Normal mapping

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    if you want to work in the model space aka transfrom the light vector instead of the normal - you are asking for a world of pain, especially if you are planning to add skinning to this code. btw if you still want to work in model space you need to multiply your light direction by a transpose of the rotation matrix (or just swap the order of multiplication, instead of l = mat * vec do l = vec * mat). also there is no need to normalize the light direction vector in vertex shader because you still have to normalize it in pixel shader (which you are not doing btw).
    so here's the code that I think should work:
    vertex shader
    Code:
    //it is a good idea to specify which version of GLSL
    //specification you want to work with
    #version 120
    uniform mat4 WVP;
    uniform mat4 W;
    uniform vec3 CamPos; //this is needed for specular reflactions (camera position)
    varying vec3 LightDir;
    varying vec3 CamDir;
    void main () {
      //this way of transforming the position is deprecated in later versions
      //of GLSL so I strongly suggest that you use the attributes of the vertex
      //gl_Position = ftransform();
      gl_Position = WVP * gl_Vertex;
      //again I recommend using the texture coord attribute here
      //but lets just keep it as is for now
      gl_TexCoord[0] = gl_MultiTexCoord0;
      vec3 VertexPosition = vec3(W * gl_Vertex);
      LightDir = gl_LightSource[0].position.xyz - VertexPosition;
      CamDir = VertexPosition - CamPos; //Direction from camera to vertex
      //absolutely no need to do this in vertex shader
      LightDir = normalize(LightDir);
    }
    pixel shader
    Code:
    #version 120
    uniform sampler2D colorMap;
    uniform sampler2D normalMap;
    uniform mat4 W;
    varying vec3 LightDir;
    varying vec3 CamDir;
    void main() {
      vec3 l = normalize(LightDir);
      vec3 n = texture2D(normalMap, gl_TexCoord[0].xy).xyz * 2.0 - 1.0;
      n = normalize(mat3(W) * n);
      vec3 r = CamDir - 2 * n * dot(CamDir, n);
      float DiffuseLight = clamp(dot(n, LightDir), 0, 1);
      float SpecularLight = pow(clamp(dot(n, r), 0, 1), gl_FrontMaterial.shininess);
      vec4 AmbientColor = vec4(gl_LightSource[0].ambient.xyz * gl_FrontMaterial.ambient.xyz, 1);
      vec4 DiffuseColor = vec4(gl_LightSource[0].diffuse.xyz * gl_FrontMaterial.diffuse.xyz * DiffuseLight, 1);
      vec4 SpecularColor = vec4(gl_LightSource[0].specular.xyz * gl_FrontMaterial.specular.xyz * SpecularLight, 1);
      gl_FragColor = texture2D(colorMap, gl_TexCoord[0].xy) * (DiffuseColor + AmbientColor) + SpecularColor;
    }
    for these shaders you will need to send additional uniforms to the shader like WVP is the combined matrix of ModelView * Projection, W is the transformation of the model, CamPos is just your camera position
    Last edited by Dan; 03-10-2012 at 04:06 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •