简体   繁体   中英

How to make the background transparent in fullscreen mode (in OpenGL)?

I want to create a transparent background, but it only works if the 4th (monitor) parameter is set to NULL. If I modify the window to be fullscreen (as the code shows below) the background just turns to black without any transparency. Is there any solution?

Note: I only want the background to be transparent. Not the whole window with its content.

//at configuring  
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, 1);       

...  

//at window creation  
GLFWmonitor* primary = glfwGetPrimaryMonitor();  
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Application",primary, NULL);

...

//in render loop  
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

I tryed something and is look like transparent is working by default if you have transparent supported on OS theme (some windows 7 themes are just solid colors so be carefoul because your OS theme will disable OS transparence support).
file: 文件:

// INCLUDE
#include "main.h"

// DECLARATION
static void key_event(GLFWwindow*, int, int, int, int);

// GLOBAL VARIABLES
int app_width = 512;
int app_height = 256;

// RUN
void main(void)
{
    if (!glfwInit()) { exit(EXIT_FAILURE); }

    //glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
    glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);

    GLFWmonitor* monitor = glfwGetPrimaryMonitor();
    const GLFWvidmode* mode = glfwGetVideoMode(monitor);
    GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, "GLFW Test", monitor, NULL);

    glfwSetKeyCallback(window, key_event);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    while (!glfwWindowShouldClose(window))
    {
        glfwGetFramebufferSize(window, &app_width, &app_height);
        glViewport(0, 0, app_width, app_height);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        // TO DO
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    exit(EXIT_SUCCESS);
}

static void key_event(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { 
        glfwSetWindowShouldClose(window, GLFW_TRUE); 
        glfwTerminate();
        exit(EXIT_SUCCESS);
    }
}


file: 文件:

#pragma once

// INCLUDE
#include <iostream>
#include <conio.h>
#include <GLFW/glfw3.h>

For me is working. (I see if the have not full value, the background will not be complet transparente... 是否没有完整的值,背景不会完全透明......

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