How to draw HUD with glOrtho?

Hi, I want to draw a healt bar to the top of my screen and then to scale it based on how much health I have left.

I found 2 functions that enable me to switch from projection to 3d drawing. This is Java code for OpenGL ES, but the principles of OpenGL should be the same :).:
private void viewOrtho(GL10 gl, int x, int y){ // Set Up An Ortho View
gl.glMatrixMode(gl.GL_PROJECTION); // Select Projection
gl.glPushMatrix(); // Push The Matrix
gl.glLoadIdentity(); // Reset The Matrix
gl.glOrthof( 0, x , y , 0, -1, 1 ); // Select Ortho Mode
gl.glMatrixMode(gl.GL_MODELVIEW); // Select Modelview Matrix
gl.glPushMatrix(); // Push The Matrix
gl.glLoadIdentity(); // Reset The Matrix

private void viewPerspective(GL10 gl) // Set Up A Perspective View
gl.glMatrixMode( gl.GL_PROJECTION ); // Select Projection
gl.glPopMatrix(); // Pop The Matrix
gl.glMatrixMode( gl.GL_MODELVIEW ); // Select Modelview
gl.glPopMatrix(); // Pop The Matrix

Then I use them to draw a cross in the middle of the screen in the main loop

       viewOrtho(gl, width,height);   //Starting to draw the HUD
  gl.glScalef(0.025f, 0.005f, 0.0f); //Have to scale it otherwise the screen would be filled with it   
  square.draw(gl);    //Draw horizontal line
  gl.glScalef(0.005f, 0.025f, 0.0f);    
  square.draw(gl);   //Draw vertical line
            viewPerspective(gl); //switch back to 3D drawing

I tried placing gl.glTranslatef to different places, but the + would still be drawn in the middle of the screen and glrotatef won’t rotate it either. I want do move it to a corner of the screen.

How can I do that? And why isn’t the glTranslate moving it? Can you give me to some other examples? A la
gl.glTranslatef(TopRightCorner.x, TopRightCorner.y, 0.0f);

Why not use glOrtho2D and make a matrix which maps the pixels of the screen one to one with your matrix. i.e. Use the screen dimensions for glOrtho2D.

Then construct the geometry with the vertices as the actual screen coordinates of the things you are trying to draw.

Seems a much simpler approach to me. :slight_smile:

After that you can even still move stuff around, or scale it, en-masse, with translates and other matrix operations.

Tx for quick reply. I’m using OpenGL ES 1.0, no glOrtho2D there :(.

I now know what I did do wrong. I had to put gl.glOrthof( 0, x , 0 , y, -1, 1 ); not gl.glOrthof( 0, x , y , 0, -1, 1 );

So now I can translate 2D stuff :).

Well glOrtho will work the same way.
I just type the 2D as a bad habit, sorry!

I presume you’re on some kind of PowerVR chip. The iPhone perhaps?

Basically what you are doing is what I meant…

glOrtho(0, Width , 0, Height, -1, 1);

Glad you got it sorted. :slight_smile:

Thank You. I’m using Android emulator.