framebuffer object rendering on ios

拟墨画扇 提交于 2019-12-11 19:31:00

问题


I'm having problems getting offscreen rendering working on ios. Below is the code for setting up a framebuffer object and associated buffers. It is basically the same as the code that apple have on their developer centre.

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

GLuint color_id, depth_id;
glGenRenderbuffers(1, &color_id);
glBindRenderbuffer(GL_RENDERBUFFER, color_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, screen_width, screen_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_id);

glGenRenderbuffers(1, &depth_id);
glBindRenderbuffer(GL_RENDERBUFFER, depth_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, screen_width, screen_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_id);

std::cout << pick_id << " " << color_id << " " << depth_id << std::endl;

switch(glCheckFramebufferStatus(GL_FRAMEBUFFER))
{
  case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: std::cout << "framebuffer incomplete attachment" << std::endl; break;
  case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: std::cout << "framebuffer incomplete missing attachment" << std::endl; break;
  case GL_FRAMEBUFFER_UNSUPPORTED: std::cout << "framebuffer unsupported" << std::endl; break;
  case GL_FRAMEBUFFER_COMPLETE: std::cout << "framebuffer complete" << std::endl; break;
}

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glFinish();

GLubyte pix[] = {0, 0, 0};
glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pix);

std::cout << (int)pix[0] << " " << (int)pix[1] << " " << (int)pix[2] << std::endl;

When this is run it prints out 'framebuffer complete', so I assume the framebuffer object has been set up correctly. But I would expect the call to glReadPixels to give me back the clear colour (red) which I have set - instead it returns black.

Is there anything I am missing here? I'm trying to get the offscreen rendering working for picking objects in my app.


回答1:


I think you have to add

glReadBuffer(GL_COLOR_ATTACHMENT0);

before the glReadPixels. But if that doesn't work, I'm out of ideas.




回答2:


It seems to be that the code above is correct. The only problem is that on ios you need to use GL_RGBA as argument to glreadpixels, GL_RGB does not work.



来源:https://stackoverflow.com/questions/19006791/framebuffer-object-rendering-on-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!