我知道glGetTexImage
OpenGL函数允许从整个纹理中读取像素。是否有OpenGL函数执行相同的操作但允许传递矩形来限制读取的像素?
答案 0 :(得分:3)
以下是我最终在OpenTK中完成的工作(应该很容易转换为C ++或其他支持OpenGL / OpenGL ES的语言):
public Bitmap GetBitmap()
{
int fboId;
GL.Ext.GenFramebuffers(1, out fboId);
CheckGLError();
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboId);
CheckGLError();
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, TextureId, 0);
CheckGLError();
Bitmap b = new Bitmap(Width, Height);
var bits = b.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.ReadPixels(Rect.Left, Rect.Top, Width, Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
GL.Ext.DeleteFramebuffers(1, ref fboId);
b.UnlockBits(bits);
return b;
}