Hi,
I was trying to add a normal map effect to a shader tutorial I have found here but it's strangely flickering O.O
The vertex shader code:
UPDATE 2:
http://www.gamedev.net/topic/654758-glsl-normal-mapping-strange-behaviour/?view=findpost&p=5141727
UPDATE 1:
http://www.gamedev.net/topic/654758-glsl-normal-mapping-strange-behaviour/?view=findpost&p=5141553
#version 330
in vec3 inPosition;
in vec3 vertNormal;
in vec2 vertTexCoord;
in vec4 vertNormalMapping;
out vec3 surfacePos;
out vec3 fragNormal;
out vec2 fragTexCoord;
out vec4 fragNormalMapping;
uniform mat4 modelViewProjectionMatrix;
uniform mat4 camera;
void main(){
fragTexCoord = vertTexCoord;
fragNormal = vertNormal;
surfacePos = vec3(modelViewProjectionMatrix * vec4(inPosition, 1));
fragNormalMapping = vertNormalMapping;
gl_Position = camera * modelViewProjectionMatrix * vec4(inPosition, 1.0);
}
The fragment shader
#version 330
precision highp float;
uniform vec3 cameraPosition;
uniform mat4 modelViewProjectionMatrix;
uniform sampler2D tex;
uniform sampler2D normalMap;
uniform float materialShininess;
uniform vec3 materialSpecularColor;
uniform struct Light {
vec3 position;
vec3 intensities; //a.k.a the color of the light
float attenuation;
float ambientCoefficient;
} light;
in vec3 fragNormal;
in vec3 surfacePos;
in vec2 fragTexCoord;
in vec4 fragNormalMapping;
out vec4 finalColor;
void main() {
vec3 normalMap;
if(fragNormalMapping[0] == 1.0f){ //has a normal map?
normalMap = texture(normalMap, fragTexCoord).xyz * 2.0 - 1.0;
normalMap.g = 1.0-normalMap.g;
normalMap = normalize(normalMap);
}else{ //No normal map texture so just normal calculation
normalMap = normalize(transpose(inverse(mat3(modelViewProjectionMatrix))) * (fragNormal));
}
vec4 surfaceColor = texture(tex, fragTexCoord);
vec3 surfaceToLight = normalize(light.position - surfacePos);
vec3 surfaceToCamera = normalize(cameraPosition - surfacePos);
//ambient
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
//diffuse0.0
float diffuseCoefficient =max( dot(normalMap, surfaceToLight),0.0);
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
//specular
float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0){
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalMap))), materialShininess);
}
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
//attenuation
float distanceToLight = length(light.position - surfacePos);
float attenuation = 1.0 / (1.0 + light.attenuation * pow(distanceToLight, 2));
//linear color (color before gamma correction)
vec3 linearColor = ambient + attenuation*(diffuse + specular);
//final color (after gamma correction)
vec3 gamma = vec3(1.0/2.2);
finalColor = vec4(pow(linearColor, gamma), surfaceColor.a);
}
For better testing I'm using a plane with a one color texture and a bump texture
this is the result
The light is following the camera and I can see that the normal map is calculated correctly. But as you can see there is something wrong with the "shadows" and I really don't know what it is.
If I load objects without a bump map the result with the same shader is perfect.
thank you everyone for the help =D