Hello Guest

[SOLVED]Issues with loading a Matrix4f as a uniform variable after updating ...

  • 2 Replies
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.glVertex2f(-0.5f + pos1, 0.5f);
GL11.glVertex2f(0.5f, 0.5f);
GL11.glVertex2f(0.5f, -0.5f);
GL11.glVertex2f(-0.5f, -0.5f);
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) {
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?
« Last Edit: September 23, 2019, 22:37:54 by LinusD »


Offline KaiHH

  • ****
  • 334
Read the documentation on Matrix4f.get(FloatBuffer):
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
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

Thank you so much, flipping the buffer was the problem, thank you for helping me!