首页 > 解决方案 > 带有 python 和 pygame 的 OpenGL 模型

问题描述

我正在尝试使用 pygame、OpenGL 和 numpy 制作 3d 模型,在运行此脚本时遇到以下错误

   Traceback (most recent call last:
    File "C:\Users\[I]Username[/I]\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\latebind.py", line 41, in __call__
    return self._finalCall(*args,**named)
   TypeError: 'NoneType' object is not callable
   During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
   File "C:\Users\[I]Username[/I]\Desktop\Python-graphic-game\3d-game\model.py", line 60, in <module>
   main()
File "C:\Users\[I]Username[/I]\Desktop\Python-graphic-game\3d-game\model.py", line 56, in main
m.draw()
File "C:\Users\[I]Username[/I]\Desktop\Python-graphic-game\3d-game\model.py", line 35, in draw
glVertex3fv(self.vertices[vertex])
File "C:\Users\[I]Username[/I]\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\latebind.py", line 45, in __call__
    return self._finalCall(*args,**named)
File "C:\Users\[I]Username[/I]\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\wrapper.py", line 675, in wrapper call
pyArgs = tuple(calculate_pyArgs(args))
File "C:\Users\[I]Username[/I]\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\wrapper.py", line 436, in calculate_pyArgs
yield converter(args[index], self, args)
File "C:\Users\[I]Username[/I]\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenGL\arrays\arrayhelpers.py", line 122, in asArraySize
incoming,
ValueError: ('Expected 12 byte array, got 8 byte array', (0,1), <function asArrayTypeSize.<locals>.asArraySize at 0x09212588>)

这是有问题的代码:

import sys, pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy

class threeDModel:
    nodes = [
        [0,0,0],
        [1.1,1.1,0],
        [0,-1.1,0],
        [0,-1.1,-1.1]
    ]
    vertices = [
        (0,1),
        (0,2),
        (0,3),
        (1,2),
        (1,3),
        (2,3)
    ]
    surfaces = (
        (0,1,2),
        (0,2,3)
    )
    def __init__(self):
        self.nodes = threeDModel.nodes
        self.vertices = threeDModel.vertices
        self.surfaces = threeDModel.surfaces
    def draw(self):
        glBegin(GL_TRIANGLES)
        for surface in self.surfaces:
            for vertex in surface:
                glColor3f(1,0,0)
                glVertex3fv(vertices[vertex])
        glEnd()

那么有人可以帮助我吗?

这是我第一次使用 OpenGL,所以请对我好。

我也在使用 Tech with Tim 的教程,这就是我使用 from module import * 的原因,即使我知道我不应该使用它。

提前致谢!

标签: openglpygamepython-3.6

解决方案


vertices是类的一个属性threeDModel。所以它必须是self.vertices

但是属性的内容.vertices是对(元组)的列表。

vertices = [(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)]

可能您想绘制.nodes而不是.vertices

class threeDModel:

    # [...]

    def draw(self):
        glColor3f(1,0,0)
        glBegin(GL_TRIANGLES)
        for surface in self.surfaces:
            for i in surface:
                glVertex3fv(self.nodes[i])
        glEnd()

或者您想绘制边缘(线框):

class threeDModel:

    # [...]
    def drawEdges(self):
        glColor3f(1,1,0)
        glBegin(GL_LINES)
        for edges in self.vertices:
            for i in edges:
                glVertex3fv(self.nodes[i])
        glEnd()

推荐阅读