javaopenglglsllwjgldirectional-light

LWJGL GLSL shader directional light appears inside out


When I render a vbo cube using a directional light shader, the light appears "inside out". (Sorry if I can't explain it better).

Outside

Inside

Here is my vertex shader code (My fragment shader just applies the color, all the work is done in the vertex shader).

#version 150 core
in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;
in vec3 in_Normal;

uniform sampler2D texture;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normalMatrix;
uniform vec3 lDir;

out vec4 color;

void main(void) {
    mat3 nMat = normalMatrix;
    vec3 normal = normalize(in_Normal * nMat);
    vec3 dir = normalize(lDir);
    float nl = max(dot(dir, normal), 0.25);
    //color = NdotL * vec4(0,1,0,1);
    color = vec4(0,1,0,1) * nl;
    gl_Position = projection * view * model * in_Position;
}

The lDir uniform variable stores a float[] array of {0,1,1}. The uniform variable normalMatrix is calculated like this.

    FloatBuffer preBuffer = BufferUtils.createFloatBuffer(16);
    modelMatrix.store(preBuffer);
    preBuffer.flip();
    Matrix4f preMat = new Matrix4f();
    preMat.load(preBuffer);

    Matrix3f preModel3f;
    Matrix3f normal3f = new Matrix3f();
    preModel3f = Tk.to3f(preMat);
    preModel3f.transpose(normal3f);
    normal3f.invert();

    normal3f.store(nmBuffer);
    nmBuffer.flip();
    glUniformMatrix3(glGetUniformLocation(ss.pId, "normalMatrix"), false, nmBuffer);

the modelMatrix variable is self explanitory, minus the fact that it is a buffer, not a matrix. The normal3f variable stores the transposed inverse of the model matrix (which I am pretty sure is the correct way to calculate the normal Matrix). I have to put the modelMatrix buffer into a buffer called preBuffer, if I don't I get errors for some reason.

Leave a comment if you need anything else to help solve this.


Solution

  • I figured out the problem, when I rendered the text to the screen (the text that describes the problem with the cube), I forgot to set the glBlendFunc() back to this setting,

    glBlendFunc(GL_ONE, GL_ZERO);
    

    This was causing the transparency issue and also fixed some other problems.