Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  switching between two OpenGL matrices  (Read 2450 times)
0 Members and 1 Guest are viewing this topic.
Offline Nate

JGO Neuromancer
****

Posts: 1063
Medals: 30


mooooo


« on: 2010-04-06 23: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
« League of Dukes »

JGO Strike Force
*****

Posts: 788
Medals: 4


Used to be bleb


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

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

JGO Neuromancer
****

Posts: 1063
Medals: 30


mooooo


« Reply #2 on: 2010-04-07 05: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! Go get 'em!
Offline ryanm
« League of Dukes »

JGO Strike Force
*****

Posts: 788
Medals: 4


Used to be bleb


« Reply #3 on: 2010-04-07 06: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 Neuromancer
****

Posts: 1063
Medals: 30


mooooo


« Reply #4 on: 2010-04-07 06: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 Kernel
*****

Posts: 1940
Medals: 27



« Reply #5 on: 2010-04-07 07: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 Neuromancer
****

Posts: 1063
Medals: 30


mooooo


« Reply #6 on: 2010-04-07 07: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
*****

Posts: 2960
Medals: 37


Monkey for a head


« Reply #7 on: 2010-04-07 08: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 Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #8 on: 2010-04-07 10: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.126 seconds with 20 queries.