如何在 Matrix4f 中获取当前的 OpenGL 变换矩阵?

How can I get my current OpenGL transformation matrix in a Matrix4f?

为了照明,我需要获取当前的变换矩阵。

例如,Matrix4f transMatrix = Get_OpenGL_Transformation_Matrix

论坛上有各种各样的答案,但要么不起作用,要么人们意见不一。

简单。

float[16] transMatrix;
glGetFloatv(GL_MODELVIEW_MATRIX, transMatrix);

这纯粹来自文档,我从未使用过LWJGL。但我相信这样的事情应该有效:

// Create FloatBuffer that can hold 16 values.
FloatBuffer buf = BufferUtils.createFloatBuffer(16);

// Get current modelview matrix:
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, buf);

// Rewind buffer. Not sure if this is needed, but it can't hurt.
buf.rewind();

// Create a Matrix4f.
Matrix4f mat = new Matrix4f();

// Load matrix from buf into the Matrix4f.
mat.load(buf);