c++ - 附上两张图片到fbo进行mrt渲染

标签 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);

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

最佳答案

I want to attach two render targets to the default fbo, so I can render to two targets at once.

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

关于c++ - 附上两张图片到fbo进行mrt渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20827976/

相关文章:

c++ - 什么时候应该存储函数的引用或指针?

c++ - 计算数值误差

c++ - 如何使用 operator<< 将文件对象提供给使用 << 的类?

c - 为什么 a++=5 是错误的?

c - 读取文件并在结构数组中写入/排序

c - #define 在 C 中的工作

c++ - 从opencv矩阵框架将数据从矩阵转换为数组

c++ - 如何在 Linux 上将 glut 与 Windows 交叉编译器一起使用?

c++ - 为什么我的有线球体在平移和改变摄像机角度时会变成椭球体?

c++ - 使用 OpenGL 计算着色器的朴素框模糊非常慢