Push/Pop matrix for 2d games?

I’ve been reading up on this push/pop matrix stuff. I kinda don’t understand it but do I need to? If I’m making a 2D game, does pushing or popping any matrices have any sort of benefit, like speed or simplicity or something? If it’s not, then I’ll stop reading about it (for now) :stuck_out_tongue:

Thanks

Er, unless I’m wrong (Correct me people if I am!) Pushing/Popping matrix is to control it’s state when you’re doing operations. When you push, it saves the current state, allowing you to mess around with it. When you pop, it returns it to the state it was in when you last popped.

For a 2D game? It depends on how you’re doing it. I mean, it might be easier to use a library/framework that would obscure all of that from you (LibGDX and Slick2D come to mind). If you’re dead set on doing it on your own by setting up your own orthographic camera, writing out little square meshes and then texturing them, then yes it can be useful. All depends on what you’re doing. :3

Almost all of what I’m doing is just drawing quads. (OpenGL/LWJGL)
Sometimes I change draw color of sprites for certain effects, but I always have to set the draw color back to white at the end. If I used push/pop matrix, would I need this constant resetting?

Nope. That’s exactly what it’s for. You push, do something, then pop.

Think about like ‘tiling’ where you’d iterate through a large list of things and need to change the position and other things each time. You can push at the start of the operation, translate, push, paint the tile, pop, translate, push paint the tile, pop… And on and on. Then finally pop when you’re done with the operation.

Then you can do whatever else you need to (Like paint the UI, etc) without having to worry about whether you’d remembered to reset the position back to the origin.

Though, for what you describe about the Color things. For color stacking you need other calls than “glPushMatrix()” and “glPopMatrix()”. For that you need “glPushAttrib(GL_COLOR_BUFFER_BIT)” and “glPopAttrib()”.

Sorry to resurrect this thread, but I’m still kinda confused.

So I have an object with ints x, y, length, and height, including the ints red, green, and blue

How do I draw a colored rectangle with these variables, using a void method?

This doesn’t work… Eclipse isn’t recognizing the methods, and it’s not suggesting any imports

public void draw(){
		glPushAttrib(GL_COLOR_BUFFER_BIT);
		glColor3i(red, green, blue);
		glRecti(x,y,x+length,y+height);
		glPopAttrib();
}

I just kinda… don’t get it

Whenever I see someone refer to their IDE this way, it sort of raises a red flag to me that you’re letting your IDE think for you rather than learning the actual language.

I’ll give a hint: These methods are static methods on the GL11 class, and you need to statically import them if you want to refer to them without the prefix. If you did import them, then you’ve likely passed them the wrong argument types (and you should normally be getting a different error in that case)

Off topic here, but…

when using OpenGL, use floats, not ints. By using ints you are forcing an int -> float conversion every time you call a gl function like glColor3i. If you just stick with floats, this conversion doesn’t have to happen.

| Nathan

Haha yeah, I basically am learning by hopping from tutorial to tutorial and I accrue a lot of shameful gaps in my knowledge that way, and I was having a hard time figuring out how to search for the answers here.

Having said that, I was able to find out what to do with your hint. Once I statically imported GL11 all the squiggly red lines disappeared and my program ran.

However, I’ve just gotten stuck again because no rectangle is being drawn. There’s something I’m still doing wrong

public void draw(){
		glPushAttrib(GL_COLOR_BUFFER_BIT);
		glColor4f(red, green, blue,1f);
		glRecti(x,y,x+length,y+height);
		glPopAttrib();
}

Also, red/green/blue are all floats between 0 and 1 (and for my testing they are all 1)

Thanks