【问题标题】:opengl transparency not working as desiredopengl 透明度无法按预期工作
【发布时间】:2019-03-10 18:45:50
【问题描述】:

我想渲染一个包含一些透明物体的 3D 场景, 这是我的输出示例(我在 c# 中使用 opentk)
但是透明度没有按我的意愿工作,我需要这样的东西:

这是我在代码中的设置:

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

【问题讨论】:

  • @Rabbid76 在用户可能旋转视图方向时对 3D 场景中的对象进行排序太难了,你确定吗?
  • 甚至可能是不可能的,比如第二张图,真正的顺序是什么?
  • 对于相交半透明对象的一般情况,您可能不得不求助于depth peeling(pdf)
  • @Rabbid76 "当你使用混合时你必须禁用深度测试,并且你必须按排序顺序绘制对象" 不需要禁用深度目标。实际上,如果您也想要场景中的不透明对象,这是一个坏主意。只需按任意顺序绘制不透明对象,从后到前绘制透明对象,并始终保持深度测试。
  • @mrbm: "我需要这样的东西:" 这是手绘图吗?因为如果是这样,它就没有“真正的顺序”。如果你用手画一些东西,你可以随心所欲地画它。如果使用基于计算机的渲染过程,则必须遵守一致的规则

标签: c# opengl transparency opentk


【解决方案1】:

我在GlInit()方法中删除了这两行

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

现在我有了这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多