Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  switching between two OpenGL matrices  (Read 3734 times)
0 Members and 1 Guest are viewing this topic.
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Posted 2010-04-07 03:42:35 »

Bit of a noob question methinks.  Cheesy I have a game with 2D and 3D stuff to draw. I've been doing this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
// setup 3D matrix
while (true) {
   GL11.glMatrixMode(GL11.GL_PROJECTION);
   GL11.glPushMatrix();
   GL11.glLoadIdentity();
   GL11.glOrtho(0, 480, 320, 0, 1, -1);
   GL11.glMatrixMode(GL11.GL_MODELVIEW);
   GL11.glPushMatrix();
   GL11.glLoadIdentity();
   // draw 2D stuff
   GL11.glMatrixMode(GL11.GL_PROJECTION);
   GL11.glPopMatrix();
   GL11.glMatrixMode(GL11.GL_MODELVIEW);
   GL11.glPopMatrix();
   // draw 3D stuff
}


Is this the right approach? Now I have the need to switch between drawing 2D and 3D stuff many times per frame, so that the graphics are layered correctly. What would the most efficient way to do this?

Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2010-04-07 08:29:08 »

Are you rendering translucent stuff, or can you get away with just relying on the z-buffer?
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2010-04-07 09:23:43 »

Some layers are translucent, yes. Am I able to use the depth buffer when some stuff is ortho and some is perspective?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #3 - Posted 2010-04-07 10:05:05 »

Good question. I would think you'd be OK as long as the near and far clip planes are the same.

Since you're rendering translucent stuff, you are pretty much limited to rendering in back-to-front order, so it's either:
  • Live with the matrix changes (are they actually a problem?)
  • Scale the 2D layers based on their distance from the camera to counteract the perspective view matrix

edit: actually less sure about the z-buffer being OK - it's farther between the corners of the near and far clip planes than it is in the center with a perspective projection. No idea what would happen there.
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2010-04-07 10:25:42 »

True, I don't know that the matrix changes will be a problem. I just see I'm going to be switching them a bunch, so was wondering if there are any tricks I'm missing, or if I should even worry about this.

My perspective layers are drawn at an angle. It might be pretty tricky to fake ortho and still get the layers drawn at the correct depths.

FWIW, I'm also implementing this on the iPhone. Below are the current OpenGL calls, organized a bit differently than the LWJGL above:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
// setup 2D view
while (true) {
   // draw 2D stuff
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
   glRotatef(...);
   glFrustumf(...);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();
   glMultMatrixf(...);
   glTranslatef(...);
   // draw 3D stuff
   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   glMatrixMode(GL_MODELVIEW);
}


It certainly seems like a lot of stuff, especially if that is happening multiple times per frame. Is the way I'm doing this ideal? Does it matter if I push and pop ortho or would it be better to push and pop perspective?

Offline cylab

JGO Ninja


Medals: 55



« Reply #5 - Posted 2010-04-07 11:51:23 »

If the Matrix changes are a problem (profile it), you can just aggregate the needed 2d-stuff in some way (make yourself a descriptor object, which stores what you need to do in 2d and append it to a list) and draw all the 2d stuff in the end after one matrix change.

Mathias - I Know What [you] Did Last Summer!
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2010-04-07 11:54:42 »

Unfortunately, I can't do that. I need to draw some 2D stuff, then some 3D stuff on top, then more 2D stuff, then more 3D stuff, etc. That I need to layer the 2D and 3D stuff means I need to change the matrix a lot, it seems.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2010-04-07 12:05:50 »

Matrix manipulation (glLoadIdentity, glRotatef, etc.) is all cpu side. Only when you actually start drawing a batch is the final matrix sent to the graphics card.

If you really want to you could handle all the matrix maths yourself and just use glLoadMatrix (so you'd just need glMatrixMode() and glLoadMatrix() between each switch) but I think you'd be wasting your time.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2010-04-07 14:45:22 »

There's a (significant) overhead to each call to opengl in java due to JNI, so that speaks a lot for doing the math yourself and using glLoadMatrix()
However, glLoadMatrix is almost always slower than glTranslates and glRotates, as those are quite heavily optimized.

If you're simply switching back and forth between two modes, I'd suggest using display lists.
It's a single glCallList per switch, and it's super duper fast.

Play Minecraft!
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.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!