我正在尝试渲染到帧缓冲区(具有与窗口/屏幕相同的大小)以进行后期处理。一切都是二维的,因此应该不会给您带来太多麻烦。直接渲染到屏幕上可以正常工作。但是我只能得到清晰的颜色和空的缓冲区。将任何内容渲染到其中的唯一方法是渲染一个矩形,其位置的缓冲区的确切大小为(0,0),这很奇怪。
我尝试在缓冲区的随机位置进行渲染,但没有任何效果。在屏幕上显示/渲染缓冲区。
这是我创建缓冲区的方式:
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
std::cout << "frame buffer fbo: " << fbo << std::endl;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "error creating screen buffer width: " << width << " height: " << height << std::endl;
}
else {
std::cout << "created screen buffer width: " << width << " height: " << height << std::endl;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
顶点着色器:
#version 330 core
layout (location = 0) in vec2 vertex;
uniform mat4 projection;
void main() {
gl_Position = projection * vec4(vertex.x, vertex.y, 0.0f, 1.0f);
}
片段着色器:
#version 330 core
uniform vec4 in_color;
out vec4 FragColor ;
void main() {
FragColor = in_color;
}
用于渲染的投影矩阵:
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(this->width), static_cast<GLfloat>(this->height), 0.0f, -1.0f, 1.0f);
要渲染到缓冲区中的代码:
// binding and clearing the buffer to render into it
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, width, height);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// render the rectangle
renderer->drawRectangle(0, 0, 10, 10, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, resolutionWidth, resolutionHeight);
// rendering main screen showing the quad
结果是这样的(缓冲区以一半大小呈现到屏幕): Empty Quad/framebuffer showing clear color
答案 0 :(得分:0)
在实施此课程时,我一直在关注本教程:https://learnopengl.com/Advanced-OpenGL/Framebuffers
作为解决方案,我只是简单地更改了屏幕着色器以接受我的顶点/纹理数据。然后我注意到实际的缓冲区数据非常好...
顶点着色器:
#version 330 core
layout (location = 0) in vec4 aPos;
out vec2 TexCoords;
void main()
{
TexCoords = aPos.zw;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}
片段着色器:
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D screenTexture;
void main()
{
FragColor = texture(screenTexture, TexCoords);
}