0

I'm learning OpenGL from the OpenGL Programming Guide, and have a simple 3d view of a cube working. The I know that using VBOs should help performance and would like to modify my code to use them. I'm having trouble getting this to work, and haven't really found good clean examples online.

Here's what I have working.

My imports are:

import numpy as np

from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGL.GLU import *
from OpenGL.GLUT import *

In my initialization I have:

    # Unit cube, centered at the origin.
    self.positions = np.array([(0.5, -0.5, -0.5), (0.5, 0.5, -0.5),
                               (0.5, 0.5, 0.5), (0.5, -0.5, 0.5),
                               (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
                               (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5),
                               (-0.5, 0.5, -0.5), (-0.5, 0.5, 0.5),
                               (0.5, 0.5, 0.5), (0.5, 0.5, -0.5),
                               (-0.5, -0.5, 0.5), (-0.5, -0.5, -0.5),
                               (0.5, -0.5, -0.5), (0.5, -0.5, 0.5),
                               (-0.5, -0.5, 0.5), (0.5, -0.5, 0.5),
                               (0.5, 0.5, 0.5), (-0.5, 0.5, 0.5),
                               (0.5, -0.5, -0.5), (-0.5, -0.5, -0.5),
                               (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5)],
                              dtype=np.float32)
    self.normals = np.array([(1.0, 0.0, 0.0), (1.0, 0.0, 0.0),
                             (1.0, 0.0, 0.0), (1.0, 0.0, 0.0),
                             (-1.0, 0.0, 0.0), (-1.0, 0.0, 0.0),
                             (-1.0, 0.0, 0.0), (-1.0, 0.0, 0.0),
                             (0.0, 1.0, 0.0), (0.0, 1.0, 0.0),
                             (0.0, 1.0, 0.0), (0.0, 1.0, 0.0),
                             (0.0, -1.0, 0.0), (0.0, -1.0, 0.0),
                             (0.0, -1.0, 0.0), (0.0, -1.0, 0.0),
                             (0.0, 0.0, 1.0), (0.0, 0.0, 1.0),
                             (0.0, 0.0, 1.0), (0.0, 0.0, 1.0),
                             (0.0, 0.0, -1.0), (0.0, 0.0, -1.0),
                             (0.0, 0.0, -1.0), (0.0, 0.0, -1.0)],
                            dtype=np.float32)

    self.faces = np.array(range(24), dtype=np.uint32)

In my draw code:

    # clear color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_NORMAL_ARRAY)

    # self.position_vbo.bind()
    glVertexPointer(3, GL_FLOAT, 0, self.positions)

    # self.normal_vbo.bind()
    glNormalPointer(GL_FLOAT, 0, self.normals)

    glColor3f(0., 1., 0.)
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, self.faces)

    glDisableClientState(GL_VERTEX_ARRAY)
    glDisableClientState(GL_NORMAL_ARRAY)

To refactor this to use VBOs, I've added this to the init:

    self.position_vbo = vbo.VBO(self.positions)
    self.normal_vbo = vbo.VBO(self.normals)
    self.faces_vbo = vbo.VBO(self.faces, target=GL_ELEMENT_ARRAY_BUFFER)

And changed the draw to bind the VBOs:

    # clear color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glEnableClientState(GL_VERTEX_ARRAY)
    glEnableClientState(GL_NORMAL_ARRAY)

    self.position_vbo.bind()
    glVertexPointer(3, GL_FLOAT, 0, 0)

    self.normal_vbo.bind()
    glNormalPointer(GL_FLOAT, 0, 0)

    self.faces_vbo.bind()
    glIndexPointer(GL_UNSIGNED_INT, 0, 0)

    glColor3f(0., 1., 0.)
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, None)

    glDisableClientState(GL_VERTEX_ARRAY)
    glDisableClientState(GL_NORMAL_ARRAY)

I've also tried putting only the positions and normals in VBOs, and leaving the faces alone. I've also tried using 0 instead of None in the glDrawElements.

There is a similar question here, except that it doesn't use thevbo.VBO wrappers available in PyOpenGL.

Community
  • 1
  • 1
Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45

1 Answers1

0

You are missing a glEnableClientState(GL_INDEX_ARRAY) to activate the use of the index pointer.

kakTuZ
  • 562
  • 2
  • 11
  • Ok, I added that and now I get an "Invalid Enumerant" error, for glIndexPointer. Any other clues? – Codie CodeMonkey Oct 04 '13 at 20:39
  • hmm, I was wrong. glIndexPointer is not needed here at all. It has a completely different purpose (color indexing ...). Try to remove the glIndexPointer line. Binding a buffer to GL_ELEMENT_ARRAY_BUFFER is enough for glDrawElements to use it. – kakTuZ Oct 05 '13 at 01:00
  • I made some progress trying to use interleaved arrays. It seems the vertex and normal data must be in a single structure for VBO's--at least that's what I've gleaned from the surprisingly few examples out there. I don't quite have it right yet however, and have had to put it aside for this week. – Codie CodeMonkey Oct 07 '13 at 15:55