简体   繁体   中英

GLFW magenta color causes unexpected behavior

When I create a window with GLFW (on a Windows operating system) and set GLFW_TRANSPARENT_FRAMEBUFFER to 1 via glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, 1); I can't draw a rect with a magenta color glColor3f(1.f, 0.f, 1.f); . This color causes the rect to be transparent and makes me able to click through this part of the window.

I did not expect this behavior and also did not find any documentation that explains this.

I used the example code on the GLFW homepage and added a gray, yellow and magenta rect of which only the gray and yellow rect produce expected behavior.

Screenshot that shows the window

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    // ADDED THIS LINE
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, 1);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        // ADDED THE FOLLOWING LINES
        // background
        glColor3f(0.1f, 0.1f, 0.1f); // gray
        glRectf(-0.75f, 0.75f, 0.75f, -0.75f);

        // unexpected behavior: makes part of the window transparent and click through
        glColor3f(1.f, 0.f, 1.f); // magenta
        glRectf(-0.1f, -0.1f, 0, 0);

        // draws yellow rect
        glColor3f(1.0f, 1.f, 0.f); // yellow
        glRectf(-0.1f, 0.1f, 0, 0);


        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

The magenta color gets drawn correctly when GLFW_TRANSPARENT_FRAMEBUFFER is set to 0 .

The reason for this behavior is that the current transparency color key equals magenta. All pixels drawn in this color will be transparent.

In order to change the color key (on a Windows operating system) you can call the function SetLayeredWindowAttributes .

This example will change the color key to red: SetLayeredWindowAttributes(glfwGetWin32Window(window), RGB(255, 0, 0), NULL, LWA_COLORKEY);

The documentation of the function states the following about the color key:

crKey

Type: COLORREF

A COLORREF structure that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.

In order to call glfwGetWin32Window you need to define the macro #define GLFW_EXPOSE_NATIVE_WIN32 and include #include <GLFW/glfw3native.h> as explained here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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