We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › 2D drawing on a 3D scene
Page Index Toggle Pages: 1
2D drawing on a 3D scene (Read 2017 times)
2D drawing on a 3D scene
Sep 11th, 2008, 10:37am
 
(first post)

hello everyone!
I'm just going to be straight-forward and give an example: let's say i have a 3D scene with geometry, lights etc. and a camera that is constantly changing via user input (e.g. keyboard keys w, a, s, d and mouse).. yup, you guessed it probably, it's a first person camera Smiley what I'd like to know is an effective way to draw a HUD during the game play. what i am doing now is get the camera's position and rotation along it's center and then do some push/pop and translate/rotate to draw the text, images etc. (the magical HUD) let's say 500 pixels away from the camera and in front of it all the time. this gives the impression of a 2D overlay on the screen, but this is not so professional to do, I know.

I'm sure it's possible to do it some other way, and I'd appreciate the help.
Re: 2D drawing on a 3D scene
Reply #1 - Sep 11th, 2008, 12:34pm
 
you can use ortho mode and define a different viewport where you draw your HUD

here's a class i use to draw a fullscreen quad. it defines a space of the size of the screen and from there u just draw normally (in other words as you would normally when u using a processing sketch)

i use direct opengl calls, but you could change it easily to use processing ones.



class Overlay
{
 Overlay( GL gl )
 {
   _gl = gl;
 }
 
 void setOrtho()
 {
   //
   // save old MVP matrices and set ortho mode for screen size (pixels)
   // warning. after drawing you must call unsetOrtho() call
   //

   // Setting orthographic projection.  
   _gl.glMatrixMode( GL.GL_PROJECTION );
   _gl.glPushMatrix();    
   _gl.glLoadIdentity();
   _gl.glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
   _gl.glMatrixMode( GL.GL_MODELVIEW );
   _gl.glPushMatrix();    
   _gl.glLoadIdentity();
 }
 
 void unsetOrtho()
 {
   // restore old matrices MVP
   _gl.glMatrixMode( GL.GL_PROJECTION );
   _gl.glPopMatrix();  
   _gl.glMatrixMode( GL.GL_MODELVIEW );
   _gl.glPopMatrix();      
 }
 
 void draw( int texID )
 {
   // bind texture
   _gl.glBindTexture( GL.GL_TEXTURE_2D, texID );
   
   gl._gl.glBegin( GL.GL_QUADS );
   
   gl._gl.glNormal3f( 0.0f, 0.0f, 1.0f );
   gl._gl.glColor4f( gl._r, gl._g, gl._b, gl._a );
   
   gl._gl.glTexCoord2f( 0.0, 0.0 );
   gl._gl.glVertex2f( 0.0, 0.0 );

   gl._gl.glTexCoord2f( 1.0, 0.0 );
   gl._gl.glVertex2f( width, 0.0 );

   gl._gl.glTexCoord2f( 1.0, 1.0 );
   gl._gl.glVertex2f( width, height );

   gl._gl.glTexCoord2f( 0.0, 1.0 );
   gl._gl.glVertex2f( 0.0, height );
   gl._gl.glEnd();
   
   // unbind texture
   //_gl.glBindTexture( GL.GL_TEXTURE_2D, 0 );
 }
 
 //
 // Members
 //
 GL _gl;
};
Re: 2D drawing on a 3D scene
Reply #2 - Sep 11th, 2008, 1:03pm
 
thank you very much! this sure is better than my n00b solution.
Re: 2D drawing on a 3D scene
Reply #3 - Sep 11th, 2008, 3:30pm
 
just another thing, i can't figure out how i can use p5 functions like text("bla", x, y) with the class.. could you please, please give me an example? (i feel totally stupid)
Re: 2D drawing on a 3D scene
Reply #4 - Sep 12th, 2008, 11:56am
 
escaped from the bounds of not thinking and figured it out during the night Smiley
Re: 2D drawing on a 3D scene
Reply #5 - Sep 18th, 2008, 12:50pm
 
hi, well you already got this working but something else you could make (probably slightly slower but alot simpler) e just call camera(); this will reset your viewport to its original settings and you can just draw your text after that.
Re: 2D drawing on a 3D scene
Reply #6 - Sep 19th, 2008, 3:09pm
 
pelintra wrote on Sep 18th, 2008, 12:50pm:
hi, well you already got this working but something else you could make (probably slightly slower but alot simpler) e just call camera(); this will reset your viewport to its original settings and you can just draw your text after that.


oh, lol Smiley
Re: 2D drawing on a 3D scene
Reply #7 - Nov 1st, 2008, 11:59pm
 
<3  This is just what I was looking for.

<3

Except!

_gl.glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

Gives some strange results.


glOrtho(left, right, BOTTOM, TOP, nearVal, farVal);

so instead of

_gl.glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

We want

_gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0);

Or everything is upside down and unhappy.

Thank you so much crmx!  You saved my brain!
Re: 2D drawing on a 3D scene
Reply #8 - Jan 3rd, 2009, 7:43pm
 
Hey,
Isn't there a way to push the projection matrix before calling the perspective etc.. functions using the gl object and popping it before drawing a 2d gui?
Wouldn't it be faster then setting it back to ortho?
I tried it like this

 gl = pgl.beginGL();
 gl.glMatrixMode( GL.GL_PROJECTION );
 gl.glPushMatrix();    
 gl.glMatrixMode( GL.GL_MODELVIEW );
 gl.glPushMatrix();  
 cam.feed() // using Damkjer's Camera class..
 box(340);
 gl.glMatrixMode( GL.GL_PROJECTION );
 gl.glPopMatrix();  
 gl.glMatrixMode( GL.GL_MODELVIEW );  
 gl.glPopMatrix();
 pgl.endGL();
 text("HELLO", 100, 100);
.. but it doesn't work..
Are the two methods above mentioned the only solution? pelintra how much of a slow down is calling camera()?
Page Index Toggle Pages: 1