Jump to content
  • Advertisement
Sign in to follow this  
kenjinsakura

OpenGL OpenGL ES orthographic depth rendering problem

This topic is 3109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm coding in Java with the android opengl libraries and I have a problem. Using orthographic projection, the layering of the objects seem to be depending on the order they are drawn and not on their z-values. This would be fine if I wanted a 2d environment but I'm making a 3d environment.

Here's some code:

//this is the initialization function
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//Initialize Here
gl.glEnable(GL10.GL_TEXTURE_2D);//Enable Texture Mapping
gl.glEnable(GL10.GL_DEPTH_TEST);//Enable depth testing

gl.glShadeModel(GL10.GL_SMOOTH);//Enable Smooth Shading

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

//Enable the vertex and texture state
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);


gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();

//set the projection
gl.glOrthof(-screenWidth/70, screenWidth/70, -screenHeight/70, screenHeight/70, 0, -200);
//set the viewport
gl.glViewport(0, 0, screenWidth, screenHeight);

gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix

}

//The drawing function
public void onDrawFrame(GL10 gl) {
//define color of clipping wall
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//clear the buffer to display the clear color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearDepthf(1.0f);

//select model matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);
//reset model view matrix
gl.glLoadIdentity();

//I draw here using glDrawElements calls
}



I think I'm missing an initialization call or something, but I can't figure out what. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Based on what I see here it doesn't look like you're missing anything. As long as you have depth test enabled it should work (unless you're doing something else that you aren't showing).

I'd say either post more of your code, or maybe an image to show your issue?

Edit: also anytime you're experiencing something weird always check for opengl errors at least at the end of each frame.

Share this post


Link to post
Share on other sites
AFAIK The code I omitted has to do with loading textures and transformations only.

Here's a screenshot:


The panels are arranged in a circle on the x-z plane and the camera is slightly elevated looking down on the panels.

If you look at the left-hand side, the "Titanic" panel is obscured by the "Star Wars" panel which is supposed to be behind it. I drew the star wars panel first, and the rest in counter-clockwise order, ending with the WildWildWest panel partially seen behind the starwars panel. The renderer is giving higher priority to the earlier object drawn(Which is the reverse of what I would expect if depth testing wasn't enabled).

If I change a single line of code, the glOrthof call to a gluPerspective call instead, the panels display properly.

Share this post


Link to post
Share on other sites
I'm almost wondering if you've got your scene flipped. From what I can tell in the image it's not that the depth test isn't working, but it looks like items in the back are always overtop of items in the front.

Quote:

gl.glOrthof(-screenWidth/70, screenWidth/70, -screenHeight/70, screenHeight/70, 0, -200);


I think you want the near/far value to be something like 0, +200. If you read the glOrtho doc it says that "if the near/far value is negative the plane is to be behind the viewer" and "-farval is the location of the negative clipping plane".

Flip your farval to +200 and shift everything to the range from 0 (camera) to -200(far plane), and I think that should work.

Share this post


Link to post
Share on other sites
Holy smokes, good catch man. That -200 was supposed to be positive. I must have been confused thinking "negative z is into the screen..." I changed that and it displayed everything properly. Thanks a lot!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!