-1

I'm applying bump mapping to a tree. When no normal mapping applied, it appears like this.

enter image description here

But when a normal texture is used, it turned out to be too aggressive as follow which is ugly.

enter image description here

I checked tangent data which should be fine. So I was wondering if it's because a wrong normal texture image is used.

enter image description here

Here is my vertex shader code:

            "#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 code:

            "#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" + 
            "}"

drawing command:

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

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

        gl.glBindVertexArray(vaoBuff.get(0));
        gl.glMultiDrawElementsIndirect(...);
Oscar
  • 1,993
  • 2
  • 18
  • 27

1 Answers1

1

Your texture is a height map. Not a normal map. You have two options: Replace the height map with a normal map (they are usually blueish). Or calculate the normal from the height map. This can be done like this:

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));
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Fixed with a normal map image. And what is a height map used for?I saw two options in photoshop, Generate a normal map, another is Generate a bump map. That bump map generated is very similar to the height map. – Oscar Jun 21 '15 at 07:53
  • Height maps are usually used for displacement mapping where the geometry is actually changed. Normal mapping uses normal maps because it requires less texture samples. There are also some very advanced techniques like parallax mapping or parallax occlusion mapping that take both into account. – Nico Schertler Jun 21 '15 at 08:15