带有python和pygame的OpenGL模型

时间:2019-05-21 00:02:47

标签: opengl pygame python-3.6

我正在尝试使用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,请对我好一点。

我还与Tim一起使用Tech的教程,这就是为什么我使用from模块import *的原因,即使我知道我不应该使用它。

谢谢!

1 个答案:

答案 0 :(得分:0)

RabbitListener是类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()