0

Currently i try to implement Bump Mapping for my OpenGL Project. The Problem is that same parts of my cube is black. Like shown in this picture : enter image description here

I am almost certain that i just dont understand how the shaders works, so i used the shader from OpenGL Superbible . Here is my code : Vertex Shader

    #version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
layout(location = 3) in vec3 tangent;
layout(location = 4) in vec3 bitangent;

uniform mat4 matModel;
uniform mat4 matNormal;
uniform mat4 matMVP;
uniform mat4 matMV;
uniform vec3 light_pos = vec3(0.0,0.0,100.0);

out VS_OUT{
    vec3 eyeDir;
    vec3 lightDir;
    vec2 fragTexCoord;
} vs_out;

void main()
{
    vec4 P = matMV*position;
    vec3 V = P.xyz;
    vec3 L = normalize(light_pos - P.xyz);

    vec3 N = normalize(mat3(matNormal)*normal);
    vec3 T = normalize(mat3(matNormal)*tangent);
    vec3 B = cross(N,T);

    vs_out.lightDir = normalize(vec3(dot(L,T),
                                     dot(L,B),
                                     dot(L,N)));
    V = -P.xyz;
    vs_out.eyeDir = normalize(vec3(dot(V,T),
                                     dot(V,B),
                                     dot(V,N)));

    vs_out.fragTexCoord = texCoord;
    gl_Position = matMVP * position;
}

And the fragment shader :

#version 330

uniform sampler2D diffuseTex;
uniform sampler2D heightTex;
uniform vec3 heightColor;


in vec3 fragNormal;
in vec2 fragTexCoord;
in vec3 tangent_out_normalized;
in vec3 bitangent_out;
in vec3 normal_out_normalized;

in VS_OUT{
    vec3 eyeDir;
    vec3 lightDir;
    vec2 fragTexCoord;
}fs_in;

out vec4 outputColor;

void main()
{

    vec3 V = normalize(fs_in.eyeDir);
    vec3 L = normalize(fs_in.lightDir);
    vec3 N = normalize(texture(heightTex,fs_in.fragTexCoord).rgb*2-vec3(1.0));
    vec3 R = reflect(-L,N);
    vec3 diffuse_albedo =  texture(diffuseTex,fs_in.fragTexCoord).rgb;
    vec3 diffuse =max(dot(N,L),0)*diffuse_albedo;
    vec3 specular_albedo = vec3(1.0);
    vec3 specular = max(pow(dot(R,V),25.0),0.0)*specular_albedo;


    outputColor = vec4(diffuse+specular,1);
}

What am i missing?

greedsin
  • 1,252
  • 1
  • 24
  • 49
  • "*I am almost certain that i just dont understand how the shaders works, so i used the shader from OpenGL Superbible .*" So. You don't understand something, so you just copy-and-pasted it, and when that didn't work, you tell us to figure it out for you? – Nicol Bolas Feb 02 '16 at 14:37
  • Nice citation. I just asked for help to figure out what i am missing.Still i didnt copy and paste, because i had several implementations and got the same results, this example should just simplify the code.! – greedsin Feb 02 '16 at 14:42
  • From my experience black is very bad background for debugging. You don't know whether your object is transparent or just black. – Adrian Krupa Feb 02 '16 at 14:50
  • @AdrianKrupa yes you are right, i changed it and it seems that the parts of the cube are really black – greedsin Feb 02 '16 at 14:55

1 Answers1

2

It seems very wrong that you don't use fragNormal anywhere. You should use it to rotate the texture normal. To make it obvious, if the bump is flat you should still get the usual surface lighting.

The next strange thing is that you need to multiply you bump normal by 2 and subtract {1,1,1}. The normal should never flip and I suspect this is going on in your case. When it flips you will suddenly go from in light to in shadow, and that might cause the black areas.

Sorin
  • 11,863
  • 22
  • 26