为什么法线贴图没有正确显示?

时间:2015-06-20 16:05:46

标签: opengl jogl

我将凹凸贴图应用于树。如果没有应用法线贴图,则显示如下。

enter image description here

但是当使用普通纹理时,结果就像下面那样过于激进了,这很难看。

enter image description here

我检查了正常的切线数据。所以我想知道它是否是因为使用了错误的正常纹理图像。

enter image description here

这是我的顶点着色器代码:

            "#version 430                                       \n" + 
            "layout (location = 3) uniform mat4 mvMatrix;       \n" + 
            "layout (location = 4) uniform mat4 proMatrix;      \n" + 
            "uniform vec3 light_pos = vec3(100.0, 100.0, 100.0);\n" + 
            "                                                   \n" + 
            "layout (location = 0) in vec4 position;            \n" + 
            "layout (location = 1) in vec2 tc_in;               \n" + 
            "layout (location = 2) in vec3 normal;              \n" + 
            "layout (location = 5) in vec3 tangent;             \n" + 
            "layout (location = 10) in uint draw_id;            \n" + 
            "                                                   \n" + 
            "out VS_OUT                                         \n" + 
            "{                                                  \n" + 
            "   vec2 UV;                                        \n" + 
            "   vec3 L;                                         \n" + 
            "   vec3 V;                                         \n" + 
            "} vs_out;                                          \n" + 
            "                                                   \n" + 
            "void main(void)                                    \n" + 
            "{                                                  \n" + 
            "   vec4 P = mvMatrix * position;                   \n" + 
            "                                                   \n" + 
            "   vec3 N = normalize(mat3(mvMatrix) * normal);    \n" + 
            "   vec3 T = normalize(mat3(mvMatrix) * tangent);   \n" + 
            "   vec3 B = cross(N, T);                           \n" + 
            //" vs_out.L = light_pos - P.xyz;                   \n" + 
            "   vec3 Light = mat3(mvMatrix) * light_pos - P.xyz;\n" + 
            "   vs_out.L = normalize(vec3(dot(Light, T), dot(Light, B), dot(Light, N)));    \n" + 
            "                                                   \n" + 
            "   vec3 View = -P.xyz;                             \n" + 
            "   vs_out.V = normalize(vec3(dot(View, T), dot(View, B), dot(View, N)));   \n" + 
            "                                                   \n" + 
            "   vs_out.UV = tc_in;                              \n" + 
            "                                                   \n" + 
            "   gl_Position = proMatrix * P;                    \n" + 
            "}"

fs代码:

            "#version 430                                       \n" + 
            "layout (binding = 0) uniform sampler2D tex_color;  \n" + 
            "layout (binding = 1) uniform sampler2D tex_normal; \n" + 
            "out vec4 color;                                    \n" + 
            "                                                   \n" + 
            "in VS_OUT                                          \n" + 
            "{                                                  \n" + 
            "   vec2 UV;                                        \n" + 
            "   vec3 L;                                         \n" + 
            "   vec3 V;                                         \n" + 
            "} fs_in;                                           \n" + 
            "                                                   \n" + 
            "void main(void)                                    \n" + 
            "{                                                  \n" + 
            //" vec3 ambient = texture(tex_color, fs_in.UV).xyz * 0.2;          \n" + 
            "   vec3 diffuse_albedo = texture(tex_color, fs_in.UV).rgb; \n" + 
            "   vec3 specular_albedo = vec3(0.7);               \n" + 
            "   float specular_power = 128.0;                   \n" + 
            "                                                   \n" + 
            "   vec3 N = normalize(texture(tex_normal, fs_in.UV).rgb * 2.0 - vec3(1.0));    \n" + 
            "   vec3 L = fs_in.L;                               \n" + 
            "   vec3 V = fs_in.V;                               \n" + 
            "   vec3 R = reflect(-L, N);                        \n" + 
            "                                                   \n" + 
            "   vec3 diffuse = max(dot(N, L), 0.0) * diffuse_albedo;    \n" + 
            "   vec3 specular = pow(max(dot(R, V), 0.0), specular_power) * specular_albedo; \n" + 
            "                                                   \n" + 
            "   color = vec4(diffuse, 1.0);\n" + //ambient +  + specular
            //" color = vec4(texture(tex_color, fs_in.UV).rgb * 0.6, 1.0);\n" + 
            "}"

绘图命令:

        gl.glActiveTexture(GL4.GL_TEXTURE0);
        texture.bind(gl);

        gl.glActiveTexture(GL4.GL_TEXTURE1);
        bumpTexture.bind(gl);

        gl.glBindVertexArray(vaoBuff.get(0));
        gl.glMultiDrawElementsIndirect(...);

1 个答案:

答案 0 :(得分:1)

您的纹理是高度贴图。不是一张普通的地图。您有两个选择:用法线贴图替换高度贴图(它们通常是蓝色的)。或者从高度图计算法线。这可以这样做:

float heightLeft   = textureOffset(tex_normal, fs_in.UV, ivec2(-1, 0)).r;
float heightRight  = textureOffset(tex_normal, fs_in.UV, ivec2( 1, 0)).r;
float heightBottom = textureOffset(tex_normal, fs_in.UV, ivec2( 0,-1)).r;
float heightTop    = textureOffset(tex_normal, fs_in.UV, ivec2( 0, 1)).r;
vec3 N = normalize(cross(vec3(2, 0, heightRight - heightLeft), 
                         vec3(0, 2, heightTop - heightBottom));