首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >渲染OBJ pyopengl

渲染OBJ pyopengl
EN

Stack Overflow用户
提问于 2020-06-30 19:25:18
回答 1查看 121关注 0票数 1

在过去的几个月里,我一直在尝试这个方法,但是我尝试了很多在线资源来读取和显示OBJ文件,没有MTl的东西都是徒劳的,我写了我的代码仍然错误,所以我终于得到了我自己的代码,可以读取任何人脸类型的obj文件,例如(v//vn)(v/by/vn)(v)我希望你能理解这一点。在我读取文件后,我没有得到任何错误,但是使用pyopengl渲染,我没有得到任何错误,但是对象没有显示。请检查我的obj加载器代码,以及屏幕上的opengl代码,结果在另一个屏幕截图中。注意我没有得到错误,我不知道我是否写错了在obj加载程序,但请检查并帮助我,请。OBJ代码:

代码语言:javascript
复制
from OpenGL.GL import *
from pyopengltk import OpenGLFrame
class OBJ:
    def __init__(self,filename):
    self.faces = []
    self.vertices = []
    with open(filename, 'r') as f:
        lines = f.readlines()
        #print(lines)
        if lines:
            for line in lines:
                if line[0] == 'v':
                    index1 = line.find(' ')
                    index2 = line.find(' ', index1+1)
                    index3 = line.find(' ', index2+1)

                    v1 = float(line[index1: index2])
                    v2 = float(line[index2: index3])
                    v3 = float(line[index3:])
                    self.vertices.append((v1,v2,v3))
                    #print(self.vertices)
        for line in open(filename, "r"):
            face = []
            texcoords = []
            norms = []
            if line.startswith('#'): continue
            values = line.split()
            #print(values)
            if not values: continue
            if values[0] == 'v':continue
            elif values[0] == 'vn':continue
            elif values[0] == 'vt':continue
            if values[0] == 'f':
                for v in values[1:]:
                    #print(len(v))
                    if len(v) == 4:
                        if ['/' for s in values[1:]]:
                            w = v.split('//')
                            face.append(int(w[0]))
                            norms.append(int(w[1]))
                            self.faces.extend(tuple((face, norms)))
                            #self.faces = [y for x in self.faces for y in x]
                            #print(self.faces)
                            glBegin(GL_QUADS)
                            glColor3f(0,1,1)
                            for surface in self.faces:
                                for vertex in surface:
                                    glVertex3fv(self.vertices[vertex])
                            glEnd
                        else:
                            self.faces.append(v)
                            glBegin(GL_QUADS)
                            glColor3f(0,1,1)
                            for surface in self.faces:
                                for vertex in surface:
                                    glVertex3fv(self.vertices[vertex])
                            glEnd
                            print(self.faces)
                    elif len(v) == 3:
                        w = v.split('/')
                        face.append(int(w[0]))
                        texcoords.append(int(w[1]))
                        norms.append(int(w[2]))
                        self.faces.append((face, norms, texcoord))
                        glBegin(GL_TRIANGLES)
                        glColor3f(0,1,1)
                        for surface in self.faces:
                            for vertex in surface:
                                glVertex3fv(self.vertices[vertex])
                        glEnd
                        #print('v/vt/vn')
                        print(self.faces)
#to run this code just use file path to any obj file on your pc
#OBJ('filename_path')

呈现代码

代码语言:javascript
复制
import tkinter as tk
from OpenGL.GL import *
from OpenGL.GLU import *
from pyopengltk import OpenGLFrame
from obj import *

class frame(OpenGLFrame):

  def initgl(self):
        glViewport(0,0,self.width,self.height)
        glClearColor(0.902,0.902,1,0.0)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #glOrtho(-10,10,-10,10,-10,10)
        gluPerspective(60, self.width/self.height,0.1,100.0)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glEnable(GL_CULL_FACE)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        
  
  def redraw(self):
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
        OBJ('C:\\Users\\DANIEL\\Desktop\\v-cam\\me.obj')
        #that is what I did here obj suff
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0,-10,0,0,0,0,1,0,20)
        

root = tk.Tk()
app = frame(root, width=900, height=600)
app.pack(fill=tk.BOTH,expand=tk.YES)
app.mainloop()

EN

回答 1

Stack Overflow用户

发布于 2020-06-30 21:08:56

我推荐使用您在前面的一个问题(Pyopengl reading obj file)中介绍的对象加载器。

initgl中创建OBJ实例

代码语言:javascript
复制
self.obj =  OBJ('C:\\Users\\DANIEL\\Desktop\\v-cam\\me.obj')

然后在redraw中使用glCallList绘制网格。

代码语言:javascript
复制
glCallList(self.obj.gl_list)

frame

代码语言:javascript
复制
class frame(OpenGLFrame):

    def initgl(self):
        glViewport(0,0,self.width,self.height)
        glClearColor(0.902,0.902,1,0.0)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        #glOrtho(-10,10,-10,10,-10,10)
        gluPerspective(60, self.width/self.height,0.1,100.0)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glEnable(GL_CULL_FACE)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        self.obj =  OBJ('C:\\Users\\DANIEL\\Desktop\\v-cam\\me.obj')
  
    def redraw(self):
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
        #that is what I did here obj suff
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0,-10,0,0,0,0,1,0,20) 

        glCallList(self.obj.gl_list)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62656363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档