Jump to content

  • Log In with Google      Sign In   
  • 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.

  • You cannot reply to this topic
6 replies to this topic

#1 Sollum   GDNet+   -  Reputation: 760

Like
0Likes
Like

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.

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

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


Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 8277

Like
3Likes
Like

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

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
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.


#3 Sollum   GDNet+   -  Reputation: 760

Like
0Likes
Like

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!

#4 mhagain   Crossbones+   -  Reputation: 8277

Like
0Likes
Like

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.


#5 Sollum   GDNet+   -  Reputation: 760

Like
0Likes
Like

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

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

Edited by Sollum, 23 September 2012 - 10:41 AM.


#6 larspensjo   Members   -  Reputation: 1557

Like
0Likes
Like

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/

#7 Sollum   GDNet+   -  Reputation: 760

Like
1Likes
Like

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.



PARTNERS