Search code examples
c#openglopentk

OpenGl BindBuffer to array


I have this code snippet from OpenTK using Open GL, which is used to draw texture on screen and works completely fine;

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); 

But what I want to do, is not to map output data to some texture, but to store it inside array; As I see from kernel code, data comes as uint.

How should I modify this code, so that I could get pixel data as uint[] _array?


Solution

  • I have managed to get it working:

    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);