yuriks's picture

VBO problems: nothing gets drawn

Hey. 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.BindBuffer(BufferTarget.ArrayBuffer, glVertexBuffer);
            GL.VertexPointer(2, VertexPointerType.Int, 0, 0);
            //GL.BindTexture(TextureTarget.Texture2D, glTexture);
            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.DeleteBuffers(1, ref glVertexBuffer);
            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);

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?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

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?

yuriks's picture

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.

objarni's picture

Oh, sorry didn't see the end of your first post.

Maybe this could be fixed? I mean accepting 0 as a valid IntPtr.Zero substitute?

the Fiddler's picture

I'm afraid no, as this would mean adding an int overload to every IntPtr parameter.

We'll just have to use IntPtr.Zero or null as the compiler writers intended.

objarni's picture

@yuriks, Fiddler

But why did yurik NOT get a compiler warning/error then..? I just assumed he did...

yuriks's picture

Yeah, didn't get any errors or warnings. I assume that 0 (Int32) is being treated as an object, instead of a null pointer.

the Fiddler's picture

It's a bit ugly, but I can't think of any way to work around that, aside from adding inline documentation.

objarni's picture

I'm still not clear on this; is 0 automagically converted to some kind of IntPtr? If so, to which IntPtr?

yuriks's picture

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.

objarni's picture

I cannot see the cast in your example:

            GL.VertexPointer(2, VertexPointerType.Int, 0, 0);