Jump to content
  • Advertisement
Sign in to follow this  
SaikkezHai

OpenGL Getting VBOs working in C#

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!