opengl es HUD etc

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:



	// 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:


	[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!

Fot a HUD, the best approach is to draw all the opaque elements very early, so that early z culling wil avoid generating fragments that will be hidden anyway. The translucent elements are drawn last.

However, it is much simpler to draw all your 3D stuff first, and then your HUD.
So basically, this is your workflow :

gl_less depth test
clear buffers

setup matrices with gluLookAt or whatever
render all the 3D stuff

setup matrices with glOrtho
render your HUD

this is (more or less) my function to switch from 3D to ortho :


        glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0,x,0,y,1,3);

        glMatrixMode(GL_MODELVIEW);	glLoadIdentity();glTranslatef(dx,y-dy,-2);glScalef(1,-1,1);


        glEnable(GL_TEXTURE_2D);

        glEnable(GL_BLEND);

        glEnable(GL_ALPHA_TEST);

        glDisable(GL_DEPTH_TEST);

        glAlphaFunc( GL_GEQUAL,0.1f);


I wrote that a long time ago and it seems that I’m doing some strange stuff, but it works ^^

you can now draw your quad with glVertex2i or whatever.

However, it is much simpler to draw all your 3D stuff first, and then your HUD.

I think it depends on the hud opacity. If I am right, for a opaque hud, it is better IMO, to draw the hud first, then all fragments behind (typically, the scene geometry) this one will fail quickly and it save a write in the depth buffer.

That assumes that the architecture supports early Z cull. If you look at the topic, he is targeting OpenGL ES. TBDR architectures sort all of the geometry per frame anyway, so your draw order doesn’t make any difference.