Search code examples
pythonopenglpyopengl

PyOpenGL: Contents of vertex buffer object ignored


I'm trying to use the tutorials at http://ogldev.atspace.co.uk to get a grip on 'modern' OpenGL, working in Python 3 with PyOpenGL, with some reference to this.

At the moment I'm just trying to draw a single point at various places on the screen, but the point always draws in the center of the screen, regardless of the data I put in my VBO.

I've spotted this question that looks similar, but the rewind() function he mentions looks specific to LWJGL so I'm guessing doesn't apply here.

The relevant bits of my code are as follows:

import sdl2
import numpy
from OpenGL import GL

def _init_gl(self):
    self._gl_context = sdl2.SDL_GL_CreateContext(self._window.window)
    GL.glClearColor(0, 0, 0, 1)

    self._vao = GL.glGenVertexArrays(1)
    GL.glBindVertexArray(self._vao)

    self._vbo = GL.glGenBuffers(1)
    GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self._vbo)
    vertexData = numpy.array([0.5, 0.5, 0.0], dtype=numpy.float32)
    GL.glBufferData(GL.GL_ARRAY_BUFFER, vertexData.nbytes, vertexData,
                    GL.GL_STATIC_DRAW)
    GL.glEnableVertexAttribArray(0);
    GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)

def _draw(self):
    GL.glClear(GL.GL_STENCIL_BUFFER_BIT |
               GL.GL_DEPTH_BUFFER_BIT |
               GL.GL_COLOR_BUFFER_BIT)
    GL.glBindVertexArray(self._vao)
    GL.glDrawArrays(GL.GL_POINTS, 0, 1)

    sdl2.SDL_GL_SwapWindow(self._window.window)

Can anyone spot what might be going wrong?


Solution

  • You're using generic vertex attributes:

    GL.glEnableVertexAttribArray(0);
    GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
    

    Generic attributes are used in conjunction with shaders, which you don't have in the posted code. Trying to use them without shaders may work under some conditions at least on certain platforms, but it's generally a recipe for disappointment.

    As long as you're using the legacy fixed function pipeline, you should use fixed function attributes. The matching calls to set up the vertex positions are:

    GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
    GL.glVertexPointer(3, GL.GL_FLOAT, 0, None)
    

    The other option is that you fully commit to using the OpenGL Core Profile, and add shaders to your code. One of the next steps in the tutorial you are following describes how to do that.