Getting VBOs working in C#

Started by
3 comments, last by nawitus 16 years, 3 months ago
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#:

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.
Advertisement
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)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
You should create the VBO only once, not every frame.
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.
Take a look at this tutorial.
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

This topic is closed to new replies.

Advertisement