Drawing multiple objects from one Vertex Buffer Object in OpenGL/OpenTK
Posted
by
stoney78us
on Game Development
See other posts from Game Development
or by stoney78us
Published on 2013-10-21T21:05:11Z
Indexed on
2013/10/21
22:05 UTC
Read the original article
Hit count: 313
I am trying to experimenting drawing method using VBO in OpenGL. Many people normally use 1 vbo to store one object data array. I was trying to do something quite opposite which is storing multiple object data into 1 vbo then drawing it. There is story behind why i want to do this. I want to group many of objects as a single object sometime. However my code doesn't do the justice. Following is my pseudo code:
//Data
double[] vertices = {line strip 1, line strip 2, line strip 3}; //series of vertices
int linestrip1offset = index of the first vertex in line strip 1;
int linestrip2offset = index of the first vertex in line strip 2;
int linestrip3offset = index of the first vertex in line strip 3;
int linestrip1VertexNum = number of vertices in linestrip 1;
int linestrip2VertexNum = number of vertices in linestrip 2;
int linestrip3VertexNum = number of vertices in linestrip 3;
//Setting Up
void init()
{
int[] vBO = new int[1];
GL.GenBuffer(1, vBO);
GL.BindBuffer(BufferTarget.ArrayBuffer, vBO[0]);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(_vertices.Length * sizeof(double)), _vertices, BufferUsageHint.StaticDraw);
GL.EnableClientState(Array.VertexArray);
}
//Drawing
void draw()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, vBO[0]);
GL.EnableClientState(ArrayCap.VertexArray);
GL.VertexPointer(3, VertexPointerType.Double, 0, linestrip1offset); //drawing first linestrip
GL.DrawArrays(drawMode, linestrip1offset , linestrip1VertexNum );
GL.VertexPointer(3, VertexPointerType.Double, 0, linestrip2offset); //drawing second linestrip
GL.DrawArrays(drawMode, linestrip2offset , linestrip2VertexNum );
GL.VertexPointer(3, VertexPointerType.Double, 0, linestrip3offset); //drawing third linestrip
GL.DrawArrays(drawMode, linestrip3offset , linestrip3VertexNum );
GL.DisableClientState(ArrayCap.VertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
I don't know what i did wrong but i think technically it should work where we can tell OpenGL which part of the data in the vBO to be drawn.
© Game Development or respective owner