如何在OpenTK中使对象透明

我正在使用OpenTK和C#,我在3D空间中定义了一个平面,如下所示:

GL.Begin(BeginMode.Quads); GL.Color3(Color.Magenta); GL.Vertex3(-100.0f, -25.0f, -150.0f); GL.Vertex3(-100.0f, -25.0f, 150.0f); GL.Vertex3( 200.0f, -25.0f, 100.0f); GL.Vertex3( 200.0f, -25.0f, -100.0f); GL.End(); 

谁能帮助我让飞机透明?

所以你想要这样的东西?

图片

有很多事情需要照顾到那里。

这一切都始于一个包含alpha值<255的Color对象。 例如Color.FromArgb(85, Color.Turquoise)下面的球体的Color.FromArgb(85, Color.Turquoise)

主渲染类,设置摄像机视图并渲染所有灯光,然后渲染场景中的所有对象:

  public void RenderOnView(GLControl control) { control.MakeCurrent(); var camera=views[control]; GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit); GL.Disable(EnableCap.CullFace); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); camera.LookThrough(); if (EnableLights) { GL.LightModel(LightModelParameter.LightModelAmbient, new[] { 0.2f, 0.2f, 0.2f, 1f }); GL.LightModel(LightModelParameter.LightModelLocalViewer, 1); GL.Enable(EnableCap.Lighting); foreach (var light in lights) { light.Render(); } } else { GL.Disable(EnableCap.Lighting); GL.ShadeModel(ShadingModel.Flat); } GL.Enable(EnableCap.LineSmooth); // This is Optional GL.Enable(EnableCap.Normalize); // These is critical to have GL.Enable(EnableCap.RescaleNormal); for (int i=0; i 

然后每个对象都有一个基本渲染代码Render() ,它调用更专业的代码Draw()

  public void Render() { if (Shading==ShadingModel.Smooth) { GL.Enable(EnableCap.ColorMaterial); GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse); GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Specular, SpecularColor); GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Emission, EmissionColor); GL.Material(MaterialFace.FrontAndBack, MaterialParameter.Shininess, Shinyness); GL.Enable(EnableCap.Lighting); } else { GL.Disable(EnableCap.ColorMaterial); GL.Disable(EnableCap.Lighting); } GL.ShadeModel(Shading); GL.Translate(Position); GL.Scale(Scale, Scale, Scale); Draw(); // Draws triangles and quads to make up a shape } 

例如,绘制一个四边形表面

  protected void DrawQuad(Color color, params Vector3[] nodes) { GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Enable(EnableCap.PolygonOffsetFill); // special code when translucent if (color.A<255) { GL.Enable(EnableCap.Blend); GL.DepthMask(false); } GL.Begin(PrimitiveType.Quads); GL.Color4(color); //this is where the color with alpha is used for (int i=0; i 

还有用于绘制在DrawQaud()之后调用的四边形轮廓的代码

  protected void DrawLineLoop(Color color, params Vector3[] nodes) { GL.Disable(EnableCap.PolygonOffsetFill); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); if (color.A<255) { GL.Enable(EnableCap.Blend); GL.DepthMask(false); } GL.Begin(PrimitiveType.LineLoop); GL.Color4(color); for (int i=0; i 

最后我找到了问题的解决方案:

 GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One); GL.Enable(EnableCap.Blend); //Definition of Plane GL.Begin(BeginMode.Quads); GL.Color4(0, 0.2, 1, 0.5); GL.Vertex3(-100.0f, -25.0f, -150.0f); GL.Vertex3(-100.0f, -25.0f, 150.0f); GL.Vertex3( 200.0f, -25.0f, 100.0f); GL.Vertex3( 200.0f, -25.0f, -100.0f); GL.End(); GL.Disable(EnableCap.Blend); 

只需使用GL.Color4而不是GL.Color3。 第四个值是alpha

在计算中,使用颜色混合来消除透明效果。对于透明度的特殊情况,我们谈论Àlpha混合`

对于透明度,混合因子通常存储在颜色的第4个分量(RGBA中的A )中,代表alpha 。 所以你必须用它设置所有的颜色。

半透明蓝色(如玻璃)的示例:

 GL.Color4(0,0,1,0.5f); 

在OpenGL中,必须使用以下命令激活混合,这将在渲染管道上启用补充阶段。

 GL.Enable( EnableCap.Blend ); 

然后,因为混合可以用于混合颜色而不是透明度,您必须指定要使用的混合函数 。 以下是透明度的常用function:

 GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); 

http://www.opentk.com/doc/chapter/2/opengl/fragment-ops/blending