opengl - 带有 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 * 的原因,即使我知道我不应该使用它。
提前致谢!
解决方案
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()
推荐阅读
- python - 如何计算和显示 Django 应用程序/类中的销售价值?
- amazon-s3 - 从深层 S3 路径层次结构中的 CSV 读取 Dask DataFrame
- api-platform.com - 如何在 API Platform Admin 中隐藏列?
- ios - 集合视图单元格缩放未重置
- javascript - 试图使一个字母猜谜游戏的onkeyup函数只接受带有indexOf方法的字母键
- multithreading - 当两个处理器处理的两个线程试图同时访问堆中的单个资源时会发生什么?
- scala - 如何防止 Future 永远不会完成
- python - tox和Travis-CI有什么关系和区别?
- regex - 使用正则表达式从逗号分隔的字符串中提取值
- python - 在 Docker 容器中运行的 Python 脚本未将文件保存到正确的路径