OpenGl BindBuffer到数组

时间:2014-01-05 00:07:55

标签: c# opengl opentk

我使用Open GL从OpenTK获取此代码片段,用于在屏幕上绘制纹理并完全正常工作;

GL.BindBuffer(BufferTarget.PixelUnpackBuffer, glOutputBufferID);
GL.BindTexture(TextureTarget.Texture2D, glOutputTexID);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, xRes, yRes,
    PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0); 

但我想做的,不是将输出数据映射到某些纹理,而是将其存储在数组中;正如我从内核代码中看到的那样,数据来自uint。

我应该如何修改此代码,以便我可以将像素数据作为uint [] _array?

1 个答案:

答案 0 :(得分:0)

我设法让它发挥作用:

GL.BindBuffer(BufferTarget.PixelUnpackBuffer, bufferID);
Color color = Color.Black;
unsafe
{ 
 byte* basePtr =   (byte*)GL.MapBuffer(BufferTarget.PixelUnpackBuffer,    BufferAccess.ReadOnly);
            byte* pixPtr = basePtr;
            for(int y = 0; y < yRes; y++)
            {
                int flipY = yRes-1-y;  //used to vertically flip the output
                for(int x = 0; x < xRes; x++)
                {
                    result[flipY,x] = Color.FromArgb((int)pixPtr[3], (int)pixPtr[0], (int)pixPtr[1], (int)pixPtr[2]);
                    pixPtr += 4;
                }
            }
            GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer);
        }
        GL.BindBuffer(BufferTarget.PixelUnpackBuffer,0);