簡體   English   中英

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

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

我想創建一個透明背景,但它只有在第 4 個(監視器)參數設置為 NULL 時才有效。 如果我將 window 修改為全屏(如下面的代碼所示),背景就會變成黑色,沒有任何透明度。 有什么解決辦法嗎?

注意:我只希望背景是透明的。 不是整個 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);

我嘗試了一些東西,如果您在操作系統主題上支持透明,默認情況下看起來透明正在工作(一些 windows 7 主題只是堅實的 colors 所以要小心,因為您的操作系統主題將禁用操作系統透明支持)。
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文件:

#pragma once

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

對我來說正在工作。 (我看glClearColor是否沒有完整的0.0f值,背景不會完全透明......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM