
VBO problems: nothing gets drawn
Posted Monday, 25 August, 2008 - 18:23 by yuriks inHey. I'm having some issues using VBOs. This is the code I have right now:
public void Draw(Point position) { int[] vertexData = { position.X, position.Y, position.X + Image.Width, position.Y, position.X, position.Y + Image.Height, position.X + Image.Width, position.Y + Image.Height }; uint glVertexBuffer; GL.GenBuffers(1, out glVertexBuffer); GL.BindBuffer(BufferTarget.ArrayBuffer, glVertexBuffer); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertexData.Length * sizeof(int)), vertexData, BufferUsageHint.StreamDraw); int size; // This is 32, like it should be so I assumed the buffer got uploaded correctly. GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferMapped, out size); GL.EnableClientState(EnableCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, glVertexBuffer); GL.VertexPointer(2, VertexPointerType.Int, 0, 0); //GL.BindTexture(TextureTarget.Texture2D, glTexture); GL.Disable(EnableCap.Texture2D); GL.Color3(1.0f, 1.0f, 1.0f); GL.BindBuffer(BufferTarget.ArrayBuffer, glVertexBuffer); GL.DrawArrays(BeginMode.TriangleStrip, 0, vertexData.Length); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DisableClientState(EnableCap.VertexArray); GL.DeleteBuffers(1, ref glVertexBuffer); /*GL.Begin(BeginMode.TriangleStrip); GL.TexCoord2(0, 0); GL.Vertex2(position.X, position.Y); GL.TexCoord2(1, 0); GL.Vertex2(position.X + Image.Width, position.Y); GL.TexCoord2(0, 1); GL.Vertex2(position.X, position.Y + Image.Height); GL.TexCoord2(1, 1); GL.Vertex2(position.X + Image.Width, position.Y + Image.Height); GL.End();*/ }
I call SwapBuffer later on in another function. The commented out immediate mode section is what I want to replace, and that works fine, but using VBOs, nothing get's drawn. I've checked using GL.GetError after every function call and no errors are reported. Anyone got a clue on what I'm doing wrong?


Comments
Re: VBO problems: nothing gets drawn
One thing that strikes me as odd is that you construct/destroy the VBO in the "Draw" function. The "usage pattern" for VBOs is:
1. Construct a VBO eg. when loading a level of the game, known by an 'uint name'
2. Use 'name' when rendering each frame to draw the VBO
3. When you don't need the VBO anymore, destroy it (eg. when loading another level)
This pattern is very similar to texture objects actually.
Are you able to draw using basic GL.Begin()-primitives?
Re: VBO problems: nothing gets drawn
Yes, [b]like I said in my original post[/b], the immediate mode sections works. I was creating the VBO on the fly just for testing.
Anyway, I fixed the problem, turns out I needed to cast the last parameter of GL.VertexPointer to IntPtr.
Re: VBO problems: nothing gets drawn
@yuriks
Oh, sorry didn't see the end of your first post.
@Fiddler
Maybe this could be fixed? I mean accepting 0 as a valid IntPtr.Zero substitute?
Re: VBO problems: nothing gets drawn
I'm afraid no, as this would mean adding an
intoverload to everyIntPtrparameter.We'll just have to use
IntPtr.Zeroornullas the compiler writers intended.Re: VBO problems: nothing gets drawn
@yuriks, Fiddler
But why did yurik NOT get a compiler warning/error then..? I just assumed he did...
Re: VBO problems: nothing gets drawn
Yeah, didn't get any errors or warnings. I assume that 0 (Int32) is being treated as an object, instead of a null pointer.
Re: VBO problems: nothing gets drawn
It's a bit ugly, but I can't think of any way to work around that, aside from adding inline documentation.
Re: VBO problems: nothing gets drawn
I'm still not clear on this; is 0 automagically converted to some kind of IntPtr? If so, to which IntPtr?
Re: VBO problems: nothing gets drawn
0 isn't converted to an IntPtr, it's taken as an object of type Int32, that's why I explicitly need to cast it.
Re: VBO problems: nothing gets drawn
I cannot see the cast in your example: