1

我正在使用 OpenGL ES 在 iOS 上编程,并且正在尝试实现 MSAA。我成功地做到了这一点。我的屏幕出来是抗锯齿的。但是,每一帧都会将以下字符串记录到我的输出中:“无法制作完整的帧缓冲区对象 8cd6”。0x8CD6 表示 GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT。所以,虽然它正在工作,但我不知道该怎么做。我已经阅读了我的代码一百遍,并且没有发现任何错误。

在第一次调用 Render() 之前但在 Setup() 完成之后记录错误。我不知道具体在哪里,因为在这两个函数调用之间的是 Apple 编写的代码。

void Setup() {
  // ...

  glGenFramebuffers(1, &resolveFramebuffer);
  glBindFramebuffer(GL_FRAMEBUFFER, resolveFramebuffer);

  glGenRenderbuffers(1, &resolveColorRenderbuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, resolveColorRenderbuffer);
  [[self context] renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, resolveColorRenderbuffer);

  int backingWidth, backingHeight;
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
  glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);

  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    std::cerr << "Failed to make complete resolve framebuffer." << std::endl;

  glGenFramebuffers(1, &sampleFramebuffer);
  glBindFramebuffer(GL_FRAMEBUFFER, sampleFramebuffer);

  GLuint sampleColorRenderbuffer;
  glGenRenderbuffers(1, &sampleColorRenderbuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, sampleColorRenderbuffer);
  glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, backingWidth, backingHeight);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, sampleColorRenderbuffer);

  GLuint sampleDepthRenderbuffer;
  glGenRenderbuffers(1, &sampleDepthRenderbuffer);
  glBindRenderbuffer(GL_RENDERBUFFER, sampleDepthRenderbuffer);
  glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, sampleDepthRenderbuffer);

  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    std::cerr << "Failed to make complete sample framebuffer." << std::endl;

  glBindFramebuffer(GL_FRAMEBUFFER, resolveFramebuffer);
}

void Render() {
  glBindFramebuffer(GL_FRAMEBUFFER, sampleFramebuffer);
  glViewport(0, 0, renderer->DeviceWidth(), renderer->DeviceHeight());
  glClearColor(0xfa/255.f, 0xe6/255.f, 0xd4/255.f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glBindVertexArrayOES(vertexArray);
  glUseProgram(program);

  GLuint mvpm = glGetUniformLocation(program, "modelViewProjectionMatrix");
  GLuint nm = glGetUniformLocation(program, "normalMatrix");
  glUniformMatrix4fv(mvpm, 1, GL_FALSE, modelViewProjectionMatrix.begin());
  glUniformMatrix4fv(nm, 1, GL_FALSE, normalMatrix.begin());

  glDrawArrays(GL_TRIANGLES, 0, verts.size()/2);

  glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, resolveFramebuffer);
  glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, sampleFramebuffer);
  glResolveMultisampleFramebufferAPPLE();

  const GLenum discards[]  = {GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT};
  glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, discards);

  glBindRenderbuffer(GL_RENDERBUFFER, resolveColorRenderbuffer);

  [[self context] presentRenderbuffer:GL_RENDERBUFFER];
}
4

0 回答 0