Search code examples
pythonopenglpygletopengl-compat

expected LP_c_float instance instead of tuple in glVertex3fv


I'm rendering a 3D cube using Python, OpenGL and Pyglet, so I defined vertices and edges variables in a tuple

vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1))

edges = (
    (0, 1),
    (0, 3),
    (0, 4),
    (2, 1),
    (2, 3),
    (2, 7),
    (6, 3),
    (6, 4),
    (6, 7),
    (5, 1),
    (5, 4),
    (5, 7))

after that i defined a function def Cube() that creates this 3D cube using these coordinates i defined above

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

so i create a window with on_draw() function, that calls the Cube() function.

When i run this application on my Linux terminal using the command python3 main.py i get the following error

ctypes.ArgumentError: argument 1: : expected LP_c_float instance instead of tuple

So i guess the main error of this code is the line glVertex3fv(vertices[vertex])

I want to know what i need to draw this 3D cube correctly.


Solution

  • The parameter to glVertex3fv has to be an array of floats.

    To solve issue, either use glVertex3f and pass 3 separated values:

    glVertex3f(vertices[vertex][0], vertices[vertex][1], vertices[vertex][2])
    

    Or use pythons ctypes module to generate an array of floats.
    The data type of an element is c_float and an Array can be generated by the *-operator:

    import ctypes
    
    glVertex3fv( (ctypes.c_float * 3)(*vertices[vertex]) )