簡體   English   中英

如何將 FBO 的文本附件繪制到默認幀緩沖區?

[英]How can I draw the text attachment of an FBO to the default frame buffer?

我想使用片段着色器輸出到 FBO,然后將其紋理附件繪制到默認幀緩沖區。 最終,我希望能夠輸出到一個 FBO,然后使用另一個着色器將其傳遞給另一個 FBO,依此類推。 但我認為讓它在默認幀緩沖區上工作是一個很好的第一步,特別是對於調試着色器的輸出。

我不確定我做錯了什么,我已經設置了一個小程序來演示它。 它與 learnopengl.com 上的完整示例大致相同: https ://learnopengl.com/Getting-started/Textures

該程序綁定到 FBO,然后使用着色器在單擊光標的位置繪制一個點。 如果沒有點擊它,它會將其繪制到默認位置。 然后它綁定到默認的幀緩沖區並使用另一個着色器(assign.frag.glsl)來繪制 FBO 的紋理附件。

無論如何,這就是我想要它做的......問題是我只是得到一個黑屏,好像FBO的紋理附件從未被寫入或者它以某種方式沒有被綁定到着色器中的統一sampler2D。 如果我注釋掉“assignProgram.use();” 線然后我看到我用鼠標單擊的點。 這讓我感到驚訝,因為光標着色器的輸出去了 FBO……但我在默認幀緩沖區中看到了它。 即使我注釋掉“glBindTexture(GL_TEXTURE_2D, tex0);” 我仍然可以看到那個點。

這是頂點着色器(即使我將它排除在程序之外也會出現問題):

#version 330 core
layout (location = 0) in vec3 aPos;

void main()
{
    gl_Position = vec4(aPos, 1.0);
}

光標點着色器:

#version 330 core

uniform vec2 cursor;
uniform float rdx;

out vec4 FragColor;

void main()
{

    float distance = distance(cursor, gl_FragCoord.xy) * rdx;
    distance = step(0.02, distance);
    vec3 colour = vec3(distance);

    FragColor = vec4(colour, 1.0f);
}

分配着色器:

#version 330 core

uniform sampler2D mytexture;
uniform float rdx;

out vec4 FragColor;

void main()
{
    FragColor = texture2D(mytexture, gl_FragCoord.xy);
    FragColor.a = 1.0f;
}

和 C++ 代碼:

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <learnopengl/shader_s.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 800;

int main()
{
    // glfw: initialize and configure
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    Shader cursorProgram("default.vert.glsl", "cursor.frag.glsl"); // you can name your shader files however you like
    Shader assignProgram("default.vert.glsl", "assign.frag.glsl"); // you can name your shader files however you like

    float vertices[] = {
       1.f,  1.f, 0.0f,  // top right
        1.f, -1.f, 0.0f,  // bottom right
        -1.f, -1.f, 0.0f,  // bottom left
        -1.f,  1.f, 0.0f,   // top left 
    };

    unsigned int indices[] = {  // note that we start from 0!
        0, 1, 3,   // first triangle
        1, 2, 3    // second triangle
    };

    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO); 

    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glBindVertexArray(0);

    unsigned int tex0, fbo0;

    glGenTextures(1, &tex0);
    glBindTexture(GL_TEXTURE_2D, tex0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glGenFramebuffers(1, &fbo0);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo0);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0, 0);
    glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
    glClearColor(0.f, 0.f, 0.f, 0.f);
    glClear(GL_COLOR_BUFFER_BIT);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        std::cout << "uh oh :(" << std::endl;


    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    float rdx = 1.f / SCR_WIDTH;

    while (!glfwWindowShouldClose(window))
    {
        // input
        processInput(window);


        // Draw to FBO
        glBindFramebuffer(GL_FRAMEBUFFER, fbo0);
        GLenum targets[1] = { GL_COLOR_ATTACHMENT0 };
        glDrawBuffers(1, &targets[0]);

        cursorProgram.use();
        cursorProgram.setFloat("rdx", rdx);
        
        if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
        {
            double x = 0, y = 0;
            glfwGetCursorPos(window, &x, &y);
            cursorProgram.setVec2("cursor", (float)x, SCR_HEIGHT - (float)y);
        }
        else
        {
            cursorProgram.setVec2("cursor", 0.f, 0.f);
        }

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


        // Draw fbo0 to default buffer
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        assignProgram.use();
        assignProgram.setFloat("rdx", rdx);
        assignProgram.setInt("mytexture", 0);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, tex0);

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        Sleep(16);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // optional: de-allocate all resources once they've outlived their purpose:
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);

    // glfw: terminate, clearing all previously allocated GLFW resources.
    glfwTerminate();
    return 0;
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

您必須通過glTexParameter設置紋理縮小功能( GL_TEXTURE_MIN_FILTER ):

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

如果您沒有生成的貼圖(與glGenerateMipmap )這是一套重要GL_TEXTURE_MIN_FILTER 由於默認過濾器是GL_NEAREST_MIPMAP_LINEAR ,如果您沒有將最小化函數更改為GL_NEARESTGL_LINEAR ,則紋理將為“Mipmap Incomplete”。


您錯過了在賦值着色器gl_FragCoord.xy乘以rdx

FragColor = texture2D(mytexture, gl_FragCoord.xy);

FragColor = texture2D(mytexture, gl_FragCoord.xy * rdx);

暫無
暫無

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

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