PDA

View Full Version : Load the current Matrix



Zenith
09-14-2017, 11:05 AM
I can update the current Matrix using an array with the function:

glLoadMatrixf(myArray);

But how can I do the inverse?
I mean how can I load the current Matrix in a array to view / edit it?

mhagain
09-14-2017, 11:28 AM
float myArray[16];
glGetFloatv (GL_MODELVIEW_MATRIX, myArray);
Reference: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glGet.xml

GClements
09-14-2017, 01:17 PM
While mhagain's answer is technically accurate, an arguably-better answer is "don't do that".

If you need to access the matrix' contents from client code, just keep a copy of the matrix in client memory. That means using e.g. GLM to construct the matrix rather than the legacy matrix operations (glRotate, glTranslate, etc). This is a large part of the reason why those functions have been removed from later versions of OpenGL.

Note that using glGet* can cause a pipeline stall, resulting in a significant performance hit. Ideally, it shouldn't be used outside of initialisation code.

Zenith
09-15-2017, 11:31 AM
Thank you people, my problem is solved.

mhagain
09-15-2017, 12:52 PM
It should be added however that because the legacy matrix stack is most likely going to be implemented in software, this particular glGet probably won't involve a round trip to the GPU and the associated pipeline stall. So yes, glGet calls for current state are in the general case a bad idea, but special case exceptions may exist.