我正在尝试从动态生成的纹理(RTT,渲染到纹理)中读取像素。我通过实施此处列出的 Apple 建议方法来拍摄此快照。
这适用于呈现到屏幕上的“默认”颜色缓冲区,但我无法让它适用于写入纹理的像素。
我对这些行有问题:
// Bind the color renderbuffer used to render the OpenGL ES view
// If your application only creates a single color renderbuffer which is already bound at this point,
// this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
// Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);
// Get the size of the backing CAEAGLLayer
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
问题是我没有颜色缓冲区,因为我正在渲染到纹理中。
这是我创建纹理的方法:
void Texture::generate()
{
// Create texture to render into
glActiveTexture(unit);
glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_2D, handle);
// Configure texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
以下是我如何将其链接到用于渲染的 FBO:
void Texture::attachToFrameBuffer(GLuint framebuffer)
{
this->framebuffer = framebuffer;
// Attach texture to the color attachment of the provided framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0);
}
如您所见,我并没有真正可以首先绑定到的 _colorRenderbuffer。我首先认为绑定到帧缓冲区然后执行 Apple 的代码可以解决问题,但是 backingWidth 和 backingHeight 并没有给我纹理的分辨率(2048x2048),而是我的场景的分辨率(320x320)。所以我想我在这里遗漏了一些东西。