Pls I have been trying this for past few month but all prevail I tried alot of online resources to read and display OBJ file without MTl stuff all in vain, I write my codes still errors so I finally arrive at my self made code that can read obj file of any faces type e.g (v//vn)(v/by/vn)(v) I hope you understand this. After I read the file I got no error but rendering with pyopengl I got no error but the object didn't show. Pls check my obj loader code, and the opengl code in the screen with the result in another screenshot. Note I got no error I don't know if I wrote something wrong in the obj loader but pls check and help me pls. OBJ code:
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')
Rendering code
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()