我想使用片段着色器输出到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);
}
答案 0 :(得分:2)
您必须通过glTexParameter
设置纹理最小化功能(GL_TEXTURE_MIN_FILTER
):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
如果您没有生成mipmaps(使用glGenerateMipmap
),则设置GL_TEXTURE_MIN_FILTER
很重要。由于默认过滤器为GL_NEAREST_MIPMAP_LINEAR
,因此如果您未将最小化功能更改为GL_NEAREST
或GL_LINEAR
,则纹理将为“ Mipmap Incomplete”。
您错过了在作业着色器中将gl_FragCoord.xy
乘以rdx
:
FragColor = texture2D(mytexture, gl_FragCoord.xy);
FragColor = texture2D(mytexture, gl_FragCoord.xy * rdx);