PDA

View Full Version : depth testing in iphone opengl



quint- iphone designer
01-22-2010, 05:02 AM
Hi all,


I am trying to learn 3d rendering in iphone opengl. I am using xcode 3.1.4 and opengl ES 1.1.

I try to draw two cube , one is near to eye and other one is behind the first one in the negative z plane... I render the front cube first and then the back cube. But Always my back cube is showing in the front because it is rendering second. Ofcourse trying to render in reverse order should cure the problem. But since we have depth testing in iphone opengl I tried to enable it. But there I am having some trouble.


These are the settings I have done.





In my draw (render()) function..



glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepthf(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthMask( GL_TRUE );
glDepthFunc(GL_EQUAL);


In my resizeFromLayer function in ES1Renderer.m



glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);


In init function in ES1Renderer.m



glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);



I tried to render after this but this time nothing is drawing and the scene is blank. Then I comment this line from init


glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

Now Two cubes are rendering but just the same way as it is rendered before enabling depth testing.. The cube which should be behind is shown in front obstructing the other cube. What am i missing...Please anybody help me...

quint- iphone designer
01-22-2010, 05:12 AM
I have done one more thing to see whether depth buffer value..


int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
NSLog(@"%i bits depth", depth);

And the NSLog output is :


0 bits depth

So depth buffer value is zero ... I doesnt look good.

scratt
01-22-2010, 05:21 AM
Why not grab one of the widely available EAGLView (http://code.google.com/p/oolongengine/source/browse/trunk/Oolong%20Engine2/Renderer/Core/GraphicsDevice/OS2x/EAGLView.m?r=44) classes from Apple, or an engine like Oolong (http://code.google.com/p/oolongengine).

Pretty much all you need to know is in those files.