PathSims's picture

[Solved] Can't get GL.ColorPointer to properly color my VBO - Please Help

This seems quite simple yet I can't seem to get the vertices properly colored in my vertex buffer object. I am simply drawing to a GLControl in my winforms application. I must have something out of order or I am incorrectly defining the colorpointer. Please help. My simple example which sit in a button click for testing is below. The attached image shows what is being rendered, I would expect all of the points to be red.

        GL.Clear(ClearBufferMask.ColorBufferBit)
        GL.Clear(ClearBufferMask.DepthBufferBit)
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadIdentity()
 
        'Create the vertex buffer object
        Dim vbohandle As Int32, vbo(9) As OpenTK.Vector2
        vbo(0) = New Vector2(5, 5)
        vbo(1) = New Vector2(10, 10)
        vbo(2) = New Vector2(50, 50)
        vbo(3) = New Vector2(290, 480)
        vbo(4) = New Vector2(340, 480)
        vbo(5) = New Vector2(340, 580)
        vbo(6) = New Vector2(440, 610)
        vbo(7) = New Vector2(300, 300)
        vbo(8) = New Vector2(300, 100)
        vbo(9) = New Vector2(600, 100)
 
        'Create the color buffer object
        Dim cbohandle As Int32, cbo(vbo.Count * 3 - 1) As Byte
        For i = 0 To cbo.Count - 1
            cbo(i) = Color.Red.R
            cbo(i + 1) = Color.Red.G
            cbo(i + 2) = Color.Red.B
            i += 2
        Next
 
        'Buffer that sweet data
        GL.GenBuffers(1, cbohandle)
        GL.BindBuffer(BufferTarget.ArrayBuffer, cbohandle)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(cbo.Count * Marshal.SizeOf(cbo(0))), cbo, BufferUsageHint.StaticDraw)
 
        GL.GenBuffers(1, vbohandle)
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbohandle)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(vbo.Count * Marshal.SizeOf(vbo(0))), vbo, BufferUsageHint.StaticDraw)
 
        'Prepare to Draw
        GL.PointSize(10)
 
        'Enable the states
        GL.EnableClientState(ArrayCap.VertexArray)
        GL.EnableClientState(ArrayCap.ColorArray)
 
        'Define the pointers
        GL.VertexPointer(2, VertexPointerType.Float, 0, 0)
        GL.ColorPointer(3, ColorPointerType.Byte, 0, 0)
 
        'Draw them babies!
        GL.DrawArrays(BeginMode.Points, 0, vbo.Count)
 
        'Disable those states
        GL.DisableClientState(ArrayCap.VertexArray)
        GL.DisableClientState(ArrayCap.ColorArray)
 
        'Swap that shtuff
        GlControl1.SwapBuffers()

Thanks for helping.

Inline Images
Colors appear to be random

Comments

Comment viewing options

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

Given enough time I can usually solve my problems ;). There were two things limiting me from the solution, one was that the color type is ColorPointerType.UnsignedByte and not ColorPointerType.Byte, which makes sense since in .NET a byte goes from 0 to 255. The second problem was that I was not binding my color buffer before defining the colorpointer, I assume since the vertex buffer object was the last thing binded it was just grabbing the byte values from the vertex buffer and hence the random colors displayed. The full solution for this simple example is below. Anyone please feel free to post.

        GL.Clear(ClearBufferMask.ColorBufferBit)
        GL.Clear(ClearBufferMask.DepthBufferBit)
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadIdentity()
 
        'Create the vertex buffer object
        Dim vbohandle As Int32, vbo(9) As OpenTK.Vector2
        vbo(0) = New Vector2(5, 5) : vbo(1) = New Vector2(10, 10) : vbo(2) = New Vector2(50, 50)
        vbo(3) = New Vector2(290, 480) : vbo(4) = New Vector2(340, 480) : vbo(5) = New Vector2(340, 580)
        vbo(6) = New Vector2(440, 610) : vbo(7) = New Vector2(300, 300) : vbo(8) = New Vector2(300, 100)
        vbo(9) = New Vector2(600, 100)
 
        'Create the color buffer object
        Dim cbohandle As Int32, cbo(vbo.Count * 3 - 1) As Byte
        For i = 0 To cbo.Count - 1
            cbo(i) = Color.Red.R
            cbo(i + 1) = Color.Red.G
            cbo(i + 2) = Color.Red.B
            i += 2
        Next
 
        'Buffer that sweet data
        GL.GenBuffers(1, cbohandle)
        GL.BindBuffer(BufferTarget.ArrayBuffer, cbohandle)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(cbo.Count * Marshal.SizeOf(cbo(0))), cbo, BufferUsageHint.StaticDraw)
 
        GL.GenBuffers(1, vbohandle)
        GL.BindBuffer(BufferTarget.ArrayBuffer, vbohandle)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(vbo.Count * Marshal.SizeOf(vbo(0))), vbo, BufferUsageHint.StaticDraw)
 
        'Prepare to Draw
        GL.PointSize(10)
 
        'Enable the states
        GL.EnableClientState(ArrayCap.VertexArray)
        GL.EnableClientState(ArrayCap.ColorArray)
 
        'Define the pointers
        'don't need to bind this one since it was the last thing binded above.
        GL.VertexPointer(2, VertexPointerType.Float, 0, 0)
 
        GL.BindBuffer(BufferTarget.ArrayBuffer, cbohandle)
        GL.ColorPointer(3, ColorPointerType.UnsignedByte, 0, 0)
 
        'Draw them babies!
        GL.DrawArrays(BeginMode.Points, 0, vbo.Count)
 
        'Disable those states
        GL.DisableClientState(ArrayCap.VertexArray)
        GL.DisableClientState(ArrayCap.ColorArray)
 
        'Swap that shtuff
        GlControl1.SwapBuffers()