OpenGL-在存储的Texture2D中访问RGB像素数据 [英] OpenGL - Access RGB pixel data in a stored Texture2D
问题描述
我已将场景作为纯色渲染到FBO,并将其存储在纹理2D中,如下所示.
I've rendered the scene as solid colours to an FBO and stored it in a texture 2D, shown below.
现在,我需要访问特定像素坐标或纹理坐标的RGB值,这很有帮助.
Now I need to access the RGB values of a particular pixel co-ordinate, or texture-co-ordinate, either is helpful.
我了解我需要使用GL.ReadPixels
,但只有先成功创建位图,然后才能在每一帧进行操作,否则会导致性能问题.
I understand I need to use GL.ReadPixels
but have only succeeded creating a bitmap first and doing this every frame creates performance issues.
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;
但是有 明显的缺点.
由jp链接的 PBO 文章中所述,glReadPixels()
会阻塞管道并等到所有像素数据传输完毕,然后再将控制权返回给应用程序.在我的程序和我的机器上,该停顿是显而易见的.
As described in the PBO article linked by j-p, glReadPixels()
blocks the pipeline and waits until all pixel data is transferred, only then returning control to the application. In my program, and on my machine, that stall is noticeable.
长期解决方案似乎是像素缓冲区对象,它通过在GL和客户端之间提供异步数据传输来减少开销.当我有东西要显示时,我会修改这个答案.
The long term solution seems to be the Pixel Buffer Object which reduces the overhead by providing asynchronous data transfer between GL and the client. I'll amend this answer when I have something to show.
这篇关于OpenGL-在存储的Texture2D中访问RGB像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!