预期 LP_c_float 实例而不是 glVertex3fv 中的元组

expected LP_c_float instance instead of tuple in glVertex3fv

我正在使用 Python、OpenGL 和 Pyglet 渲染 3D 立方体,因此我在元组中定义了 verticesedges 变量

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))

之后,我定义了一个函数 def Cube(),该函数 创建 使用我在上面定义的坐标

这个 3D 立方体
def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

所以我创建了一个带有 on_draw() 函数的 window,它调用了 Cube() 函数。

当我 运行 在我的 Linux 终端上使用命令 python3 main.py 这个应用程序时,我得到以下错误

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

所以我猜这段代码的主要错误是行 glVertex3fv(vertices[vertex])

我想知道我需要什么才能正确绘制这个 3D 立方体。

glVertex3fv 的参数必须是 float 的数组。

要解决问题,请使用 glVertex3f 并传递 3 个分隔值:

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

或者使用pythons ctypes模块生成float个数组。
元素的数据类型是 c_float and an Array 可以由 * 运算符生成:

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