# GL Matrix Stack issue

This topic is 2126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello!

Recently i stumbled upon one issue. It took me few hours to find out why form is displaying some elements wrongly. I found that issue out, but i don't understand problem in my code.

Here is the code part that i have problem with. I use LWJGL.

[source lang="java"]
GL11.glPushMatrix();

// first translate
GL11.glTranslatef(100, 0, 0);

GL11.glPushMatrix();

//second translate
GL11.glTranslatef(0, 100, 0);

// first object
GL11.glPushMatrix();
glColor4f(1f, 1f, 1f, 1);
GL11.glPopMatrix();

//second object
GL11.glPushMatrix();
glColor4f(1f, 0f, 0f, 1);
GL11.glPopMatrix();

GL11.glPopMatrix();

GL11.glPopMatrix();[/source]

Problem is that second object ignores second glTranslatef in this case. If i would remove first glPushMatrix and last glPopMatrix, everything would work well, but the actuall form rendering code is quite bigger and i would like to know what am i doing wrong.

Maybe someone has any clues to what am i doing wrong?

Edit:
I call function to draw form in this code

[source lang="java"]
GL11.glPushMatrix();

glMatrixMode(GL_PROJECTION);
GL11.glPushMatrix();

glOrtho(0, UTILITY.SCREEN_WIDTH, UTILITY.SCREEN_HEIGHT, 0, 5, -5);

drawForm();

GL11.glPopMatrix();
glMatrixMode(GL_MODELVIEW);

GL11.glPopMatrix();[/source] Edited by Sollum

##### Share on other sites
Your current matrix mode is GL_PROJECTION when you're drawing and GL only specifies a stack depth of 2 for that (individual implementations may provide more but it's not guaranteed). The amount of pushing and popping you're doing is just overflowing the matrix stack.

You probably also don't want to be calling glScalef and glTranslatef on the projection matrix; these are more commonly used on modelview (the fact that the projection matrix stack is only specified as 2 deep is a pretty good hint that it's not intended to be used this way).

A more normal 2D drawing routine would look like: glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho (...); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glTranslatef (...); // initial position // draw objects with pushing/popping per-object if required Edited by mhagain

##### Share on other sites

Your current matrix mode is GL_PROJECTION when you're drawing and GL only specifies a stack depth of 2 for that (individual implementations may provide more but it's not guaranteed). The amount of pushing and popping you're doing is just overflowing the matrix stack.

You probably also don't want to be calling glScalef and glTranslatef on the projection matrix; these are more commonly used on modelview (the fact that the projection matrix stack is only specified as 2 deep is a pretty good hint that it's not intended to be used this way).

Oh my...

Thanks!

My first thought was that all matrixes have stack of 16, but now that you mention, i didnt notice i am using projection matrix all the time!

Thanks!

##### Share on other sites
http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml - modelview is specified as 32, the others as 2.

##### Share on other sites

http://www.opengl.or...lPushMatrix.xml - modelview is specified as 32, the others as 2.

Thanks!

I will think of some fast workaround.

P.S. I am drawing 2d menu onto 3d space, thats why i am using glOrtho, whilst model view matrix is storing last "3d" view

[source lang="java"]
GL11.glPushMatrix();

glMatrixMode(GL_PROJECTION);
GL11.glPushMatrix();

glOrtho(0, UTILITY.SCREEN_WIDTH, UTILITY.SCREEN_HEIGHT, 0, 5, -5);
glMatrixMode(GL_MODELVIEW);

drawForm();

glMatrixMode(GL_PROJECTION);
GL11.glPopMatrix();
glMatrixMode(GL_MODELVIEW);

GL11.glPopMatrix();[/source]

Seems to work... for now... Edited by Sollum

##### Share on other sites

I will think of some fast workaround.

You know you are using deprecated legacy OpenGL?

##### Share on other sites

[quote name='Sollum' timestamp='1348418048' post='4982958']
I will think of some fast workaround.

You know you are using deprecated legacy OpenGL?
[/quote]

Yes, i do. Hell was i surprised when i used GL ES 2.0 on Android, its so alien to me (facepalm). They don't even promote view matrixes ._.

At the moment i am trying to make game logic and mechanics work, rather than graphical side. If i will go somewhere near comercial, i will surely consider rewriting graphics "engine". But for now, old opengl is more than enough for my project to work at decent framerate.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002218
×