Newbie Question: Using Depth Test for ordered drawing

Started by emu, January 11, 2011, 22:11:20

Previous topic - Next topic

emu

Hi!
im very new at this topic (opengl) so maybe im doing some mistakes.. :-[

i want to do a simple rpg-style 2d game. I need to "order" my Entities (For drawing) so that the "nearer" entities overlap tha "farer" ones. Now i want to use the z-coordinate to let the depth-test "order" my entities so i dont have to but it dont seem to work for me..

Maybe i misunderstand the zNear and zFar parameters of the glOrtho-function but as far as i understand i can use the range between zNear and zFar for my z-values.
But if i set zNear to 0 (zero) and zFar to well.. lets say 128 i can see my textures (im using slick) only if they have z = 0; If they have a different z-value, lets say 1, then they are not visible.

some code:

initGL()

try{
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.create();
			Display.setVSyncEnabled(true);
		}catch(LWJGLException e){
			e.printStackTrace();
			System.exit(-1);
		}		
GL11.glEnable(GL11.GL_TEXTURE_2D);
			
			GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);     
			
			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glLoadIdentity();
			
			GL11.glViewport(0, 0, width, height);
			
			GL11.glOrtho(0, width, 0, height, 0, 128);
			
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			
			GL11.glEnable(GL11.GL_CULL_FACE);
			GL11.glCullFace(GL11.GL_BACK);
			
			GL11.glLoadIdentity();
			
			GL11.glEnable(GL11.GL_DEPTH_TEST);
			GL11.glDepthFunc(GL11.GL_LESS);
			GL11.glDepthFunc(GL11.GL_EQUAL);
			
			GL11.glEnable(GL11.GL_ALPHA_TEST);
			GL11.glAlphaFunc(GL11.GL_GREATER, 0.1f);


loop:
while(true){
			if (Display.isCloseRequested()){
				Display.destroy();
				System.exit(0);
			}
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			
			elem.draw();    // only visible if z = 0
			tilemap.draw(); // only visible if z = 0

			Display.update();
			Display.sync(100);
		}


What am i doing wrong?? Do i need to order my entities manually? I just want all my entites drawn to the display overlap each other depending on the z value (rather than order them manually..)

thank you in advance!

avm1979

At first glance, you're doing this:

GL11.glDepthFunc(GL11.GL_LESS);
GL11.glDepthFunc(GL11.GL_EQUAL);

I'm guessing the depth buffer is initialized with a value of 0, which would make GL_EQUAL cause the behavior you describe.  You probably don't want that second line.

bobjob

also you may want to consider using "GL_LEQUAL" (less or equal)

emu

hi guys
thank you very much for your replies.

Quote
GL11.glDepthFunc(GL11.GL_LESS);
GL11.glDepthFunc(GL11.GL_EQUAL);

i tried out some flags and i forgot to comment them out!^^

Quote"GL_LEQUAL"

THAT is the flag that work for me!

But...
my mistake was, that i gave a POSITIVE value to my textures z-values! (So they were drawn behind my head...) .
Now i give them negetive z values and they are .. visible!

Ok, sry for wasting your time and thanks alot for help!;)
(hope next time my question will be a bit less stupid.. :D)