简体   繁体   English

如何在全屏模式下(在 OpenGL 中)使背景透明?

[英]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.我想创建一个透明背景,但它只有在第 4 个(监视器)参数设置为 NULL 时才有效。 If I modify the window to be fullscreen (as the code shows below) the background just turns to black without any transparency.如果我将 window 修改为全屏(如下面的代码所示),背景就会变成黑色,没有任何透明度。 Is there any solution?有什么解决办法吗?

Note: I only want the background to be transparent.注意:我只希望背景是透明的。 Not the whole window with its content.不是整个 window 及其内容。

//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).我尝试了一些东西,如果您在操作系统主题上支持透明,默认情况下看起来透明正在工作(一些 windows 7 主题只是坚实的 colors 所以要小心,因为您的操作系统主题将禁用操作系统透明支持)。
main.cpp file: main.cpp文件:

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


main.h file: main.h文件:

#pragma once

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

For me is working.对我来说正在工作。 (I see if the glClearColor have not full 0.0f value, the background will not be complet transparente... (我看glClearColor是否没有完整的0.0f值,背景不会完全透明......

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

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