opengl 在Pyrthon中发送指向glVertexAttributePointer的指针

cyej8jka  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(124)

我需要在Python中传递一个偏移量指针给glVertexAttribPointer。目前,当创建Box类的示例时,我已经退出了pythons的控制台窗口:

C:\Users\phill\PycharmProjects\texturebox\venv\Scripts\python.exe C:\Users\phill\PycharmProjects\texturebox\main.py 
Traceback (most recent call last):
File "C:\Users\phill\PycharmProjects\texturebox\main.py", line 29, in <module>
main()
File "C:\Users\phill\PycharmProjects\texturebox\main.py", line 18, in main
m_box = Box()
        ^^^^^
File "C:\Users\phill\PycharmProjects\texturebox\Box.py", line 60, in __init__
array_colors_offset(*vertices_list)
IndexError: invalid index

Process finished with exit code 1

有人知道在Python中如何传递偏移量指针吗?
(void *)对应的C代码如下所示:

// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

Box.py:

from OpenGL import GL as GL
import ctypes

class Box:
    def __init__(self):
        #3 floats for vertices x,y,z 3 floats for color R G B 2 floats for tex co-ords
        vertices_list = [
            0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
            0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
            -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
            -0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0
        ]

        indices_list = [
            0, 1, 3,
            1, 2, 3
        ]

        self.EBO = None
        self.VBO = None
        self.VAO = None

        self.VAO = GL.glGenVertexArrays(1)

        self.VBO = GL.glGenBuffers(1)
        self.EBO = GL.glGenBuffers(1)

        GL.glBindVertexArray(self.VAO)

        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.VBO)
        array_type_vertices = (GL.GLfloat * len(vertices_list))
        GL.glBufferData(GL.GL_ARRAY_BUFFER, len(vertices_list) * ctypes.sizeof(ctypes.c_float),
                        array_type_vertices(*vertices_list), GL.GL_STATIC_DRAW)

        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.EBO)
        array_type_indices = (GL.GLint * len(indices_list))
        GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, len(indices_list) * ctypes.sizeof(ctypes.c_ulong),
                        array_type_indices(*indices_list), GL.GL_STATIC_DRAW)

        GL.glVertexAttribPointer(
            0,
            3,
            GL.GL_FLOAT,
            False,
            8 * ctypes.sizeof(ctypes.c_float),
            None
        )

        GL.glEnableVertexAttribArray(0)

        # attempting to pass the pointer
        array_colors_offset = (GL.GLfloat * 3)
        GL.glVertexAttribPointer(1,
                                 3,
                                 GL.GL_FLOAT,
                                 False,
                                 8 * ctypes.sizeof(ctypes.c_float),
                                 array_colors_offset(*vertices_list)
                                 )
        GL.glEnableVertexAttribArray(1)

        array_tex_offset = (GL.GLfloat * 6)
        GL.glVertexAttribPointer(2,
                                 2,
                                 GL.GL_FLOAT,
                                 GL.GL_FALSE,
                                 8 * ctypes.sizeof(ctypes.c_float),
                                 array_tex_offset(*vertices_list)
                                  )

        print("Box()")

所以我从gitlabs opengl python的例子开始。基本的三角形似乎没有枚举如何传递一个指针到glVertexAtribPointer。所以根据我的C代码,有一个偏移量作为(void *)传递,我将转移到Python中。

ssm49v7z

ssm49v7z1#

(GL.GLfloat * 6)(*vertices_list)没有执行预期的操作。它创建了一个包含6个元素的数组。
请参阅pyopengl, glVertexAttribPointer。参数必须是void指标。您可以使用ctypes.c_void_p来建立void指标,例如:

GL.glVertexAttribPointer(1,
                         3,
                         GL.GL_FLOAT,
                         False,
                         8 * ctypes.sizeof(ctypes.c_float),
                         ctypes.c_void_p(3 * ctypes.sizeof(ctypes.c_float))
                         )

相关问题