我在使用ios进行屏幕外渲染时遇到了问题。下面是设置帧缓冲对象和相关缓冲区的代码。它与苹果公司在其开发人员中心的代码基本相同。
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;
当它运行时,它打印出'framebuffer complete',所以我假设framebuffer对象已经正确设置。但我希望调用glReadPixels给我回复我设置的清晰颜色(红色) - 而不是它返回黑色。
这里有什么我想念的吗?我正试图让屏幕外渲染在我的应用程序中挑选对象。
答案 0 :(得分:0)
我认为你必须添加
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels之前的。但如果这不起作用,我就没有想法了。
答案 1 :(得分:0)
似乎上面的代码是正确的。唯一的问题是在ios上你需要使用GL_RGBA作为glreadpixels的参数,GL_RGB不起作用。