将两个图像附加到 FBO 以进行 MRT 渲染
attach two images to fbo for mrt rendering
我想将两个渲染目标附加到fbo,这样我就可以一次渲染到两个目标。我有一个接受两个渲染目标的函数。
void render(struct glhexbutton *_this, GLuint target0, GLuint target1)
{
GLenum buffers[2];
GLfloat vertices[] = { -1.0, -1.0,
-1.0, 1.0,
1.0, -1.0,
1.0, 1.0 };
GLubyte indices[] = { 0, 1, 2,
1, 2, 3 };
GLuint fbo;
struct {
int x;
int y;
} position;
/*Load variables into shader program*/
glUniform4f(glhexbutton_static.uni_address, 1.0, 1.0, 1.0, 1.0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _this->data.texture);
glUniform1i(glhexbutton_static.uni_texture, /*GL_TEXTURE*/0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(_this->data.static_data->program);
/*create fbo and attach render targets*/
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
target0, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D,
target1, 0);
/*Set the viewport of the fbo*/
position.x = glutGet(GLUT_WINDOW_WIDTH)*_this->data.position.relative.x+_this->data.position.offset.x;
position.y = glutGet(GLUT_WINDOW_HEIGHT)*_this->data.position.relative.y+_this->data.position.offset.y;
glViewport(position.x, position.y,
_this->data.size.width, _this->data.size.height);
buffers[0] = GL_COLOR_ATTACHMENT0;
buffers[1] = GL_COLOR_ATTACHMENT1;
glDrawBuffers(2, buffers);
glEnableVertexAttribArray(_this->data.static_data->att_coord);
glVertexAttribPointer(_this->data.static_data->att_coord,
2,
GL_FLOAT,
GL_FALSE,
0,
vertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
glDisableVertexAttribArray(_this->data.static_data->att_coord);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(GL_FRAMEBUFFER, &fbo);
}
我的顶点着色器:
#version 330
attribute vec2 coord;
varying vec2 f_coord;
void main() {
gl_Position = vec4(coord, 0.0, 1.0);
f_coord = (1.0+coord)/2.0;
}
我的像素/片段着色器:
#version 330
uniform sampler2D texture;
uniform vec4 address;
varying vec2 f_coord;
void main() {
gl_FragData[0]=texture2D(texture,f_coord);
gl_FragData[1]=address;
if(texture2D(texture,f_coord).a == 0.0)
discard;
}
当我打电话时
render(glhexbutton, 0, texture);
或
render(glhexbutton, texture, 0);
我只是得到一个黑色的空白屏幕
我想将两个渲染目标附加到默认 fbo,以便我可以一次渲染到两个目标。
没有默认的 FBO。有一个默认的帧缓冲区,这是将上下文绑定到的可绘制对象(通常是窗口)提供的帧缓冲区。您无法将纹理/渲染缓冲区附加到默认 FB,并且GL_COLOR_ATTACHMENT
对默认 FB 中的glDrawBuffer
无效。因此,您的代码只会产生一些 GL 错误,并继续以单个呈现目标模式呈现,呈现到之前设置的绘制缓冲区。
由于没有默认的屏幕外 fbo,因此需要在代码中实现乒乓球方案,然后将 FBO 用作要显示的绘图的输入纹理。您可以查看 sgxperf 中的简单代码,以获取设置 FBO 并使用它来渲染到显示器的工作示例,要点在这里https://gist.github.com/prabindh/8173489
相关文章:
- OpenGL将纹理四边形渲染为(0,0)
- 按需渲染帧
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- dx11 渲染到纹理仅显示透明颜色
- 在 opengl 中渲染 obj 文件时出现黑窗口
- Qt 如何渲染 mainwidget
- C++ SDL2:如何将矩形渲染到多个视口中
- 纹理单位重叠?渲染了错误的纹理
- Qt OpenGL 渲染到纹理性能问题
- 从 FBO 渲染时在表面上重复纹理
- 设置OpenGL FBO以渲染Cubemap深度纹理时,为什么我会得到不完整的附件
- 用于在许多Qopenglwidget中显示的FBO渲染的全局OpenGL上下文
- 将两个图像附加到 FBO 以进行 MRT 渲染
- OpenGL FBO 不会渲染为纹理 (C++)
- 屏幕外渲染 OpenGL 4.5 多样本 FBO
- eglmakeccurrent的上下文只渲染为fbo,使用什么表面?
- OpenGL - FBO在NVIDIA卡上渲染纹理时出现黑屏
- OpenGL渲染到纹理(FBO)不起作用
- 用FBO绘制到渲染缓冲区