Pyopengl 读取 obj 文件 [英] Pyopengl reading obj file
问题描述
我试图用 pyopengl 读取 obj 文件,但错误来自 glGenList(1)我只想读取文件并在 opengl 中显示请你能帮我提供完整的代码吗;
我厌倦了堆栈和错误,我花了 3 小时才发布这个从 OpenGL.GL 导入 *从 tkinter 导入消息框导入系统
Am tired of stack and error it took me 3hrd to post this from OpenGL.GL import * from tkinter import messagebox import sys
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)
推荐答案
对于任何 OpenGL 指令,一个有效且最新的 OpenGL 上下文 是必需的.在创建显示列表之前,请确保您已经创建了 OpenGL 窗口.
For any OpenGL instruction a valid and current OpenGL Context is required. Make sure that you have created the OpenGL window before creating the display list.
还有第二个问题,在第一个问题解决后,调用 glNewList
时会导致错误.您想创建 1 个以上的显示列表对象,因此 的参数glGenLists
必须至少为 1:
There is a 2nd issue, that will cause an error when glNewList
is called, after the 1st issue has been solved. You want to create more than 1 display lists objects, hence the argument to glGenLists
has to be at least 1:
self.gl_list = glGenLists(0)
self.gl_list = glGenLists(1)
这篇关于Pyopengl 读取 obj 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!