• Create Account

## GL Matrix Stack issue

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Sollum  Members

Posted 23 September 2012 - 09:46 AM

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.

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(); GL11.glLoadIdentity(); glOrtho(0, UTILITY.SCREEN_WIDTH, UTILITY.SCREEN_HEIGHT, 0, 5, -5); drawForm(); GL11.glPopMatrix(); glMatrixMode(GL_MODELVIEW); GL11.glPopMatrix();[/source]

Edited by Sollum, 23 September 2012 - 09:51 AM.

### #2mhagain  Members

Posted 23 September 2012 - 10:21 AM

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);
glOrtho (...);

glMatrixMode (GL_MODELVIEW);
glTranslatef (...); // initial position

// draw objects with pushing/popping per-object if required

Edited by mhagain, 23 September 2012 - 10:24 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3Sollum  Members

Posted 23 September 2012 - 10:24 AM

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!

### #4mhagain  Members

Posted 23 September 2012 - 10:28 AM

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

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #5Sollum  Members

Posted 23 September 2012 - 10:34 AM

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(); GL11.glLoadIdentity(); 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, 23 September 2012 - 10:41 AM.

### #6larspensjo  Members

Posted 23 September 2012 - 12:00 PM

I will think of some fast workaround.

You know you are using deprecated legacy OpenGL?
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #7Sollum  Members

Posted 23 September 2012 - 02:58 PM

I will think of some fast workaround.

You know you are using deprecated legacy OpenGL?

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.