LWJGL Forum

Programming => OpenGL => Topic started by: Daslee on November 22, 2015, 11:34:46

Title: Normal Mapping
Post by: Daslee on November 22, 2015, 11:34:46
I'm trying to add normal mapping technique in my game, but I can't get correct results. It looks like everything is okay, but lighting is inverted, take a look at this picture: http://s1.postimg.org/8g0lrdmcv/nmapping.png (http://s1.postimg.org/8g0lrdmcv/nmapping.png) so as you can see the plane is lit not correctly, it is lit with ambient component where it should be lit with diffuse and specular component and vice versa. I tried various different codes for shaders, but none worked correctly. This is my vertex shader:

void main()
{
Position = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));
TexCoord = VertexTexCoord;

Normal = normalize(NormalMatrix * vec3(0.0, 1.0, 0.0));
vec3 T = normalize(NormalMatrix * vec3(1.0, 0.0, 0.0));
vec3 B = normalize(cross(Normal, T));

TBNMatrix = mat3(T, B, Normal);

gl_Position = MVPMatrix * vec4(VertexPosition, 1.0);
}


and fragment shader:
vec3 PhongShade(PointLight light, vec3 n, vec3 diffR)
{
vec3 lightDir = normalize(light.Position.xyz - Position);
vec3 viewDir = normalize(-Position);

float sDotN = max(dot(lightDir, n), 0.0);

vec3 ambient = light.Color * material.Ambient * diffR;
vec3 diffuse = light.Color * diffR * sDotN;
vec3 specular = vec3(0.0);

if(sDotN > 0.0){
vec3 r = reflect(-lightDir, n);
specular = light.Color * material.Specular * pow(max(dot(r, viewDir), 0.0), material.Shininess);
}

return ambient + diffuse + specular;
}

void main()
{
vec3 normal = texture2D(normalMap, TexCoord).rgb;
normal = normalize(normal * 2.0 - 1.0);
normal = normalize(TBNMatrix * normal);

vec3 lightColor = PhongShade(light, normal, texture2D(texture, TexCoord).rgb);

FragColor = vec4(lightColor, 1.0);
}


And I can't see where is my mistake.. Btw if I invert texture coordinates when loading model it works good, but I don't think that it's the proper way to fix it.
Title: Re: Normal Mapping
Post by: emris on November 22, 2015, 14:10:30
Aloha,

I noticed that you have 2 "void mains()" in your fragment shader, no idea if that could be the problem though.

I think this tutorial can help -->  https://www.youtube.com/watch?v=4DUfwAEx4Ts
Title: Re: Normal Mapping
Post by: Daslee on November 22, 2015, 16:35:54
Quote from: emris on November 22, 2015, 14:10:30
Aloha,

I noticed that you have 2 "void mains()" in your fragment shader, no idea if that could be the problem though.

I think this tutorial can help -->  https://www.youtube.com/watch?v=4DUfwAEx4Ts

Actually I don't, I just did a mistake when copy pasted my code. And thanks for the video, but nothing changed after I did the same thing as in video. Here are my new shaders, vertex:
void main()
{
Position = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));
TexCoord = VertexTexCoord;

vec3 normal = normalize(vec3(ModelViewMatrix * vec4(VertexNormal, 0.0)));
vec3 tangent = normalize(vec3(ModelViewMatrix * vec4(VertexTangent, 0.0)));
vec3 bitangent = normalize(cross(normal, tangent));

mat3 toTangentSpace = mat3(
tangent.x, bitangent.x, normal.x,
tangent.y, bitangent.y, normal.y,
tangent.z, bitangent.z, normal.z
);

LightDir = toTangentSpace * (light.Position.xyz - Position);
ViewDir = toTangentSpace * (-Position);

gl_Position = MVPMatrix * vec4(VertexPosition, 1.0);
}


and fragment:
vec3 PhongShade(PointLight light, vec3 n, vec3 diffR)
{
vec3 lightDir = normalize(LightDir);
vec3 viewDir = normalize(ViewDir);

float sDotN = max(dot(lightDir, n), 0.0);

vec3 ambient = light.Color * material.Ambient * diffR;
vec3 diffuse = light.Color * diffR * sDotN;
vec3 specular = vec3(0.0);

if(sDotN > 0.0){
vec3 r = reflect(-lightDir, n);
specular = light.Color * material.Specular * pow(max(dot(r, viewDir), 0.0), material.Shininess);
}

return ambient + diffuse + specular;
}

void main()
{
vec3 normal = 2.0 * texture2D(normalMap, TexCoord, -1.0).rgb - 1.0;
normal = normalize(normal);

vec3 lightColor = PhongShade(light, normal, texture2D(colorTexture, TexCoord).rgb);

FragColor = vec4(lightColor, 1.0);
}


What I'm doing wrong?

EDIT: Nvm, normal map image was wrong