Allright, I’m off work. To start with, I am using the identity matrix to simplify things. I am using code from ra4king’s ports of the arcsynthesis tutorials and it works with his Matrix4 class but when I do it with the lwjgl Matrix4f class It does not work correctly.
Here is the rendering code. The Init() method (not included) is pretty standard vbo/vao binding stuff and I know it works because the draw method alternates between using the two matrix classes once every second. The Matrix4 class by ra4king works and the Matrix4f class does not. It flashes between a cube and black screen. It is pretty ugly because I’ve temporarily cut everything else away for debugging.
public void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Matrix4 modelToCameraMatrix = new Matrix4().clearToIdentity().translate(0, 0, -5);
program.begin();
time++;
glBindVertexArray(vao); // vao is a simple cube
if (time <=60) // doesn't work
glUniformMatrix4(cameraToClipMatrixUniform, true, toBuffer(camtoclip)); // have tried true and false for second param.
else if (time <=120) // does
glUniformMatrix4(cameraToClipMatrixUniform, false, cameraToClipMatrix.toBuffer());
else
time = 0;
glUniformMatrix4(modelToCameraMatrixUniform, false, modelToCameraMatrix.toBuffer());
glDrawElements(GL_TRIANGLES, indices.length, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
System.out.print(toBuffer(camtoclip) == cameraToClipMatrix.toBuffer());
program.end();
}
public FloatBuffer toBuffer(Matrix4f mat){
FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
matrixBuffer.clear();
mat.store(matrixBuffer); //have tried mat.storeTranspose(matrixBuffer) here
matrixBuffer.flip();
return matrixBuffer;
}
This is the toBuffer() method from Matrix4. The variable ‘matrix’ is a class scope float[].
private final static FloatBuffer direct = BufferUtils.createFloatBuffer(16);
public FloatBuffer toBuffer() {
direct.clear();
direct.put(matrix); // 'matrix' is a float[]
direct.flip();
return direct;
}
Here are the declarations and inits for the matricies.
private Matrix4f camtoclip;
private Matrix4 cameraToClipMatrix;
cameraToClipMatrix = new Matrix4().clearToIdentity()
camtoclip = new Matrix4f();
Thanks again. There’s a wheelbarrow full of medals here for whoever can explain what is wrong.
EDIT: Heres my vert shader
#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
smooth out vec4 theColor;
uniform mat4 cameraToClipMatrix;
uniform mat4 modelToCameraMatrix;
void main()
{
gl_Position = cameraToClipMatrix * modelToCameraMatrix * position;
theColor = color;
}