【发布时间】:2020-10-18 20:59:29
【问题描述】:
我尝试使用 pyopengl 读取 obj 文件,但错误来自 glGenList(1) 我只想读取文件并在opengl中显示 请你帮我提供完整的代码;
厌倦了堆栈和错误,我花了 3 个小时才发布这个 从 OpenGL.GL 导入 * 从 tkinter 导入消息框 导入系统
filename = input('pls input file path: ')
class OBJ:
def __init__(self, filename, swapyz=False):
"""Loads a Wavefront OBJ file. """
self.vertices = []
self.normals = []
self.texcoords = []
self.faces = []
material = None
for line in open(filename, "r"):
if line.startswith('#'): continue
values = line.split()
#print(values)
if not values: continue
if values[0] == 'v':
v = list(map(float, values[1:4]))
if swapyz:
v = (v[0], v[2], v[1])
self.vertices.append(v)
elif values[0] == 'vn':
v = list(map(float, values[1:4]))
if swapyz:
v = (v[0], v[2], v[1])
self.normals.append(v)
elif values[0] == 'vt':
self.texcoords.append(map(float, values[1:3]))
elif values[0] == 'mtllib':
continue
elif values[0] == 'f':
face = []
texcoords = []
norms = []
for v in values[1:]:
w = v.split('/')
face.append(int(w[0]))
if len(w) >= 2 and len(w[1]) > 0:
texcoords.append(int(w[1]))
else:
texcoords.append(0)
if len(w) >= 3 and len(w[2]) > 0:
norms.append(int(w[2]))
else:
norms.append(0)
self.faces.append((face, norms, texcoords))
#print(self.faces)
self.gl_list = glGenLists(0)
glNewList(self.gl_list, GL_COMPILE)
#glEnable(GL_TEXTURE_2D)
#glFrontFace(GL_CCW)
for face in self.faces:
vertices, normals, texture_coords = face
glBegin(GL_POLYGON)
for i in range(len(vertices)):
if normals[i] > 0:
glNormal3fv(self.normals[normals[i] - 1])
if texture_coords[i] > 0:
glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
glVertex3fv(self.vertices[vertices[i] - 1])
glEnd()
glEndList()
OBJ(filename,swapyz=True)
【问题讨论】:
标签: python opengl pyopengl wavefront opengl-compat