I do realize that glPush/Pop matrix functions are not supported by many platforms (so don't shoot me) but does anyone knows how should I replace them without breaking my existing code base?
-
OpenGL ES for android/Iphone don't support those from what i've been told. – user1010005 Feb 18 '12 at 16:05
-
possible duplicate of [OpenGL ES 2.0 and glPushMatrix, glPopMatrix](http://stackoverflow.com/questions/2918232/opengl-es-2-0-and-glpushmatrix-glpopmatrix) – Feb 18 '12 at 16:06
-
Thanks although it doesn't seem to explain how to replace them ... – user1010005 Feb 18 '12 at 16:08
-
Actually push/pop matrix is supported for ALL platforms that implement OpenGL ES 1.1 (all android/ios devices) or OpenGL compatiblity profile (currently all desktops). – Mārtiņš Možeiko Feb 18 '12 at 16:22
2 Answers
Its actually pretty simple. Each time you push a matrix you are just multiplying it with the one that was one down the stack. You could VERY simply write your own push/pop matrix stack. Literally a stack of matrices for ModelView, Projection and Texture(s) and then each time you call push matrix just push Mprev * Mcurr to the top of the stack. When you pop you are just going back to the previous matrix.

- 61,365
- 24
- 124
- 204
Since these funcs are deprecated along with fixed function on many platforms and new core versions, you have to supply the matrices to your own shader (mandatory) as uniforms.
gl_ModelViewMatrix
, gl_ModelViewProjectionMatrix
and other builtin uniforms are removed from GLSL along with fixed function in all new OpenGL (core) versions aswell, regardless the platform.
Therefore, the decision should not be based on platforms, since all new core profiles don't have those functions anymore. Replacing this requires some effort, but will improve portability to all platforms and more importantly, (forward) compatibility to the new ones. Implementing a client side matrix stack is not that hard either.
EDIT: See this C++ example template, which mimicks the OpenGL matrix stack. It lacks the matrix math implementations, does not use the STL stack due to reasons not visible here, but shows the math operations to be done:
EDIT 2: added error check (1 element is the minimum stack size)
template<typename T>
class MatrixStack
{
public:
typedef T matrix_type;
private:
std::vector<matrix_type> stack;
public:
MatrixStack(void)
{
stack.push_back(matrix_type::identity());
}
void clear(void)
{
stack.clear();
stack.push_back(matrix_type::identity());
}
size_t size(void) const
{
return stack.size();
}
void push(void)
{
matrix_type tmp = stack.back(); //required in case the stack's storage gets reallocated
stack.push_back(tmp);
}
bool pop(void)
{
if (size() > 1)
{
stack.pop_back();
return true;
}
else
{
return false;
}
}
void load(const matrix_type& matrix)
{
stack.back() = matrix;
}
void loadIdentity(void)
{
load(matrix_type::identity());
}
void loadTransposed(const matrix_type& matrix)
{
load(transpose(matrix));
}
void mult(const matrix_type& matrix)
{
load(stack.back() * matrix);
}
void multTransposed(const matrix_type& matrix)
{
load(stack.back() * transpose(matrix));
}
const matrix_type& get(void) const
{
return stack.back();
}
};

- 7,778
- 1
- 23
- 49