OpenGL - 访问存储的 Texture2D 中的 RGB 像素数据
OpenGL - Access RGB pixel data in a stored Texture2D
我已将场景渲染为 FBO 的纯色并将其存储在 2D 纹理中,如下所示。
现在我需要访问特定像素坐标或纹理坐标的 RGB 值,两者都有用。
我知道我需要使用 GL.ReadPixels
但只成功地先创建了一个位图并且每帧都这样做会产生性能问题。
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBO);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, Texture.GetTextureID("FBOtexture"), 0);
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(0, 0, Width, Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
b.UnlockBits(bits);
如何直接访问数据?
目前,这是有效的
int pixelData = 0;
GL.ReadPixels (fboWidth / 2, fboHeight / 2, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.UnsignedByte, ref pixelData);
int red = Color.FromArgb(pixelData).R;
int green = Color.FromArgb(pixelData).G;
int blue = Color.FromArgb(pixelData).B;
但是有 明显的缺点。
如 j-p 链接的 PBO 文章中所述,glReadPixels()
阻塞管道并等待所有像素数据传输完毕,然后才将控制权返回给应用程序。在我的程序和我的机器上,这个停顿很明显。
长期的解决方案似乎是像素缓冲区对象,它通过在 GL 和客户端之间提供异步数据传输来减少开销。当我有东西要展示时,我会修改这个答案。
我已将场景渲染为 FBO 的纯色并将其存储在 2D 纹理中,如下所示。
现在我需要访问特定像素坐标或纹理坐标的 RGB 值,两者都有用。
我知道我需要使用 GL.ReadPixels
但只成功地先创建了一个位图并且每帧都这样做会产生性能问题。
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FBO);
GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, Texture.GetTextureID("FBOtexture"), 0);
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(0, 0, Width, Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
b.UnlockBits(bits);
如何直接访问数据?
目前,这是有效的
int pixelData = 0;
GL.ReadPixels (fboWidth / 2, fboHeight / 2, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.Rgb, PixelType.UnsignedByte, ref pixelData);
int red = Color.FromArgb(pixelData).R;
int green = Color.FromArgb(pixelData).G;
int blue = Color.FromArgb(pixelData).B;
但是有 明显的缺点。
如 j-p 链接的 PBO 文章中所述,glReadPixels()
阻塞管道并等待所有像素数据传输完毕,然后才将控制权返回给应用程序。在我的程序和我的机器上,这个停顿很明显。
长期的解决方案似乎是像素缓冲区对象,它通过在 GL 和客户端之间提供异步数据传输来减少开销。当我有东西要展示时,我会修改这个答案。