-1

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);
mrbm
  • 1,136
  • 1
  • 12
  • 36
  • 1
    @Rabbid76 sorting objects in 3D scene when user may rotate the view orientation is too hard, are you sure? – mrbm Mar 10 '19 at 18:50
  • 1
    It may be even impossible, for example in the second image, what's the true order? – mrbm Mar 10 '19 at 18:52
  • For the general case of intersecting semi-opaque objects you may have to resort to [depth peeling(pdf)](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.18.9286&rep=rep1&type=pdf). – G.M. Mar 10 '19 at 19:15
  • what about GlInit(): GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.DstAlpha); GL.BlendEquation(BlendEquationMode.FuncAdd); – mrbm Mar 10 '19 at 19:31
  • 2
    @Rabbid76 "You have to disable the depth test when you us blending and you have to draw the objects in sorted order" No need to disable depth dest. Actually, a bad idea if you want opaque objects in the scene too. Just draw opaque objects in any order and transparent ones back-to-front, and leave depth testing on all the time. – derhass Mar 10 '19 at 19:33
  • 1
    @mrbm: "*I need something like this:*" Is that a hand-drawn diagram? Because if so, it doesn't have a "true order". If you draw something by hand, you can draw it however you like. If you use a computer-based rendering process, you have to abide by consistent *rules*. – Nicol Bolas Mar 10 '19 at 19:41
  • I posted a new result, without disabling depth test or change the order, It seems true, Isn't? – mrbm Mar 10 '19 at 19:46

1 Answers1

0

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

Community
  • 1
  • 1
mrbm
  • 1,136
  • 1
  • 12
  • 36