glGenVertexArrays function doesn't work in every Python script

Question:

I want to create a pyOpenGL program to display quads. When creating a VAO an exception is being thrown.

The glGenVertexArrays function throws this excpetion in my loader.py script:

OpenGL.error.NullFunctionError: Attempt to call an undefined function glGenVertexArrays

I guess it has something to do with initialization.

loader.py (the create_vao function is relevant)

from raw_model import RawModel
from OpenGL.GL import *
from OpenGL.GLUT import *


class Loader:

    __vaos = []
    __vbos = []

    def load_to_vao(self, positions: list):
        vao_id = self.create_vao()
        self.store_data_in_attribute_list(0, positions)
        self.unbind_vao()
        return RawModel(vao_id, len(positions)//3)

    @classmethod
    def clean_up(cls):
        for vao in cls.__vaos:
            glDeleteVertexArrays(vao)
        for vbo in cls.__vbos:
            glDeleteBuffers(vbo)

    @classmethod
    def create_vao(cls):
        vao_id = glGenVertexArrays(1)
        cls.__vaos.append(vao_id)
        glBindVertexArray(vao_id)
        return vao_id

    @classmethod
    def store_data_in_attribute_list(cls, attribute_number: int, data: list):
        vbo_id = glGenBuffers()
        cls.__vbos.append(vbo_id)
        glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
        glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW)
        glVertexAttribPointer(attribute_number, 3, GL_FLOAT, False, 0, 0)
        glBindBuffer(GL_ARRAY_BUFFER, 0)

    @staticmethod
    def unbind_vao():
        glBindVertexArray(0)

display_manager.py (the create_display function is relevant):

from OpenGL.GL import *
from OpenGL.GLUT import *


class DisplayManager:

    def __init__(self, x: int = 1920, y: int = 1080):
        if x is not None: self.__width = x
        if y is not None: self.__height = y

    def create_display(self, window_name):
        glutInit()
        glutInitDisplayMode(GLUT_RGBA)                           # initialize colors
        glutInitWindowSize(self.get_width(), self.get_height())  # set windows size
        glutInitWindowPosition(0, 0)                             # set window position
        glutCreateWindow(f"{window_name}")                       # create window (with a name) and set window attribute
        print(glGenVertexArrays(1))
        input("This ran")
        glutDisplayFunc(self.update_display)
        glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)  # prevent program from stopping

    @staticmethod
    def update_display():
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)  # Remove everything from screen (i.e. displays all white)
        glLoadIdentity()                                    # Reset all graphic/shape's position
        glutSwapBuffers()                                   # Important for double buffering

    def get_width(self):
        return self.__width

    def get_height(self):
        return self.__height

    @classmethod
    def set_window(cls, window):
        cls.__WIND = window

I already tried to include the glutInit function into the loader.py script, with no success. I also tried to figure out in which scripts the glGenVertexArrays function works, with no real clue.

Asked By: Andreas Sabelfeld

||

Answers:

In my main.py function I called the create_vao method before I even created the window. Swapping the order did the trick.

Answered By: Andreas Sabelfeld
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.