opengl es HUD etc

Member
Posts: 94
Joined: 2008.08
Post: #1
Im attempting to get the HUD working for my game, but Im hitting some problems with setting the depth and a few other things. I think really Im getting confused as to what order to put things in etc. Ill post some of my current code which should help explain what Im currently doing and what I need to do.

Setup code:
Code:
    // Sets up matrices and transforms for OpenGL ES
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    glMatrixMode(GL_PROJECTION);
    glFrustumf(-1.0f, 1.0f, -1.5f, 1.5f, zNear, zFar);
    glViewport(0, 0, backingWidth, backingHeight);
    glMatrixMode(GL_MODELVIEW);

Draw loop:


Code:
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    customCamera();
    shipLoop();
    missileLoop();
    starLoop();
    
    glFlush ();
    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

So to get the HUD working in my draw loop ive tried to disable the depth test after the call to startLoop() function and then drawing some 2d triangles for the HUD... I then enabled depth testing again right before the customeCamera() function. This just doesnt seem to have any affect and Im kind flying blind with it I think. Any advice is greatly appreciated! Thanks!
Quote this message in a reply
Member
Posts: 338
Joined: 2004.07
Post: #2
For a 2D HUD, you'll want to disable depth testing, call glOrtho to set up an orthographic projection, and then draw as normal.

If nothing is drawing, make sure you are drawing in between your near and far clip planes. I've lost count of the number of times I've had stuff not draw, then realize I was drawing them at a z of 0, when my near clip plane was positive.

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #3
It still doesnt seem to be wanting to work for me (nothing is shown):

Ive added this code to the end of my draw loop:

Code:
    glDisable(GL_DEPTH_TEST);
    glOrthof(0, backingWidth, 0, backingHeight, -1, 1);
    
    glPushMatrix();
    
    glTranslatef(0.0, 0, 0.5f);    
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, hud);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);    
    
    glPopMatrix();

and here is the hud model:

Code:
const GLfloat hud[] = {
//front point
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,

};
Quote this message in a reply
Sage
Posts: 1,234
Joined: 2002.10
Post: #4
OpenGL transforms vertices like this:
object coordinates * ModelView * Projection * viewport.
Examine each stage to see where the problem is.

It this case, your object data is in unit space (-1 to 1)
It isn't clear what your ModelView matrix is. Assuming identity, you've still got -1 to 1.
Your Projection matrix is orthographic, so now you've got coordinates scaled down to clip coordinates, or (-1 to 1) / (backingWidth, backingHeight) in your case. This is very tiny.
At this stage, vertices are clipped. Your transformed triangle Y coordinate goes from 0 to -1/backingHeight, so it is offscreen.
Then, the results of clipping are scaled by viewport, so (tiny coordinates) * (backingWidth, backingHeight). You would get back your original (-1, 1), covering about one pixel, except that it was offscreen so it was clipped.

You need to make sure that your object data and your scale in the ModelView matrix are appropriately matched to your Projection matrix and viewport. In other words:
1) if you want to use object coordinates in (-1, 1) you can use the identity Projection matrix instead of Ortho. Or,
2) if you want to use Ortho Projection matrix, use object coordinates precalculated in that scale. Or,
3) fix your ModelView matrix to scale and translate correctly.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #5
thanks for your help.. I finally managed to get it working putting push/pop around my main game loop:

glPushMatrix();
customCamera();
shipLoop();
missileLoop();
starLoop();
glPopMatrix();

I think it needed this as the custom camera code was performing transformations on the hud overlay too.
Quote this message in a reply
Post Reply