切线空间法线贴图 - 着色器健全性检查

时间:2012-04-05 15:33:35

标签: opengl shader

我从切线空间法线贴图着色器得到了一些非常奇怪的结果:)。在我在这里展示的场景中,茶壶和方格的墙壁被我的普通Phong-Blinn着色器遮住(显然茶壶背面剔除给它一个轻微的短暂外观和感觉:-))。我试图将常规映射添加到球体中,并带有迷幻结果:

Incorrect Normal Mapping on Sphere

光线来自右边(就像黑色斑点一样可见)。我在球体上使用的法线贴图如下所示:

Normal Map

我正在使用AssImp来处理输入模型,因此它会自动为我计算每个顶点的正切和双法线。

像素和顶点着色器位于下方。我不太确定会出现什么问题,但如果切线基础矩阵在某种程度上是错误的话,我也不会感到惊讶。我假设我必须将事物计算到眼睛空间然后将眼睛和光线矢量转换为切线空间,这是正确的方法。请注意,灯光位置已进入视图空间中的着色器。

// Vertex Shader
#version 420

// Uniform Buffer Structures

// Camera.
layout (std140) uniform Camera
{
    mat4 Camera_Projection; 
    mat4 Camera_View;
};

// Matrices per model.
layout (std140) uniform Model
{
    mat4 Model_ViewModelSpace;
    mat4 Model_ViewModelSpaceInverseTranspose;
};

// Spotlight.
layout (std140) uniform OmniLight
{
    float Light_Intensity;

    vec3 Light_Position;            // Already in view space.
    vec4 Light_Ambient_Colour;
    vec4 Light_Diffuse_Colour;
    vec4 Light_Specular_Colour;
};

// Streams (per vertex)
layout(location = 0) in vec3 attrib_Position;
layout(location = 1) in vec3 attrib_Normal;
layout(location = 2) in vec3 attrib_Tangent;
layout(location = 3) in vec3 attrib_BiNormal;
layout(location = 4) in vec2 attrib_Texture;

// Output streams (per vertex)
out vec3 attrib_Fragment_Normal;
out vec4 attrib_Fragment_Position;
out vec3 attrib_Fragment_Light;
out vec3 attrib_Fragment_Eye;

// Shared.
out vec2 varying_TextureCoord;

// Main
void main()
{
    // Compute normal.
    attrib_Fragment_Normal = (Model_ViewModelSpaceInverseTranspose * vec4(attrib_Normal, 0.0)).xyz;

    // Compute position.
    vec4 position = Model_ViewModelSpace * vec4(attrib_Position, 1.0);

    // Generate matrix for tangent basis.
    mat3 tangentBasis = mat3(   attrib_Tangent, 
                                attrib_BiNormal, 
                                attrib_Normal);

    // Light vector.
    attrib_Fragment_Light = tangentBasis * normalize(Light_Position - position.xyz);

    // Eye vector.
    attrib_Fragment_Eye = tangentBasis * normalize(-position.xyz);

    // Return position.
    gl_Position = Camera_Projection * position;
}

...像素着色器看起来像这样:

// Pixel Shader
#version 420

// Samplers
uniform sampler2D Map_Normal;

// Global Uniforms

// Material.
layout (std140) uniform Material
{
    vec4  Material_Ambient_Colour;
    vec4  Material_Diffuse_Colour;
    vec4  Material_Specular_Colour;
    vec4  Material_Emissive_Colour;

    float Material_Shininess;
    float Material_Strength;
};

// Spotlight.
layout (std140) uniform OmniLight
{
    float Light_Intensity;

    vec3 Light_Position;    
    vec4 Light_Ambient_Colour;
    vec4 Light_Diffuse_Colour;
    vec4 Light_Specular_Colour;
};

// Input streams (per vertex)
in vec3 attrib_Fragment_Normal;
in vec3 attrib_Fragment_Position;
in vec3 attrib_Fragment_Light;
in vec3 attrib_Fragment_Eye;

// Shared.
in vec2 varying_TextureCoord;

// Result
out vec4 Out_Colour;

// Main
void main(void)
{
    // Compute normals.
    vec3 N = normalize(texture(Map_Normal, varying_TextureCoord).xyz * 2.0 - 1.0);  
    vec3 L = normalize(attrib_Fragment_Light);
    vec3 V = normalize(attrib_Fragment_Eye);
    vec3 R = normalize(-reflect(L, N));

    // Compute products.
    float NdotL = max(0.0, dot(N, L));
    float RdotV = max(0.0, dot(R, V));

    // Compute final colours.
    vec4 ambient  = Light_Ambient_Colour * Material_Ambient_Colour;
    vec4 diffuse  = Light_Diffuse_Colour * Material_Diffuse_Colour * NdotL;
    vec4 specular = Light_Specular_Colour * Material_Specular_Colour * (pow(RdotV, Material_Shininess) * Material_Strength);

    // Final colour.
    Out_Colour = ambient + diffuse + specular;      
}

编辑:3D Studio渲染场景(显示球体上的UV是正常的):

enter image description here

1 个答案:

答案 0 :(得分:3)

我认为你的着色器没问题,但球体上的纹理坐标完全没有了。就好像它们沿着经度向极点扭曲了一样。