GL Matrix Stack issue

Started by
5 comments, last by Sollum 11 years, 7 months ago
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.glTranslatef(UI_Form_CONFIG.FORM_HEADER_MARGIN_MOVE, UI_Form_CONFIG.FORM_HEADER_MARGIN_MOVE, 2);
GL11.glScalef(UI_Form_CONFIG.FORM_HEADER_BORDER_WIDTH, UI_Form_CONFIG.FORM_HEADER_BORDER_HEIGHT, 0);
drawQuad();
GL11.glPopMatrix();

//second object
GL11.glPushMatrix();
glColor4f(1f, 0f, 0f, 1);
GL11.glTranslatef(UI_Form_CONFIG.FORM_HEADER_MARGIN_MOVE, UI_Form_CONFIG.FORM_HEADER_MARGIN_MOVE, 1);
GL11.glScalef(UI_Form_CONFIG.FORM_HEADER_BORDER_WIDTH, UI_Form_CONFIG.FORM_HEADER_BORDER_HEIGHT / 2, 0);
drawQuad();
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();

GL11.glLoadIdentity();

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

drawForm();

GL11.glPopMatrix();
glMatrixMode(GL_MODELVIEW);

GL11.glPopMatrix();[/source]
Advertisement
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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


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!
http://www.opengl.org/sdk/docs/man/xhtml/glPushMatrix.xml - modelview is specified as 32, the others as 2.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


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

P.P.S. Changed my 2d menu drawing menu routine into
[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...

I will think of some fast workaround.


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

[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.

This topic is closed to new replies.

Advertisement