视差映射 - 仅在一个方向上起作用

时间:2015-04-11 18:39:25

标签: c++ opengl glsl parallax

我正在进行视差贴图(来自本教程:http://sunandblackcat.com/tipFullView.php?topicid=28),当我沿着一个轴(例如,从左到右)移动时,我看起来只是取得了良好的效果四。下图说明了这一点:

parallax mapping wrong

您可以在左右陡峭的边缘清楚地看到它。如果我向右移动,右边的陡峭边缘的宽度应该小于左边的边缘(在左边的图像上看起来是正确的)[相机位于立方体的右侧]。但是,如果我沿着不同的轴(而不是从西向东移动,我现在从上到下移动),您可以看到此时陡峭的边缘不正确[相机再次位于立方体的右侧]。

我使用最简单的视差映射形式,甚至有相同的问题。片段着色器看起来像这样:

void main()
{
    vec2 texCoords = fs_in.TexCoords;   
    vec3 viewDir = normalize(viewPos - fs_in.FragPos);
    vec3 V = normalize(fs_in.TBN * viewDir);
    vec3 L = normalize(fs_in.TBN * lightDir);


    float height = texture(texture_height, texCoords).r;
    float scale = 0.2;
    vec2 texCoordsOffset = scale * V.xy * height;

    texCoords += texCoordsOffset;

    // calculate diffuse lighting
    vec3 N = texture(texture_normal, texCoords).rgb * 2.0 - 1.0;
    N = normalize(N); // normal already in tangent-space
    vec3 ambient = vec3(0.2f);
    float diff = clamp(dot(N, L), 0, 1);
    vec3 diffuse = texture(texture_diffuse, texCoords).rgb * diff;
    vec3 R = reflect(L, N);
    float spec = pow(max(dot(R, V), 0.0), 32);
    vec3 specular = vec3(spec);
    fragColor = vec4(ambient + diffuse + specular, 1.0);
}

TBN矩阵在顶点着色器中创建如下:

vs_out.TBN = transpose(mat3(normalize(tangent), normalize(bitangent), normalize(vs_out.Normal)));

我使用TBN的转置将所有相关向量转换为切线空间。在没有抵消TexCoords的情况下,照明看起来很稳固,具有正常的映射纹理,所以我的猜测是它不是引起问题的TBN矩阵。是什么导致它只能在一个方向起作用?

修改

有趣的是,如果我反转TexCoords输入变量视差映射的y坐标似乎有效。我不知道为什么会这样,我需要它在没有反转的情况下工作。

vec2 texCoords = vec2(fs_in.TexCoords.x, 1.0 - fs_in.TexCoords.y);   

0 个答案:

没有答案