Alberto García

06-22-2015, 01:56 PM

Hi, I've been following a tutorial in order to implement a normal mapping using GLSL, there is something wrong in my code, because when I set a light with a negative Z coordinate, the illumination of the model ( normal factor ) change when I move the camera.

I think that tangents and bitangents are well computed because are generated by Assimp library.

Can anybody please, check for errors in my code?

Here my code:

// VERTEX SHADER

struct Light

{

vec4 position;

vec3 color;

};

uniform Light sceneLight[8];

attribute vec3 tangent;

attribute vec3 bitangent;

varying vec3 lightDirTanSpace;

void main()

{

vec3 normal = normalize( gl_NormalMatrix * gl_Normal );

vec3 vertexPos = vec3( gl_ModelViewMatrix * gl_Vertex );

mat3 mTBN = transpose(mat3(tangent, bitangent, normal));

lightDirTanSpace = normalize( mTBN * ( sceneLight[0].position.xyz - vertexPos ) );

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_Position = ftransform();

}

// FRAGMENT SHADER

uniform sampler2D layers[8];

varying vec3 lightDirTanSpace;

void main( void )

{

vec3 normalMap = 2.0 * ( texture2D( layers[1], gl_TexCoord[0].st).rgb - 0.5 );

normalMap = normalize (normalMap);

float normalFactor = max(dot(normalMap, lightDirTanSpace), 0.0);

vec4 diffuse = texture2D( layers[0], gl_TexCoord[0].st );

gl_FragColor = diffuse * normalFactor;

Thanks in advance.

I think that tangents and bitangents are well computed because are generated by Assimp library.

Can anybody please, check for errors in my code?

Here my code:

// VERTEX SHADER

struct Light

{

vec4 position;

vec3 color;

};

uniform Light sceneLight[8];

attribute vec3 tangent;

attribute vec3 bitangent;

varying vec3 lightDirTanSpace;

void main()

{

vec3 normal = normalize( gl_NormalMatrix * gl_Normal );

vec3 vertexPos = vec3( gl_ModelViewMatrix * gl_Vertex );

mat3 mTBN = transpose(mat3(tangent, bitangent, normal));

lightDirTanSpace = normalize( mTBN * ( sceneLight[0].position.xyz - vertexPos ) );

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_Position = ftransform();

}

// FRAGMENT SHADER

uniform sampler2D layers[8];

varying vec3 lightDirTanSpace;

void main( void )

{

vec3 normalMap = 2.0 * ( texture2D( layers[1], gl_TexCoord[0].st).rgb - 0.5 );

normalMap = normalize (normalMap);

float normalFactor = max(dot(normalMap, lightDirTanSpace), 0.0);

vec4 diffuse = texture2D( layers[0], gl_TexCoord[0].st );

gl_FragColor = diffuse * normalFactor;

Thanks in advance.