Search code examples
c#opengltransparencyopentk

opengl transparency not working as desired


I want to render a 3D scene whit some transparent objects in it, This is an example of my output (I am using opentk in c#) enter image description here
But the transparency is not working as i desired, I need something like this: enter image description here

Here is my settings in code:

  // 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);

Solution

  • I removed these two lines in GlInit() method

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

    and now I have this
    enter image description here