I want to make the background of OpenGL that I draw in NSView transparent.

Asked 2 years ago, Updated 2 years ago, 127 views

I am trying to draw using OpenTK on my Mac.

I'm trying to make the background transparent based on the sample, but I can't see any change while it's black.
Here's the code:

gameWindow=newMonoMacGameView(new CoreGraphics.CGRect(x,y,width,height);
       // Wire-up any required Game events
        gameWindow.Load+=(sender,e)=>
        {
            // TODO: Initialize settings, load textures and sounds here
        };

        gameWindow.Resize+=(sender,e)=>
        {
            // Adjust the GL view to be the same size as the window
            GL.Viewport(0,0, gameWindow.Size.Width, gameWindow.Size.Height);
        };

        gameWindow.UpdateFrame+=(sender,e)=>
        {
            // TODO:Add any game logic or physics
        };

        gameWindow.RenderFrame+=(sender,e)=>
        {
            GL.Enable (EnableCap.AlphaTest);
            GL.Enable (EnableCap.Blend);

            GL.ClearColor (0.0f, 0.0f, 0.0f);

            // Setup buffer
            GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.PushMatrix();

            GL.MatrixMode (MatrixMode.Projection);

            // Draw a simple triangle
            GL.LoadIdentity();
            GL. Ortho (-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
            GL.Begin (BeginMode.Triangles);
            GL.Color3 (Color.MidnightBlue);
            GL.Vertex2 (-1.0f, 1.0f);
            GL.Color3 (Color.SpringGreen);
            GL.Vertex2 (0.0f, -1.0f);
            // GL.Color3 (Color.Ivory);
            GL.Color 4 (0.0f, 0.5f, 0.5f, 0.1f);
            GL.Vertex2 (1.0f, 1.0f);
            GL.End();

            GL.PopMatrix();
        };

It may be NSView's setting, but I don't know what to add.
I tried the following code, but then the GL drawing disappeared.

CALayer viewLayer=CALayer.Create();
viewLayer.BackgroundColor=NSColor.Clear.CGColor;
renderView.WantsLayer=false;
renderView.Layer=viewLayer;
*RenderView is the NSView that draws GL.

I would like your advice.

c# macos opengl

2022-09-30 10:26

1 Answers

I have never used OpenTK, but if you want OpenGL to make the background color transparent, set glColorMask and then glClear.

Typical OpenGL Description

glColorMask(true, true, true, true);//Alpha also draws settings
glClear(GL_COLOR_BUFFER_BIT);
glColorMask(true, true, true, false); // revert

I don't know if it works, but I wonder if OpenTK writes like this.I added one line before and after GL.Clear.

GL.ColorMask(true, true, true, true);//Alpha also draws settings
GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ColorMask(true, true, true, false); // Revert


2022-09-30 10:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.