简体   繁体   中英

Strange blending effect in OpenGL ES 2.0 using GLKit

This happens on GLKit with MonoTouch 5.3, but I think the problem may be of general OpenGL ES 2.0 nature.

I have three faces, one of them red, one green and one blue, all with an alpha value of 1.0, so they should be opaque. When they are rendered on the black background, everything is okay. If the green face is in front of the others, everything is okay as well. But if

  • the red face is in front of the green
  • or the blue face is in front of one of the others

the foreground color is not rendered, but the background face is fully visible. This seems to be some kind of blending effect, but I don't see anything special in my code, and I have tried out several things like glBlendFunc , but it didn't change anything.

I could post my code, but since the code is quite simple, I thought perhaps someone knows the answer immediately.

Update: As this seems to be a Depth-Sorting issue, here are some important parts of the code:

        var aContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
        var view = this.View as GLKView;
        view.Delegate = new GenericGLKViewDelegate(this);
        view.Context = aContext;

        view.DrawableColorFormat = GLKViewDrawableColorFormat.RGBA8888;
        view.DrawableDepthFormat = GLKViewDrawableDepthFormat.Format16;
        view.DrawableMultisample = GLKViewDrawableMultisample.Sample4x;

here is the DrawInRect method:

        _baseEffect.PrepareToDraw();

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
        GL.EnableVertexAttribArray((int)GLKVertexAttrib.Color);

        GL.VertexAttribPointer((int)GLKVertexAttrib.Position, 3, VertexAttribPointerType.Float, false, 0, _squareVertices);
        GL.VertexAttribPointer((int)GLKVertexAttrib.Color, 4, VertexAttribPointerType.UnsignedByte, true, 0, _squareColors);

        GL.DrawArrays(BeginMode.TriangleStrip, 0, 9);

        GL.DisableVertexAttribArray((int)GLKVertexAttrib.Position);
        GL.DisableVertexAttribArray((int)GLKVertexAttrib.Color);

I tried every possible combination of ColorFormat , DepthFormat and Multisample , but it's still the same.

Solution:

I was missing some calls to enable the depth buffer, these calls were missing in the ViewDidLoad method:

        GL.ClearDepth(30);
        GL.Enable(EnableCap.DepthTest);
        GL.DepthFunc(DepthFunction.Lequal);

This does not sound like a blending issue- rather a depth-sorting issue.

Is the depth buffer enabled? Are you clearing it with each frame?

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