繁体   English   中英

将两个图像附加到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。 有一个默认的framebuffer ,它是将上下文绑定到(通常是一个窗口)的drawable提供的。 您不能将纹理/渲染缓冲区附加到默认FB,并且GL_COLOR_ATTACHMENT对于默认FB中的glDrawBuffer无效。 因此,您的代码只会产生一些GL错误,并继续在单个渲染目标模式下渲染到以前设置的绘制缓冲区。

由于没有默认的屏幕外fbo,因此需要在代码中实现乒乓方案,然后将FBO用作输入纹理以显示图形。 您可以在sgxperf中查看简单的代码,以获取设置FBO并使用它呈现到显示器的工作示例,要点https://gist.github.com/prabindh/8173489参见https://gist.github.com/prabindh/8173489

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM