2
\$\begingroup\$

So, ive been transitioning to modern opengl recently and it's going rather well. But alas, something must go wrong. As the titel says, all I'm getting is a completely black quad. (Ive double checked my C++ code and I'm pretty sure it has nothing to do with that.)

#version 330 core

//Vertex shader

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;
layout(location = 2) in vec2 vertexUV;
layout(location = 3) in vec3 vertexNormal;

out vec3 vertNorm;
out vec3 fragmentColor;
out vec3 vertPos;
out vec2 UV;

uniform mat4 MVP;

void main(){
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1.0f);
    UV = vertexUV;
    fragmentColor = vertexColor;
    vertPos = vertexPosition_modelspace;
    vertNorm = vertexNormal;
}

//Fragment shader
#version 330 core

in vec3 Normal;
in vec3 fragmentColor;
in vec3 vertPos;
in vec2 UV;

out vec3 color;

uniform sampler2D textureSampler;
uniform mat4 Model;

void main(){
    vec3 lightPos = vec3(0, 0, 0);
    mat3 normalMatrix = transpose(inverse(mat3(Model)));
    vec3 normal = normalize(normalMatrix * Normal);
    vec3 fragPosition = vec3(Model * vec4(vertPos, 1));
    vec3 surfaceToLight = lightPos - fragPosition;

    float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
    brightness = clamp(brightness, 0, 1);

    color = vec3(brightness * 1 * (texture(textureSampler, UV).rgb * fragmentColor));
}

But if you require my c++ code, say so and ill edit.

\$\endgroup\$
2
\$\begingroup\$

Note that out vec3 vertNorm; in your vertex shader does not match your corresponding input variable in the fragment shader in vec3 Normal;.

Since Normal in your fragment shader doesn't have any input it defaults to vec3(0, 0, 0). So your end up with a lot of unspecified behavior since you are normalizing a zero-vector which in this case probably returns another zero-vector. I don't know if there is an OpenGL standard there, at least it did for me. Then when calculating brightness you divide by zero again. Your math teacher would be furious at your dividing by zero twice in just a little bit of your code.

The end result is that brightness is 0 which is multiplied with the colors making all of the output colors vec3(0, 0, 0) or black.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy