2015-06-20 418 views
-1

我将凹凸贴图应用到树上。当没有应用正常的映射时,它看起来像这样。为什么法线贴图无法正确显示?

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

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

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)); 
+0

修正了一个正常的地图图像。什么是高度图用于?我在photoshop中看到两个选项,生成法线贴图,另一个是生成凹凸贴图。生成的凹凸贴图与高度贴图非常相似。 – Oscar

+0

高度图通常用于实际更改几何图形的位移贴图。法线贴图使用法线贴图,因为它需要更少的纹理样本。还有一些非常先进的技术,例如视差映射或视差遮挡映射。 –

相关问题