Home > Backend Development > C++ > Why is My Normal Mapping Rendering Incorrectly?

Why is My Normal Mapping Rendering Incorrectly?

DDD
Release: 2024-11-30 17:16:12
Original
645 people have browsed it

Why is My Normal Mapping Rendering Incorrectly?

Normal Mapping Gone Horribly Wrong

In your normal mapping implementation, you may have overlooked several crucial aspects:

Fragment Shader

Color Mixing:

  • You are setting the output color multiple times, which can lead to undefined behavior.
  • Consider removing the extra assignments and setting the color in one step.

Ambient and Specular:

  • You are adding these components to the color instead of multiplying them.
  • The correct approach is to add ambient and multiply specular by the diffuse value.

TBN Matrix Creation

Function:

  • Ensure the getTangent() function correctly calculates the tangents and bitangents. You may want to refer to reliable sources for the proper equations.
  • Some graphics drivers tend to optimize unused shader variables, potentially removing the normal map data and leading to incorrect rendering. Check for this possibility.

Improved Fragment Shader

#version 430

uniform sampler2D diffuseMap;
uniform sampler2D normalMap;
uniform mat4 ModelMatrix;
uniform vec3 CameraPosition;

uniform struct Light {
    float ambient;
    vec3 position;
} light;
uniform float shininess;

in vec2 fsCoords;
in vec3 fsVertex;
in mat3 TBNMatrix;

out vec4 color;

void main()
{
    // Base color
    vec3 brownColor = vec3(153.0 / 255.0, 102.0 / 255.0, 51.0 / 255.0);
    vec3 baseColor = brownColor + 0.25; // Add a fixed base color
    
    // Normal
    vec3 normal = texture(normalMap, fsCoords).rgb * 2.0 - 1.0;
    
    // Surface position and lighting
    vec3 surfacePos = vec3(ModelMatrix * vec4(fsVertex, 1.0));
    vec3 surfaceToLight = normalize(TBNMatrix * (light.position - surfacePos)); // Unit vector
    vec3 eyePos = TBNMatrix * CameraPosition;
    
    // Diffuse
    float diffuse = max(0.0, dot(normal, surfaceToLight));
    
    // Specular
    float specular;
    vec3 incidentVector = -surfaceToLight; // Unit
    vec3 reflectionVector = reflect(incidentVector, normal); // Unit vector
    vec3 surfaceToCamera = normalize(eyePos - surfacePos); // Unit vector
    float cosAngle = max(0.0, dot(surfaceToCamera, reflectionVector));
    if (diffuse > 0.0)
        specular = pow(cosAngle, shininess);
    
    // Lighting
    vec3 finalColor = baseColor * light.ambient;
    finalColor += (diffuse + specular) * light.position;
    
    // Output color
    color = vec4(finalColor, 1.0);
}
Copy after login

The above is the detailed content of Why is My Normal Mapping Rendering Incorrectly?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template