Jump to content
  • Advertisement
Sign in to follow this  
Zazz

OpenGL gl Push & Pop Matrix not working?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. I'm just getting into OpenGL using VideoTutorialsRock.com and a couple of books. Most problems I get I manage to fix on my own. But a program I've been making has a slight glitch that I can't figure out. My program loops through a list of existing 'objects' and draws the shape (using simple custom makeCube/makeSquare etc functions) to the position held by the object. This works perfectly fine for a single object. When multiple objects are drawn though, they are positioned relitive to the last object drawn, even though the loop starts with glPushMatrix() and ends with glPopMatrix(). It's as if glPushMatrix() and glPopMatrix() where never there! (Does exactly the same when I comment them out) I apologise if this comes out wrong. There isn't a [ code ] button and this is my first post here :p _________________________________________________
//Loop through the list of existing objects
	for (game.objIter = game.objects.begin(); game.objIter != game.objects.end(); game.objIter++)
	{
		Object *temp = *game.objIter; //temp pointer to current object
		
		//Move to the objects position
		glPushMatrix(); //Save current translation
		glTranslatef(temp->getPosX(), temp->getPosY(), temp->getPosZ());
		
		//Check the current object's shape
		switch(temp->getShape())
		{
		case square:
			makeSquare();
				break;
		case cube:
			makeCube();
				break;
		case triangle:
			makeTriangle();
				break;
		case pyramid:
			makePyramid();
				break;
		}

		//Delete the temp pointer
		temp = NULL; delete temp;

		glPopMatrix(); //Restore last position
	}
_________________________________________________ Does this mean that glPush/PopMatrix don't work in loops, or am I missing something? I'ld hate to think I'm missing fundimental, but I don't think so. Cheers

Share this post


Link to post
Share on other sites
Advertisement
I just found out myself at the least
you use source and /source in the []


//Loop through the list of existing objects
for (game.objIter = game.objects.begin(); game.objIter != game.objects.end(); game.objIter++)
{
Object *temp = *game.objIter; //temp pointer to current object

//Move to the objects position
glPushMatrix(); //Save current translation
glTranslatef(temp->getPosX(), temp->getPosY(), temp->getPosZ());

//Check the current object's shape
switch(temp->getShape())
{
case square:
makeSquare();
break;
case cube:
makeCube();
break;
case triangle:
makeTriangle();
break;
case pyramid:
makePyramid();
break;
}

//Delete the temp pointer
temp = NULL; delete temp;

glPopMatrix(); //Restore last position
}

Share this post


Link to post
Share on other sites
Of course it works in loops. How would OpenGL know if your original source code contains a for-loop, or any other loop, and not just multiple consecutive function calls? Sure you're not doing something else in the draing functions that alters the matrix stacks?

Share this post


Link to post
Share on other sites
Are you pushing the matrix again in any of the make() functions? I was going to suggest glLoadIdentity(), but that's actually not the problem and not used at this point in my own code either.

Share this post


Link to post
Share on other sites
lol good point Brother Bob.
Ah cheers lightbringer, you're right I surrounded my make functions with push/pull. I wouldn't have thought to check that, thanks!

Share this post


Link to post
Share on other sites
You have another possible problem unrelated to OpenGL.
Quote:
Original post by Zazz
//Delete the temp pointer
temp = NULL; delete temp;
That's probably not doing what you think it does. delete-ing a NULL pointer in C++ does nothing. You probably don't want to be delete-ing anything here because temp is a pointer to some object and if you delete temp (before you set it to NULL) then you are destroying the object it points to and you'll get an access violation the next time you try to access that object.

The reason it "works" how it is right now is because you're not actually delete-ing anything since you set temp to NULL right before you try to delete it.

Share this post


Link to post
Share on other sites
Oh right, cheers. I've kinda jumped in the deep end with C++, so I guess I've missed a few fundimentals.

Share this post


Link to post
Share on other sites
Ensure your matrix mode is set to the proper one before each push and pop, especially if you change the mode inside your makes.

Ex.

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glMatrixMode(GL_PROJECTION);
glPushMatrix();

glPopMatrix();

glMatrixMode(GL_MODELVIEW); // without this line you would be popping the projection
glPopMatrix();



Also, the matrix stack does have a limitation to how many pushes you can do before it's full. You can get this with glGetIntegerv(GL_MODELVIEW_STACK_DEPTH) and (GL_MAX_MODELVIEW_STACK_DEPTH) with similar names for the Projection and Texture matrices.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!