I am currently working with lighting shaders in JOGL, and they are not working very well. I dont really know what i did wrong, but the light position is not applying (i think) and overall the light is dropping very weirdly, on the screenshot below you can see my cube render with lighting
The java code to render:
@Override
public void display(GLAutoDrawable drawable) {
float mod = 0.1f;
if(keys.contains("w")) player[2]+=mod;
if(keys.contains("s")) player[2]-=mod;
if(keys.contains("a")) player[0]+=mod;
if(keys.contains("d")) player[0]-=mod;
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
modelViewMatrix.loadIdentity();
modelViewMatrix.translate(player[0], player[1], player[2]-6);
modelViewMatrix.rotate((float) Math.toRadians(angle), 1, 1, 1);
modelViewMatrix.scale(1, 1, 1);
projectionMatrix.loadIdentity();
projectionMatrix.makePerspective(90f, (float) frame.getWidth()/frame.getHeight(), 1.5f, 20);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMultMatrixf(FloatBuffer.wrap(projectionMatrix.getMatrix()));
// Vertices and colors for the cube's faces
float[][] vertices = {
{-1.000000f, -1.000000f, 1.000000f},
{-1.000000f, 1.000000f, 1.000000f},
{-1.000000f, -1.000000f, -1.000000f},
{-1.000000f, 1.000000f, -1.000000f},
{1.000000f, -1.000000f, 1.000000f},
{1.000000f, 1.000000f, 1.000000f},
{1.000000f, -1.000000f, -1.000000f},
{1.000000f, 1.000000f, -1.000000f}
};
float[][] normals = new float[][] {
{-1.0000f, 0.0000f, 0.0000f},
{0.0000f, 0.0000f, -1.0000f},
{1.0000f, 0.0000f, 0.0000f},
{0.0000f, 0.0000f, 1.0000f},
{0.0000f, -1.0000f, 0.0000f},
{0.0000f, 1.0000f, 0.0000f}
};
int[] indices = new int[] {
1, 2, 0,
3, 6, 2,
7, 4, 6,
5, 0, 4,
6, 0, 2,
3, 5, 7,
1, 3, 2,
3, 7, 6,
7, 5, 4,
5, 1, 0,
6, 4, 0,
3, 1, 5
};
int errorCode = 0;
int id = l.getProgramID(gl);
gl.glUseProgram(id);
gl.glUniformMatrix4fv(gl.glGetUniformLocation(id, "modelViewMatrix"), 1, false, modelViewMatrix.getMatrix(), 0);
gl.glUniformMatrix4fv(gl.glGetUniformLocation(id, "projectionMatrix"), 1, false, projectionMatrix.getMatrix(), 0);
gl.glUniform3f(gl.glGetUniformLocation(id, "lightColor"), 1, 1, 1);
gl.glUniform3f(gl.glGetUniformLocation(id, "lightPosition"), 1, 2, -1);
gl.glUniform1f(gl.glGetUniformLocation(id, "shininess"), .5f);
l.setPosition(Buffers.newDirectFloatBuffer(to1d(vertices)));
l.setNormal(Buffers.newDirectFloatBuffer(to1d(normals)));
l.setColor(Buffers.newDirectFloatBuffer(new float[] {1, 0, 1}));
l.bindBuffers(gl);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glMultMatrixf(FloatBuffer.wrap(modelViewMatrix.getMatrix()));
gl.glDrawElements(GL2.GL_TRIANGLES, indices.length, GL2.GL_UNSIGNED_INT, Buffers.newDirectIntBuffer(indices));
errorCode = gl.glGetError();
if(errorCode != GL2.GL_NO_ERROR) {
String errorString = new GLU().gluErrorString(errorCode);
System.err.println("OpenGL error: 1 " + errorString);
}
l.disable(gl);
l.unbindBuffers(gl);
gl.glUseProgram(0);
angle += 0.5f;
}
Vertex shader:
#version 330 core
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inColor;
out vec3 fragColor;
out vec3 fragNormal;
out vec3 lightVec;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 lightPosition;
void main() {
vec4 worldPos = modelViewMatrix * vec4(inPosition, 1.0);
gl_Position = projectionMatrix * worldPos;
fragNormal = (modelViewMatrix * vec4(inNormal, 0.0)).xyz;
lightVec = lightPosition;
fragColor = inColor;
}
Fragment shader:
#version 330 core
in vec3 fragColor;
in vec3 fragNormal;
in vec3 lightVec;
out vec4 outColor;
uniform vec3 lightColor;
uniform float shininess;
void main() {
vec3 norm = normalize(fragNormal);
vec3 lightDir = normalize(lightVec);
float ndot = max(dot(norm, lightDir), 0.0);
vec3 diffuse = lightColor * fragColor * ndot;
outColor = vec4(diffuse, 1.0);
}
