Bump mapping with GLSL

803 views Asked by At

I'm trying to implement bump mapping but I don't know where is the problem, the shaders seems to be ok. I'm pretty sure the normals, tangents and bitangets are well computed and the problem is in the shader but if anyone wants to look other parts of the code here it is:

(https://github.com/CarlosCarrera/OpenglElements)

VertexShader.vsh

#version 120

attribute vec3 coord3d;
attribute vec3 normals;
attribute vec2 texcoord;
attribute vec3 tangents;
attribute vec3 bitangents;

varying vec3 LightDir;
varying vec2 f_texcoord;
varying vec3 ViewDir;

//Light
uniform vec4 LightPosition;
uniform vec3 LightIntensity;

//Matrices
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 mvp;

void main()
{
vec3 norm = normalize( NormalMatrix * normals );
vec3 tang = normalize( NormalMatrix * tangents);
vec3 bitang = normalize( NormalMatrix * bitangents);

mat3 toObjectLocal =mat3(
                          tang.x, bitang.x, norm.x,
                          tang.y, bitang.y, norm.y,
                          tang.z, bitang.z, norm.z );


// Transform light direction and view direction to tangent space
vec3 pos = vec3( ModelViewMatrix * vec4(coord3d,1.0));
LightDir = normalize( toObjectLocal * (LightPosition.xyz - pos));

ViewDir = toObjectLocal * normalize(-pos);

gl_Position = mvp * vec4(coord3d,1.0);
f_texcoord = texcoord;
}

FragmentShader.fsh

#version 120

varying vec3 LightDir;
varying vec2 f_texcoord;
varying vec3 ViewDir;

uniform vec4 LightPosition;
uniform vec3 LightIntensity;

uniform vec3 Ar;            // Ambient reflectivity
uniform vec3 Sr;            // Specular reflectivity
uniform float Shininess;    // Specular shininess factor

uniform sampler2D mytexture,mytexture2;

vec3 phongModel( vec3 norm, vec3 diffR ) {
vec3 r = normalize(reflect( -normalize(LightDir), normalize(norm) ));
vec3 ambient = LightIntensity * Ar;
float sDotN = max( dot(normalize(LightDir), normalize(norm)), 0.0 );
vec3 diffuse = LightIntensity * diffR * sDotN;

vec3 spec = vec3(0.0);
if( sDotN > 0.0 )
    spec = LightIntensity * Sr *
    pow( max( dot(r,normalize(ViewDir)), 0.0 ), Shininess );

return ambient + diffuse + spec;
}

void main() {
// Lookup the normal from the normal map

vec2 flipped_texcoord = vec2(f_texcoord.x, 1.0 - f_texcoord.y);

vec3 normal = 2.0 * texture2D(mytexture2, flipped_texcoord ).rgb - 1.0;
normal = normalize(normal);
vec4 texColor = texture2D( mytexture, flipped_texcoord );

gl_FragColor = vec4( phongModel(normal.xyz, texColor.rgb), 1.0 );
}

The results I'm getting are this:

https://dl.dropboxusercontent.com/u/1015014/Captura%20de%20pantalla%202013-12-04%20a%20la%28s%29%2001.16.08.png

1

There are 1 answers

0
Tolokobcn On BEST ANSWER

Ok, so I finally found the bug...It was not in the shaders, so the shaders are ok! The problem I had is that I wasn't passing the normalmap image to the shader correctly.