简体   繁体   中英

Refactoring to use OpenGL VBOs

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 the vbo.VBO wrappers available in PyOpenGL.

您缺少glEnableClientState(GL_INDEX_ARRAY)来激活索引指针的使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM