将两个图像附加到 FBO 以进行 MRT 渲染

attach two images to fbo for mrt rendering

本文关键字:FBO 渲染 MRT 图像 两个      更新时间:2023-10-16

我想将两个渲染目标附加到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