Display 2D over 3D

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Display 2D over 3D

Postby grumpymonkey » Sat Jul 03, 2010 11:38 pm

I'm trying to implement a GUI system in my game that uses opengl. I know that to do 2D you need to make a call to gl.glOrthof and to do 3D you make a call to gl.glPerspective when the view is set up or changed.

My problem is that idk how I would be able to make a call to both of them at the same time. I'm not a professional with OpenGL or anything I've only been using it for a couple of weeks but I'm not like a complete noob at it either.

These are the two functions in my program which I think are what need to be changed
Code: Select all
   public void onDrawFrame(GL10 gl) {
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();
      gl.glPushMatrix();
      meshes.drawAll(gl);
      gl.glPopMatrix();
   }
   
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL10.GL_PROJECTION);
      GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 1000.0f);
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
   }



any help is greatly appreciated as this is the only thing stopping me from finishing my game :)

and also please dont just say "you can check out my project at http://imtoolazytohelpyou.com" because that doesn't help and is a pain in the ass for other people looking for a solution to the same problem
I <3 Android - proud G1 user
grumpymonkey
Freshman
Freshman
 
Posts: 4
Joined: Sun Aug 16, 2009 8:45 pm
Location: Miami FL

Top

Re: Display 2D over 3D

Postby MichaelEGR » Sun Jul 04, 2010 1:23 am

Here are my set/reset ortho utility methods, "gl" is statically referenced.

Code: Select all
   public void resetOrtho()
   {
      gl.glMatrixMode(GL_PROJECTION);                 // Select Projection
      gl.glPopMatrix();                                 // Pop The Matrix
      gl.glMatrixMode(GL_MODELVIEW);                  // Select Modelview
      gl.glPopMatrix();                                 // Pop The Matrix
   }

   public void setOrtho(int width, int height)
   {
      gl.glMatrixMode(GL_PROJECTION);                  // Select Projection
      gl.glPushMatrix();                              // Push The Matrix
      gl.glLoadIdentity();                              // Reset The Matrix
      gl.glOrthof( 0, width, height, 0, -1, 1 );      // Select Ortho Mode
      gl.glMatrixMode(GL_MODELVIEW);                  // Select Modelview Matrix
      gl.glPushMatrix();                              // Push The Matrix
      gl.glLoadIdentity();                              // Reset The Matrix
   }


Take note of your matrix mode when you push/pop

All you have to do is render your 3D scene then pop into ortho mode and draw your GUI then reset ortho and call swapBuffers.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Display 2D over 3D

Postby grumpymonkey » Sun Jul 04, 2010 4:57 pm

Your code works but I can only display one scene at a time. If I draw the 3D scene then I cant see the 2D scene =(

also, I can't find a function called swapBuffers anywhere :oops:

Code: Select all
   public void onDrawFrame(GL10 gl) {   
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();
      gl.glPushMatrix();
      scene.drawAll(gl);     //draw my 3D scene
      gl.glPopMatrix();
      setOrtho(gl,50,30);  //Your function
                gui.drawAll(gl);     //draw my 2D GUI
      resetOrtho(gl);     //your function
   }
I <3 Android - proud G1 user
grumpymonkey
Freshman
Freshman
 
Posts: 4
Joined: Sun Aug 16, 2009 8:45 pm
Location: Miami FL

Re: Display 2D over 3D

Postby RedToasty » Thu Jul 08, 2010 9:45 pm

You're turning off depth testing when you draw the 2d scene I take it?

gl.glDisable(GL10.GL_DEPTH_TEST);
RedToasty
Freshman
Freshman
 
Posts: 9
Joined: Thu Jun 17, 2010 7:27 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests