LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: LinusD on September 23, 2019, 20:19:32

Title: [SOLVED]Issues with loading a Matrix4f as a uniform variable after updating ...
Post by: LinusD on September 23, 2019, 20:19:32
Hello, I need help. I have a rather complicated problem with loading uniform variables to my vertex shader.
I had a project in LWJGL version 2 and it worked. Now I updated to LWJGL 3 and changed everything to make it work again. My window doesn't show any models now.
I tested this:
Code: [Select]
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(-0.5f + pos1, 0.5f);
GL11.glVertex2f(0.5f, 0.5f);
GL11.glVertex2f(0.5f, -0.5f);
GL11.glVertex2f(-0.5f, -0.5f);
GL11.glEnd();
and it shows a quad, but when i activate my shader before I draw, it doesn't work anymore. I don't want to post all my code, but I can show the relevant stuff and why it is causing the issue.
When I remove all the matrices from my vertex shader code (transformation, projection, view). It shows the quad again:
Code: [Select]
gl_Position = vec4(position, 1.0);
This works.

I can also create a matrix in my shader and use it, so this works, too:
Code: [Select]
mat4 test;
test[0] = vec4(1.0,0.0,0.0,0.0);
test[1] = vec4(0.0,1.0,0.0,0.0);
test[2] = vec4(0.0,0.0,1.0,0.0);
test[3] = vec4(0.0,0.0,0.0,1.0);
gl_Position = test * vec4(position, 1.0);

But uniform matrices don't (and I tested floats, they work):
Code: [Select]
uniform mat4 test;

void main(void) {
   gl_Position = test * vec4(position, 1.0);
}

And my code to load it:
Code: [Select]
private static FloatBuffer matrixBuffer = MemoryUtil.memAllocFloat(16);
protected void loadMatrix(int location, Matrix4f matrix) {
matrix.get(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4fv(location, false, matrixBuffer);

//somewhere else:

loadMatrix(location_test, new Matrix4f());

}

The "new Matrix4f()" creates a matrix with the same values I created manually in my shader before, but my quad disappears again. And as I said, loading floats worked and I didn't misspell any of the variable names.
Anyone have an idea?
Title: Re: Issues with loading a Matrix4f as a uniform variable after updating to LWJGL 3
Post by: KaiHH on September 23, 2019, 20:31:13
Read the documentation on Matrix4f.get(FloatBuffer):
https://joml-ci.github.io/JOML/apidocs/org/joml/Matrix4f.html#get(java.nio.FloatBuffer)
Quote
This method will not increment the position of the given FloatBuffer.

Also read this page: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls
especially this section: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls#methods-taking-a-nio-buffer-do-not-modify-the-buffer-position

Also read this: https://github.com/JOML-CI/JOML/wiki/Migrating-from-LWJGL-2
Quote
One important difference is the handling of NIO FloatBuffers when getting values from or writing values into a FloatBuffer. In LWJGL 2 the position of the FloatBuffer will be incremented by load and store operations. In JOML the position will not be changed!

Also there are some examples on the README.md: https://github.com/JOML-CI/JOML#using-with-lwjgl
Title: Re: Issues with loading a Matrix4f as a uniform variable after updating to LWJGL 3
Post by: LinusD on September 23, 2019, 22:36:45
Thank you so much, flipping the buffer was the problem, thank you for helping me!