的OpenGL透明无法正常工作

时间:2019-03-10 18:45:51

标签: c# opengl transparency opentk

我想渲染一个3D场景,其中包含一些透明对象, 这是我的输出示例(我在c#中使用opentk) enter image description here
但是透明度无法达到我想要的效果,我需要像这样的东西:enter image description here

这是我在代码中的设置:

  // before draw transparent object
     GL.Enable(EnableCap.Blend);

  // after draw transparent object 
     GL.Disable(EnableCap.Blend);

  // @ GLInit
                GL.ClearColor(0.5f, 0.5f, 1f, 1f);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
                GL.ClearDepth(1);

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

                GL.DisableClientState(ArrayCap.NormalArray);
                GL.DisableClientState(ArrayCap.VertexArray);
                GL.DisableClientState(ArrayCap.TextureCoordArray);

                //GL.Enable(EnableCap.PolygonOffsetFill);
                GL.PolygonOffset(0.01f, 0.01f);

                GL.Enable(EnableCap.StencilTest);
                GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Replace);

                try
                {
                    GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.DstAlpha);
                    GL.BlendEquation(BlendEquationMode.FuncAdd);
                    //Gl.BlendEquation(BlendEquationMode.FuncAddExt);
                }
                catch { }


                GL.Enable(EnableCap.LineSmooth);
                GL.Enable(EnableCap.PointSmooth);
                GL.Enable(EnableCap.PolygonSmooth);
                GL.Disable(EnableCap.Dither);
                GL.ShadeModel(ShadingModel.Smooth);
                GL.Disable(EnableCap.Multisample);
                GL.LineWidth(0.5f);

                GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
                GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
                GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
                GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);
                GL.Hint(HintTarget.FragmentShaderDerivativeHint, HintMode.Nicest);

1 个答案:

答案 0 :(得分:0)

我将其删除到GlInit()的行中

                //GL.Enable(EnableCap.PointSmooth);
                //GL.Enable(EnableCap.PolygonSmooth);

现在我有了这个
enter image description here