gpt4 book ai didi

c++ - 如何将 FBO 的文本附件绘制到默认帧缓冲区?

转载 作者:行者123 更新时间:2023-12-03 07:15:22 26 4
gpt4 key购买 nike

我想使用片段着色器输出到 FBO,然后将其纹理附件绘制到默认帧缓冲区。最终,我希望能够输出到一个 FBO,然后使用另一个着色器将其传递给另一个 FBO,依此类推。但我认为让它在默认帧缓冲区上工作是一个很好的第一步,特别是对于调试着色器的输出。
我不确定我做错了什么,我已经设置了一个小程序来演示它。它与 learnopengl.com 上的完整示例的代码基本相同:
https://learnopengl.com/Getting-started/Textures
该程序绑定(bind)到 FBO,然后使用着色器在单击光标的位置绘制一个点。如果未单击它,则会将其绘制到默认位置。然后它绑定(bind)到默认帧缓冲区并使用另一个着色器 (assign.frag.glsl) 来绘制 FBO 的纹理附件。
无论如何,这就是我想要它做的事情......问题是我只是得到一个黑屏,好像 FBO 的纹理附件从未被写入,或者它以某种方式未绑定(bind)到着色器中的统一 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);
}

最佳答案

您必须通过 GL_TEXTURE_MIN_FILTER 设置纹理缩小功能 ( glTexParameter ) :

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
如果您不生成 mipmaps (使用 glGenerateMipmap )设置 GL_TEXTURE_MIN_FILTER 很重要.由于默认过滤器是 GL_NEAREST_MIPMAP_LINEAR ,如果您没有将最小化函数更改为 GL_NEAREST,则纹理将是“Mipmap Incomplete”。或 GL_LINEAR .

你错过了乘法 gl_FragCoord.xy通过 rdx在分配着色器中: FragColor = texture2D(mytexture, gl_FragCoord.xy);
FragColor = texture2D(mytexture, gl_FragCoord.xy * rdx);

关于c++ - 如何将 FBO 的文本附件绘制到默认帧缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64723066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com