PDA

View Full Version : SharpGL gl.DrawElements can't work



Shintai
11-08-2014, 04:47 AM
Hi, I'm using C# sharpGL in my work, I'm trying to use gl.DrawElements to increase my performance.
I would like to draw thousands of line, i can drew it with gl.Begin()...gl.End(), but its very lag.
I'm trying to use gl.DrawElements to do this job, but there some problems here!
Here is my code, I'm using a simplest example to draw a pyramid:



float[] vertices = new float[]
{
0, 0, 0, // 0 bottom back left
1, 0, 0, // 1 bottom front left
1, 1, 0, // 2 top front left
0, 1, 0, // 3 top back left
1, 0, 1, // 4 bottom front right
1, 1, 1, // 5 top front right
};

int[] indices = new int[]
{
0, 1, 2, // left bottom triangle
2, 3, 0, // left top triangle
1, 4, 5, // front bottom triangle
5, 1, 2, // front top triangle
};

uint[] buffer = new uint[1];
uint[] indexBuffer = new uint[1];
gl.GenBuffers(1, buffer);
gl.GenBuffers(1, indexBuffer);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, buffer[0]);
IntPtr ptr1 = GCHandle.Alloc(modelData, GCHandleType.Pinned).AddrOfPinnedObject();
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices.Length * sizeof(float), ptr1, OpenGL.GL_STATIC_DRAW);

gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);

gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]);
IntPtr ptr2 = GCHandle.Alloc(indices, GCHandleType.Pinned).AddrOfPinnedObject();
gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indices.Length * sizeof(int), ptr2, OpenGL.GL_STATIC_DRAW);

gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, 0);


gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, new IntPtr(0));

gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, buffer[0]);
gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.VertexPointer(3, OpenGL.GL_FLOAT, 6 * sizeof(float), new IntPtr(0));
gl.EnableClientState(OpenGL.GL_NORMAL_ARRAY);
gl.NormalPointer(OpenGL.GL_FLOAT, 6 * sizeof(float), new IntPtr(12));
gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer[0]);

gl.DrawElements(OpenGL.GL_TRIANGLES, 12, OpenGL.GL_UNSIGNED_INT, new IntPtr(0));


Any one can teach me?
Thanks a lot!!

carsten neumann
11-08-2014, 06:44 AM
IntPtr ptr1 = GCHandle.Alloc(modelData, GCHandleType.Pinned).AddrOfPinnedObject();


vertices instead of modelData?

Shintai
11-10-2014, 12:27 AM
Sorry about that, yes, the vertices instead of modelData, just like below


IntPtr ptr1 = GCHandle.Alloc(vertices, GCHandleType.Pinned).AddrOfPinnedObject();