Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Push/Pop matrix for 2d games?  (Read 1526 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-08-02 19:11:08 »

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) :p

Thanks
Offline UprightPath
« Reply #1 - Posted 2012-08-02 19:53:37 »

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

Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #2 - Posted 2012-08-02 20:02:34 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2012-08-02 20:10:43 »

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.

Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-08-02 20:31:43 »

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()".

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #5 - Posted 2012-08-13 16:31:30 »

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

1  
2  
3  
4  
5  
6  
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
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-08-13 21:13:28 »

Eclipse isn't recognizing the methods, and it's not suggesting any imports

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)

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #7 - Posted 2012-08-13 21:19:28 »

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

1  
2  
3  
4  
5  
6  
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

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

Check out my website!
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #8 - Posted 2012-08-14 00:05:25 »

Eclipse isn't recognizing the methods, and it's not suggesting any imports

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)



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

1  
2  
3  
4  
5  
6  
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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!