vosure
vosure

Reputation: 25

Normal mapping working incorrectly, weird half-light effect

We are trying to implement normal mapping in our 2D Game Engine and get weird effect. If normal is set manually like that vec3 Normal = vec3(0.0, 0.0, 1.0) light works correctly, but we dont get "deep" effect that we want to achieve by normal mapping: enter image description here But if we get normal using normal map texture: vec3 Normal = texture(NormalMap, TexCoord).rgb it doesn't work at all. What should not be illuminated is illuminated and vice versa (such as the gaps between the bricks). And besides this, a dark area is on the bottom (or top, depending on the position of the light) side of the texture. enter image description here Although the texture of the normal map itself looks fine: enter image description here This is our fragment shader:

#version 330 core
layout (location = 0) out vec4 FragColor;

in vec2 TexCoord;
in vec2 FragPos;

uniform sampler2D OurTexture;
uniform sampler2D NormalMap;

struct point_light
{
    vec3 Position;
    vec3 Color;
};

uniform point_light Light;

void main()
{
    vec4 Color = texture(OurTexture, TexCoord);
    vec3 Normal = texture(NormalMap, TexCoord).rgb;

    if (Color.a < 0.1)
        discard;

    vec3 LightDir = vec3(Light.Position.xy - FragPos, Light.Position.z);

    float D = length(LightDir);

    vec3 L = normalize(LightDir);
    Normal = normalize(Normal * 2.0 - 1.0);

    vec3 Diffuse = Light.Color * max(dot(Normal, L), 0);
    vec3 Ambient = vec3(0.3, 0.3, 0.3);

    vec3 Falloff = vec3(1, 0, 0);

    float Attenuation = 1.0 /(Falloff.x + Falloff.y*D + Falloff.z*D*D);
    vec3 Intensity = (Ambient + Diffuse) * Attenuation;

    FragColor = Color * vec4(Intensity, 1);
}

And vertex as well:

#version 330 core
layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aTexCoord;

uniform mat4 Transform;
uniform mat4 ViewProjection;

out vec2 FragPos;
out vec2 TexCoord;

void main()
{
    gl_Position = ViewProjection * Transform * vec4(aPosition, 0.0, 1.0);
    TexCoord = aTexCoord;
    FragPos = vec2(Transform * vec4(aPosition, 0.0, 1.0));
}

I google about that and found some people that get the same result, but their questions remained unanswered. Any idea of what is the cause?

Upvotes: 1

Views: 1285

Answers (1)

dorel
dorel

Reputation: 36

What texture format are you using for the normal map? SRGB, SNORM, etc? That might be the issue. Try UNORM.

Additionally, since you are not using a tangent space, make sure the plane's Z axis aligns with the Z axis of the normals. Also OGL reads Y in the reversed direction, so you need to flip the Y coordinates of the normals that you read from the normal map. Alternatively, you can use a reversed Y normal map (green pointing down).

Upvotes: 2

Related Questions