Push/PopMatrix

Started by
5 comments, last by Sparky 23 years, 3 months ago
I''ve been looking through the Blue and Red books to find out more about glPushMatrix() and I understand that it essentialy makes a copy of the matrix on the stack. However, I am having a problem getting this to work properly. I''m attempting to display 3D objects and 2D text at the same time and I start out with an Ortho view. My question is do I need to start with a Projection view or does that even matter? Depending on where I place glPushMatrix, I either get the text or I get the 3D object but not both. I''m assuming that this is because I''m not Pushin and a Popin where I need to! (Heheh). Here''s the drawing code: int DrawGLScene(GLvoid) { GLfloat yrot=0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glPushMatrix(); glPushAttrib(GL_PROJECTION); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f,0.0f,0.0f); glRotatef(yrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glRotatef(yrot,0.0f,0.0f,1.0f); // Select the earth texture map glBindTexture(GL_TEXTURE_2D, texture[1]); gluSphere(quadratic,20.2f,32,32); yrot-=0.02f; glPopMatrix(); glPopAttrib(); glDisable(GL_DEPTH_TEST); // Select the font texture glBindTexture(GL_TEXTURE_2D, texture[0]); glColor3f(1.0f,0.0f,0.0f); glPrint(20,20,1,"FPS:%2i",(int)fps); glPrint(20,40,1,"Pitch:%2i",pitch); glPrint(20,60,1,"Roll:%2i",roll); return TRUE; } I''ve tried several different sequences and nothing seems to get both on the screen. Any suggestions? Maybe a tutorial on combining 2D and 3D would help. I just can''t seem to find enough info on glPush/PopMatrix to figure out what I''m doing wrong. Thanks for any help in advance... Sparky "Keep the dirty side down"
Robert Snyder
Advertisement
This is how I would do it... Not certain it works.. Untested...

int DrawGLScene(GLvoid)
{
GLfloat yrot=0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(yrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(yrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, texture[1]);
gluSphere(quadratic,20.2f,32,32);
yrot-=0.02f;

glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glColor3f(1.0f,0.0f,0.0f);
glPrint(20,20,1,"FPS:%2i",(int)fps); glPrint(20,40,1,"Pitch:%2i",pitch);
glPrint(20,60,1,"Roll:%2i",roll);
return TRUE;
}

or

int DrawGLScene(GLvoid)
{
GLfloat yrot=0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glPushMatrix();
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(yrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(yrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, texture[1]);
gluSphere(quadratic,20.2f,32,32);
yrot-=0.02f;
glPopMatrix();

glPushMatrix();
glDisable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glColor3f(1.0f,0.0f,0.0f);
glPrint(20,20,1,"FPS:%2i",(int)fps); glPrint(20,40,1,"Pitch:%2i",pitch);
glPrint(20,60,1,"Roll:%2i",roll);
glPopMatrix();

return TRUE;
}

What do you use glMatrixMode(GL_MODELVIEW) for?

Mvh
Søren Olesen
The glMatrixMode(GL_MODEL_VIEW) was just something I tried to get it working. I didn''t mean to add that part in my question.
Still having the same problem though (scratching head & pulling on what''s left of my hair!)

I know it can be done because I''ve seen demo''s with the FPS showing while 3D stuff is on the screen.......grrrrrrrr!

Robert Snyder
Robert Snyder
Well, from what I understand, Push & PopMatrix push/pop the *currently active* matrix onto the matrix stack. If you are in ModelView mode, only the ModelView matrix will be pushed/popped. If you push a matrix in one mode, pop it off *before* switching to a different matrix mode.

Hope that helps.



/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
Here''s a couple Ideas. I don''t know if they''ll work though.

int DrawGLScene(GLvoid)
{
// make this a global variable or a static variable or it will reinitialize every time and you''ll get no rotation
GLfloat yrot=0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// not needed - glPushMatrix();
// not needed - glPushAttrib(GL_PROJECTION);

// this line could be moved to the init code as all objects are being drawn with textures
glEnable(GL_TEXTURE_2D);

glEnable(GL_DEPTH_TEST);

// this line could be moved to the init code as blending is never enabled
glDisable(GL_BLEND);

// not needed - glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(yrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(yrot,0.0f,0.0f,1.0f);
// Select the earth texture map
glBindTexture(GL_TEXTURE_2D, texture[1]);
gluSphere(quadratic,20.2f,32,32);

yrot-=0.02f;

// not needed - glPopMatrix();
// not needed - glPopAttrib();

// add this line to clear the matrix (the text dosn''t need to be transformed)
glLoadIdentity();

glDisable(GL_DEPTH_TEST);
// Select the font texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glColor3f(1.0f,0.0f,0.0f);

glPrint(20,20,1,"FPS:%2i",(int)fps);
glPrint(20,40,1,"Pitch:%2i",pitch);
glPrint(20,60,1,"Roll:%2i",roll);

return TRUE;
}

------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
Hi there,

this is the code i use.

  glMatrixMode(GL_PROJECTION);      glPushMatrix();      glLoadIdentity();      gluOrtho2D(0,Width,Height,0);        glMatrixMode(GL_MODELVIEW);          glPushMatrix();          glLoadIdentity();          glPushAttrib(GL_ENABLE_BIT);          glDisable(GL_LIGHTING);          glDisable(GL_DEPTH_TEST);          //all orthographic output here           glPopMatrix();        glMatrixMode(GL_PROJECTION);      glPopMatrix();      glMatrixMode(GL_MODELVIEW);  

Hope its of some help
This is what i''m doing :


Draw your 3d stuff
Enable2D();

draw2d stuff

Disable2D();

draw 3d stuff again




void Enable2D()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, v_x, 0, v_y, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

void Disable2D()
{

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

This topic is closed to new replies.

Advertisement