user500944
user500944

Reputation:

Storing and loading matrices in OpenGL

Is it possible to tell OpenGL to store the current transformation matrix in a specific location (instead of pushing it on a stack) and to load a matrix from a specific location?

I prefer a solution that does not involve additional data transfer between the video device and main memory (i.e. it's better to store the matrix somewhere in video memory).

Upvotes: 3

Views: 5495

Answers (2)

datenwolf
datenwolf

Reputation: 162164

On old fixed function pipeline the matrices were loaded into some special GPU registers on demand, but never were in VRAM, all matrix calculations happened on the CPU. In modern day OpenGL matrix calculations still happen on CPU (and right so), and are loaded into registers called "Uniforms" on demand again. However modern OpenGL also has a feature called "Uniform Buffer Objects" that allow to load Uniform (register) values from VRAM. http://www.opengl.org/wiki/Uniform_Buffer_Object

But they make little use for storing transform matrices. First, you'll change them constantly for doing animations. Second, the overhead of managing the UBOs for just a simple matrix eats much more performance, than setting it from the CPU. A matrix is just 16 scalars, or the equivalent of one single vertex with a position, normal, texture coordinate and tangent attribute.

Upvotes: 3

kakTuZ
kakTuZ

Reputation: 562

To answer the first part of you question:

The functions glLoadMatrix and glGet can do that

// get current model view matrix
double matrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX, matrix)

// manipulate matrix

glLoadMatrixd(matrix);

Note that these functions are not supported with OpenGL 4 anymore. Matrix operations have to be done on application site anyway and provided as uniform variables to the shader programs.

Upvotes: 9

Related Questions