将两个图像附加到fbo以进行mrt渲染

时间:2013-12-29 17:23:09

标签: c++ c opengl framebuffer fbo

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

我只是得到一个黑色的空白屏幕

2 个答案:

答案 0 :(得分:1)

  

我想将两个渲染目标附加到默认fbo,因此我可以一次渲染到两个目标。

没有默认FBO。有一个默认的framebuffer ,这是你将上下文绑定到drawable提供的那个(通常是一个窗口)。您无法将纹理/渲染缓冲区附加到默认FB,GL_COLOR_ATTACHMENT对默认FB中的glDrawBuffer无效。因此,您的代码只会产生一些GL错误,并继续以单个渲染目标模式渲染,以前设置绘制缓冲区。

答案 1 :(得分:0)

由于没有默认的屏幕外fbo,您需要在代码中实现乒乓方案,然后使用FBO作为输入纹理来显示图形。您可以查看sgxperf中的简单代码,了解设置FBO并使用它渲染到显示的工作示例,请点击此处https://gist.github.com/prabindh/8173489

相关问题