简体   繁体   English

如何在OpenTK中使对象透明

[英]How to make objects transparent in OpenTK

I'm using OpenTK and C#, I have defined a plane in 3D space as follows: 我正在使用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();

Can anyone please help me to make the plane transparent? 谁能帮助我让飞机变得透明?

So you want something like this? 所以你想要这样的东西?

图片

There are a lot of things to take care of to get there. 有很多事情需要照顾到那里。

It all starts with a Color object that contains an alpha value<255. 这一切都始于一个包含alpha值<255的Color对象。 For example Color.FromArgb(85, Color.Turquoise) for the sphere below. 例如Color.FromArgb(85, Color.Turquoise)下面的球体的Color.FromArgb(85, Color.Turquoise)

The main render class, sets up the camera view and renders all the lights, and then renders all the objects in the scene: 主渲染类,设置摄像机视图并渲染所有灯光,然后渲染场景中的所有对象:

    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<objects.Count; i++)
        {
            GL.PushMatrix();
            objects[i].Render();
            GL.PopMatrix();
        }
        control.SwapBuffers();
    }

Then each object has a base rendering code Render() , which calls more specialized code Draw() 然后每个对象都有一个基本渲染代码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
    }

and for example to draw a quad surface you have 例如,绘制一个四边形表面

    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<nodes.Length; i++)
        {
            GL.Vertex3(nodes[i]);
        }
        GL.End();
        // special code when translucent
        if (color.A<255)
        {
            GL.Disable(EnableCap.Blend);
            GL.DepthMask(true);
        }
    }

also the code to draw the outline of a quad to be called after DrawQaud() 还有用于绘制在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<nodes.Length; i++)
        {
            GL.Vertex3(nodes[i]);
        }
        GL.End();
        if (color.A<255)
        {
            GL.Disable(EnableCap.Blend);
            GL.DepthMask(true);
        }
    }

Finally I found solution of my question: 最后我找到了问题的解决方案:

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

Simply use GL.Color4 instead of GL.Color3. 只需使用GL.Color4而不是GL.Color3。 The 4th value will be the alpha 第四个值是alpha

In computing, Transparency effects are fained using color blending .For The special case of transprency, we talk about Àlpha Blending` 在计算中,使用颜色混合来消除透明效果。对于透明度的特殊情况,我们谈论Àlpha混合`

For transparency the blending factor is usualy stored in the 4th component of the colour (the A in RGBA) which stands for alpha . 对于透明度,混合因子通常存储在颜色的第4个分量(RGBA中的A )中,代表alpha So you have to set all your colors with it. 所以你必须用它设置所有的颜色。

example for half transparent blue (like glass): 半透明蓝色(如玻璃)的示例:

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

In OpenGL, blending have to be activated with the following command, which enable a supplementary stage on the rendering pipeline. 在OpenGL中,必须使用以下命令激活混合,这将在渲染管道上启用补充阶段。

GL.Enable( EnableCap.Blend );

Then, because blending could be used to mix colors for other purpose than transparency, you have to specify the blending function to use. 然后,因为混合可以用于混合颜色而不是透明度,您必须指定要使用的混合函数 Here is the common function for transparency: 以下是透明度的常用功能:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM