4

我已经遵循了许多关于法线贴图的在线教程,并且在一定程度上已经让它工作了......

除非多边形垂直于视图矢量,否则我似乎无法使其正常工作。

我制作了一个视频来说明这一点,因为图片不足以说明我的意思。

http://www.youtube.com/watch?v=9D4o_zy-61s

我已经发布了我的着色器:

顶点:

#version 400
layout (location = 0) in vec3 attribPosition;
layout (location = 1) in vec3 attribNormal;
layout (location = 2) in vec2 attribTexCoord;
layout (location = 3) in vec3 attribTangent;
layout (location = 3) in vec3 attribBinormal;

uniform mat4 mvp;
uniform mat4 mvm;
uniform mat3 nm;
uniform vec3 lightPos_World;

out vec2 vUvs;
out vec3 lightPos_Eye;
out vec3 vertexPos_Eye;
out vec3 normalDir_Eye;
out vec3 normalDir_World;
out mat3 tbnMatrix;

void main()
{
    vUvs = attribTexCoord;
    normalDir_World = attribNormal;
    normalDir_Eye = (nm * attribNormal);
    vec3 t = nm * attribTangent;
    vec3 n = nm * attribNormal;
    vec3 b = nm * cross(n,t);

    mat3 tbnMatrix = mat3(t.x, b.x, n.x,
                          t.y, b.y, n.y,
                          t.z, b.z, n.z);

    vertexPos_Eye = (mvm * vec4(attribPosition,1.0)).xyz;
    lightPos_Eye = (mvm * vec4(lightPos_World,1.0)).xyz;

    gl_Position = mvp * vec4(attribPosition,1.0);
}

分段:

#version 400
layout( location = 0 ) out vec4 frag_main;
struct PointLight{
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    float shininess;
    float intensity;
};

uniform PointLight light[2];
uniform sampler2D colorTexture;
uniform sampler2D normalTexture;
uniform mat4 mvm;
//  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  FLAGS - all flags start false;
//  [0][0] = colour map
//  [0][1] = normal map
//  [0][2] = unused
//  [0][3] = show normal map in colour channel
//  [1][0] = use tangent space
//  [1][1] = unused
//  [1][2] = unused
//  [1][3] = unused
//  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uniform bvec4 flags[2];

in vec3 vertexPos_Eye;
in vec3 normalDir_World;
in vec3 normalDir_Eye;
in vec2 vUvs;
in mat3 tbnMatrix;


void main(){

    uint numLights=2;

    vec3 colorFragment;
    if(flags[0][0]){
        colorFragment = texture(colorTexture,vUvs).xyz; 
    }else{
        colorFragment = vec3(0.5);  
    }

    vec3 normalMapNormal = (texture(normalTexture,vUvs).rgb * 2) - 1;
    vec3 normalDir;
    if(flags[0][1]){
        normalDir = normalize(normalDir_Eye);
    }else{
        normalDir =  normalize(normalMapNormal.rgb);
    }

    vec4 outColor = vec4(1);
    outColor.xyz = vec3(0);

    float duller = 1/numLights;
    for(int i = 0;i< numLights;i++){
        vec3 lightPos_Eye = (mvm * vec4(light[i].position,1.0)).xyz;
        vec3 lightDir_Eye = normalize(lightPos_Eye - vertexPos_Eye);
        vec3 lightDir_Tangent = lightDir_Eye * tbnMatrix;

        vec3 vertDir_Eye = normalize(-vertexPos_Eye);
        vec3 vertDir_Tangent = vertDir_Eye * tbnMatrix;

        vec3 halfDir_Eye = normalize(vertDir_Eye + lightDir_Eye);
        vec3 halfDir_Tangent = halfDir_Eye * tbnMatrix;

        vec3 Ia = colorFragment.xyz * light[i].ambient;
        vec3 Id;
        vec3 Is;

        // tangent space
        Id = (colorFragment.xyz * light[i].diffuse ) * max(dot(lightDir_Tangent, normalDir), 0);
        Is = light[i].specular * pow(max(dot(halfDir_Eye, normalDir), 0), light[i].shininess*2);
        // eye space
        //Id = (colorFragment.xyz * light[i].diffuse ) * max(dot(lightDir_Eye, normalDir), 0);
        //Is = light[i].specular * pow(max(dot(halfDir_Eye, normalDir), 0), light[i].shininess*2);

        outColor.xyz += (Ia + Id + Is);

    }

    if(flags[0][3]){
        outColor.xyz = normalDir;
    }
    frag_main = (outColor);
}
4

1 回答 1

0

我注意到这条线:

vec3 b = nm * cross(n,t);

但应该是:

vec3 b = cross(n,t);

无需nm再次乘以,因为nt位于所需的相机空间中。

另一件事:

lightPos_Eye = (mvm * vec4(lightPos_World,1.0)).xyz;

因为lightPos_World在世界空间中(顾名思义),你应该将它乘以 *world_matrix* 之类的东西,而不是模型视图。或者只是lightPos_Eye在应用程序代码中计算并将其发送到顶点着色器。

管理所有这些不同的向量并将它们放在一个适当的空间中是非常困难的。有时视觉错误非常微妙且难以发现。

于 2013-03-10T11:30:51.047 回答