unsafe {
fixed (float* data_ptr = data) {
Console.WriteLine("A");
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Console.WriteLine("B");
int i = 0;
for(int j = 0; j < points.Length; j++){
data[i++] = (float)points[j].x;
data[i++] = (float)points[j].y;
data[i++] = (float)points[j].z;
}
Gl.glVertexPointer(1, Gl.GL_FLOAT, 0, (IntPtr)data_ptr);
Console.WriteLine("C");
Gl.glDrawArrays(Gl.GL_POLYGON, 0, 1);
Console.WriteLine("D");
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
Console.WriteLine("E");
}
}
"data" is a big static array of floats i'm stuffing the point data into before i pass it in as a vertex array, so i don't have to create an array each time.
Just trying to get it working first; can add on textures/shading/whatever later. I'm not particularly happy with having to do all the voodoo with unsafe code and pointers and whatnot, but more importantly this code crashes immediately after C with no error message to speak of. I'm guessing this has something to do with the pointer magic, although i've tried various methods of extracting the IntPtr from the float[] and have not have any luck at all. Anyone know what's wrong with that? Am i forgetting to enable/disable something, or passing a wrong parameter into one of the gl... calls?
edit: for reference, the code this is replacing (that works) is:
Gl.glBegin(Gl.GL_POLYGON);
for(int j = 0; j < points.Length; j++){
Point3DG p = points[j];
Gl.glVertex3d(p.x, p.y, p.z);
}
Gl.glEnd();
I just want to get the Vertex Array code working first, nevermind the fact that in it's current state it's probably not any faster.
Getting VBOs working in C#
Now, in trying to speed up my rendering code i'm trying to replace all the glBegin glVertex glEnd etc. calls with vertex arrays or VBOs. This is what i have so far, using Tao.OpenGL and Visual C#:
This
Gl.glVertexPointer(1, Gl.GL_FLOAT, 0, (IntPtr)data_ptr);
should probably be
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, (IntPtr)data_ptr);
This
Gl.glDrawArrays(Gl.GL_POLYGON, 0, 1);
should probably be
Gl.glDrawArrays(Gl.GL_POLYGON, 0, points.Length);
I also recommend that you use GL_TRIANGLES or GL_TRIANGLE_STRIP.
Break down your polygons to triangles!
Use index based rendering as well (with glDrawRangeElements)
Gl.glVertexPointer(1, Gl.GL_FLOAT, 0, (IntPtr)data_ptr);
should probably be
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, (IntPtr)data_ptr);
This
Gl.glDrawArrays(Gl.GL_POLYGON, 0, 1);
should probably be
Gl.glDrawArrays(Gl.GL_POLYGON, 0, points.Length);
I also recommend that you use GL_TRIANGLES or GL_TRIANGLE_STRIP.
Break down your polygons to triangles!
Use index based rendering as well (with glDrawRangeElements)
i replaced both things you mentioned, and it still crashes after "C" with no error message(dammit). I know about converting to triangles and re-using the VBO and whatnot, but i just want to get a simple "Hello Vertex Array" working first before i get it working well. It crashes on the first frame too, so it's not a problem with re-creating the thing each frame.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement