简体   繁体   中英

OpenGL ES 1.1 depth buffer not working using MonoTouch on iOS 5

I can't get the depth buffer to work using MonoTouch on iOS 5. I'm using the standard MonoTouch "OpenGL Application" template and added this code to test the depth buffer (OpenGL ES 1.1):

GL.Enable(All.DepthTest);
GL.DepthFunc(All.Greater);
GL.DepthMask(true);

// Shape A
GL.DrawArrays (All.TriangleStrip, 0, 4); 

// Shape B (should be behind Shape A)
GL.Translate (.5f,.5f,.5f);
GL.DrawArrays (All.TriangleStrip, 0, 4); 

// Shape C (should be in front of Shape A)
GL.Translate (-1f,-1f,-1f);
GL.DrawArrays (All.TriangleStrip, 0, 4);

Both shape B and C are drawn in front of shape A. The same thing happens even with "GL.DepthFunc(All.Never)". The depth buffer is completely ignored. I also tried manually creating a depth buffer (instead of relying on the MonoTouch/OpenTK "iPhoneOSGameView" to create one for me) using this code:

protected override void CreateFrameBuffer()
{
    base.CreateFrameBuffer();    
    uint depthbuffer=0;
    GL.Oes.GenRenderbuffers (1, ref depthbuffer);
    GL.Oes.BindFramebuffer (All.RenderbufferOes, depthbuffer);
    GL.Oes.RenderbufferStorage (All.RenderbufferOes, All.DepthComponent16Oes, (int) 768, (int) 1024);
    GL.Oes.FramebufferRenderbuffer (All.FramebufferOes, All.DepthAttachmentOes, All.RenderbufferOes, depthbuffer);
}

It's still not working. Do you have any idea how I can get the depth buffer to work? Am I missing something?

Thanks to the NeHe-Lesson holmes posted, I found a solution: I use the methods in "OpenTK.Graphics.ES20.GL" instead of those in "OpenTK.Graphics.ES11.GL.Oes" to create the depth buffer. After the buffer is created, I can use the standard OpenGL 1.1 methods in "OpenTK.Graphics.ES11" for the rest of my program. Here's the code:

OpenTK.Graphics.ES20.GL.GenRenderbuffers (1,ref depthRenderBuffer);
OpenTK.Graphics.ES20.GL.BindRenderbuffer (OpenTK.Graphics.ES20.All.Renderbuffer, depthRenderBuffer);
OpenTK.Graphics.ES20.GL.RenderbufferStorage (OpenTK.Graphics.ES20.All.Renderbuffer, OpenTK.Graphics.ES20.All.DepthComponent16, Size.Width, Size.Height);
OpenTK.Graphics.ES20.GL.FramebufferRenderbuffer (OpenTK.Graphics.ES20.All.Framebuffer, OpenTK.Graphics.ES20.All.DepthAttachment, OpenTK.Graphics.ES20.All.Renderbuffer, depthRenderBuffer);

Try the following.

GL.ClearDepth(1.0f);    
GL.Enable(All.DepthTest);   
GL.DepthFunc( All.Lequal);  

Also do not forget to clear the depth on each render.

GL.Clear((int)(All.ColorBufferBit | All.DepthBufferBit));

I posted my code for the NeHe lessons using MonoTouch on Github while I was learning. This is the first lesson that requires depth testing and my give you some more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM