1
\$\begingroup\$

I'm trying to learn how to use VBO (Vertex Buffer Objects) by putting together a simple program that draws a quad to the screen using OpenTK (C# OpenGL bindings).

Unfortunately I'm not seeing anything on screen. If I draw the same quad in immediate mode it shows up fine.

Since it wasn't working I tried adding Color Data too (in case this was the problem) but it didn't help.

Any help would be greatly appreciated!

    private uint indexBufferId;
    private uint vertexBufferId;
    private uint colorBufferId;

    private void InitialiseData()
    {

        // Set-up index buffer:
        ushort[] indices = new ushort[] { 0, 1, 2, 3 };

        GL.GenBuffers(1, out indexBufferId);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId);
        GL.BufferData(
            BufferTarget.ElementArrayBuffer,
            (IntPtr)(indices.Length * sizeof(ushort)),
            indices,
            BufferUsageHint.StaticDraw);


        // Set-up vertex buffer:
        float[] vertexData = new float[] {
            50.0f, 50.0f,
            100.0f, 50.0f,
            100.0f, 100.0f,
            50.0f, 100.0f };

        GL.GenBuffers(1, out vertexBufferId);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferId);
        GL.BufferData(
            BufferTarget.ArrayBuffer,
            (IntPtr)(vertexData.Length * sizeof(float)),
            vertexData,
            BufferUsageHint.StaticDraw);

        // Set-up color buffer:
        float[] colorData = new float[] {
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f
        };

        GL.GenBuffers(1, out colorBufferId);
        GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferId);
        GL.BufferData(
            BufferTarget.ArrayBuffer,
            (IntPtr)(colorData.Length * sizeof(float)),
            colorData,
            BufferUsageHint.StaticDraw);

    }

    protected override void OnRenderFrame(FrameEventArgs e)
    {

        base.OnRenderFrame(e);

        GL.Clear(ClearBufferMask.ColorBufferBit);

        // Bind vertex buffer:
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferId);
        GL.EnableClientState(ArrayCap.VertexArray);
        GL.VertexPointer(2, VertexPointerType.Float, 0, IntPtr.Zero);

        // Bind color buffer:
        GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferId);
        GL.EnableClientState(ArrayCap.ColorArray);
        GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero);

        // Bind index buffer:
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId);

        // Draw:            
        GL.DrawElements(
            BeginMode.Quads, 
            1, 
            DrawElementsType.UnsignedShort, 
            IntPtr.Zero);

        // Disable:
        GL.DisableClientState(ArrayCap.VertexArray);
        GL.DisableClientState(ArrayCap.ColorArray);

        SwapBuffers();

    }
\$\endgroup\$
  • \$\begingroup\$ So my call to GL.DrawElements(...) was incorrect. It should be: GL.DrawElements( BeginMode.Quads, 4, DrawElementsType.UnsignedShort, IntPtr.Zero); The second parameter is number of vertices, not number of quads. Looks like my test data is still a bit iffy but at least it's rendering now. \$\endgroup\$ – Karle Jul 31 '13 at 18:36
  • \$\begingroup\$ You should answer your own question then. \$\endgroup\$ – user15805 Jul 31 '13 at 21:25
  • \$\begingroup\$ Voting to close because this is a debugging question with a very specific programming error. It will be unlikely ot be helpful to future visitors. \$\endgroup\$ – Seth Battin Jul 31 '13 at 23:55
  • \$\begingroup\$ @Alex M: the site doesn't let you answer your own question within the first 8 hours if you are a new user. \$\endgroup\$ – Karle Aug 4 '13 at 14:59
2
\$\begingroup\$

In the glDrawElements call:

void glDrawElements(GLenum mode,  GLsizei count,  GLenum type,  const GLvoid * indices);

The second parameter count refers to number of vertices, not number of primitives.

Therefore the correct version of the above should be (using the OpenTK API):

GL.DrawElements( BeginMode.Quads, 4, DrawElementsType.UnsignedShort, IntPtr.Zero)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.