Opentk – Extracting Data from VBO with granularity

As the future of XNA is not so clear, i decided to invest a considerable time in the 3D port of  Monogame. I joined the group and started to contribute with some code. My goal is to run PloobsEngine completely on Mono Platform (Windows + Linux + Mac + IOs + Android).

In this post (and i hope in lots of more) i will put some parts of the code (some done by my, and lots done by others)  used in the project that could be usefull for everyone. Its is not going to a tutorial, it will be more a code snipped.

Most of the posts will talk about computer graphics (Specially Opengl — Opentk wrapper actually) that is the part where iam more active.

Just to start, i will put show a very simple function that just retrive vertex data from a VBO using the XNA functions signature.

public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            var elementSizeInByte = Marshal.SizeOf(typeof(T));
            IntPtr ptr = new IntPtr();
            //ptr = GL.Arb.MapBuffer(BufferTargetArb.ArrayBuffer, ArbVertexBufferObject.ReadOnlyArb | ArbVertexBufferObject.ArrayBufferArb);
            ptr = OpenTK.Graphics.ES20.GL.Oes.MapBuffer(OpenTK.Graphics.ES20.All.ArrayBuffer, (OpenTK.Graphics.ES20.All)0);
            ptr = GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.ReadOnly);
            //ErrorCode e = GL.GetError();

            ///its dark magic time
            if (ptr != null && ptr.ToInt32() != 0)
                ptr = ptr + offsetInBytes + startIndex * vertexStride;

                byte[] bytes = new byte[vertexStride * data.Count()];
                Marshal.Copy(ptr, bytes, 0, vertexStride * data.Count());

                byte[] by = new byte[elementSizeInByte];

            GL.UnmapBuffer( BufferTarget.ArrayBuffer);


                IntPtr buffer = Marshal.AllocHGlobal(elementSizeInByte);

                for (int j = 0; j < elementCount; j++)
                    Array.ConstrainedCopy(bytes, j * vertexStride, by, 0, elementSizeInByte);

                    Marshal.Copy(by, 0, buffer, elementSizeInByte);
                    object retobj = Marshal.PtrToStructure(buffer, typeof(T));
                    data[j] = (T)retobj;

                throw new Exception("Could not decode the Vertex Buffer");

The parameters are:
int offsetInBytes -> offset in bytes of the data you want from the beginning of the vertex stream
T[] data -> where the extracted data will be saved
int startIndex -> the first item of the vertex stream (each)
int elementCount -> how many elemets to extract
int vertexStride -> size of each vertex of the vertex stream

Suppose you have a vertex with the following attributes:

Position Vector3
Normal Vector3
Coordinate Texture Vector2

To get all data you could call:
GetData(0, ArrayWhereDataWillBeSaved, 0, ArraySize, sizeof vertex (Vector3 + Vector3 + Vector2 = in this case is usually 12 + 12 + 8))

To get only the position data you could call:
GetData(0, Vector3Array, 0, ArraySize, sizeof vertex (Vector3 + Vector3 + Vector2 = in this case is usually 12 + 12 + 8))

to get only normal and coordinate texture:
GetData(sizeof position (Vector2 usually 12), Array of (Struct with Vector3 and Vector2) , 0, ArraySize, sizeof vertex (Vector3 + Vector3 + Vector2 = in this case is usually 12 + 12 + 8))

Got =P


  1. #1 by on 21 de julho de 2017 - 1:52 pm

    Your item is in that great pricing I do not considered all good would be and so excellent. It’s striking. The mama will likely love that it upon Christmas time morning whenever she starts gift therefore styles like I invested significantly more, and yet pricing was just very good!!

  2. #2 by open source php developer on 21 de julho de 2017 - 3:11 pm

    below youll find the link to some web pages that we feel it is best to visit

  3. #3 by Shopping on 21 de julho de 2017 - 3:25 pm

    I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again!

1 596 597 598
(não será publicado)