-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj_model.py
182 lines (166 loc) · 8.46 KB
/
obj_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 25 23:02:04 2019
reference:
http://www.poketcode.com/en/pyglet_demos/index.html#obj_viewer
@author: weilunhuang
"""
import os
import euclid
import pyglet
from triangle import Triangle
from pyglet.gl import gl
# colors
black = (0, 0, 0, 1)
dark_gray = (0.75, 0.75, 0.75, 1)
class OBJModel:
"""
Represents an OBJ model.
"""
def __init__(self, x=0.0,y=0.0,z=0.0, color=dark_gray, path=None):
self.vertices = [];
self.vertex_nomals=[];
self.text_coord=[];
self.quad_indices = [];
self.triangle_indices = [];
self.normal_indices=[];
self.text_indices=[];
self.triangles=[];
self.vertex_list=None;
self.texture=None;
# translation and rotation values
self.x, self.y, self.z = x, y, z;
self.rx = self.rz = 0;
self.ry=0;
# color of the model
self.color = color;
# if path is provided
if path:
self.load(path);
up_dir=os.path.split(path)[0];
self.texture_setting(os.path.join(up_dir+'/Model.jpg'))
def clear(self):
self.vertices = self.vertices[:];
self.vertex_nomals=self.vertex_nomals[:];
self.text_coord=self.text_coord[:];
self.quad_indices = self.quad_indices[:];
self.triangle_indices = self.triangle_indices[:];
self.normal_indices=self.normal_indices[:];
self.text_indices=self.text_indices[:];
self.triangles=self.triangles[:];
def load(self, path):
self.clear();
with open(path) as obj_file:
for line in obj_file.readlines():
# reads the file line by line
data = line.split();
#print(data);
if len(data)==0 or data[0] not in ['v','vn','vt','f']:
continue;
# every line that begins with a 'v' is a vertex
if data[0] == 'v':
x, y, z = data[1:4];
self.vertices.extend((float(x)*10, float(y)*10, float(z)*10));
# every line that begins with a 'vn' is a vertex normal
if data[0] == 'vn':
x_n, y_n, z_n = data[1:4];
self.vertex_nomals.extend((float(x_n), float(y_n), float(z_n)));
# every line that begins with a 'vt' is a text coordinate
if data[0] == 'vt':
t_u, t_v= data[1:3];
self.text_coord.extend((float(t_u), float(t_v)));
# every line that begins with an 'f' is a face
# loads the faces
elif data[0] == 'f':
# quads
# Note: in obj files the first index is 1, so we must subtract one for each
# retrieved value
if len(data) == 5:
vi_1, vi_2, vi_3, vi_4 = data[1:5]
self.quad_indices.extend((int(vi_1) - 1, int(vi_2) - 1, int(vi_3) - 1, int(vi_4) - 1))
# triangles
# Note: in obj files the first index is 1, so we must subtract one for each
# retrieved value
elif len(data) == 4:
#triangles with complete info
vi_1, vi_2, vi_3 = data[1:4]
if '//' in data[1]:
vi_1=vi_1.split('//')
vi_2=vi_2.split('//')
vi_3=vi_3.split('//')
self.triangle_indices.extend((int(vi_1[0]) - 1, int(vi_2[0]) - 1, int(vi_3[0]) - 1))
self.normal_indices.extend((int(vi_1[1]) - 1, int(vi_2[1]) - 1, int(vi_3[1]) - 1))
elif '/' in data[1]:
vi_1=vi_1.split('/')
vi_2=vi_2.split('/')
vi_3=vi_3.split('/')
self.triangle_indices.extend((int(vi_1[0]) - 1, int(vi_2[0]) - 1, int(vi_3[0]) - 1))
self.text_indices.extend((int(vi_1[1]) - 1, int(vi_2[1]) - 1, int(vi_3[1]) - 1))
self.normal_indices.extend((int(vi_1[2]) - 1, int(vi_2[2]) - 1, int(vi_3[2]) - 1))
else:
self.triangle_indices.extend((int(vi_1) - 1, int(vi_2) - 1, int(vi_3) - 1))
points_indices=[];
for i in range(len(self.quad_indices)//4):
#put all indices for a quad into points-indices, and then separate it into 2 triangles
points_indices.extend((self.quad_indices[4*i],self.quad_indices[4*i+1],self.quad_indices[4*i+2],self.quad_indices[4*i+3]));
p1=euclid.Point3(self.vertices[3*points_indices[0]],self.vertices[3*points_indices[0]+1],self.vertices[3*points_indices[0]+2]);
p2=euclid.Point3(self.vertices[3*points_indices[1]],self.vertices[3*points_indices[1]+1],self.vertices[3*points_indices[1]+2]);
p3=euclid.Point3(self.vertices[3*points_indices[2]],self.vertices[3*points_indices[2]+1],self.vertices[3*points_indices[2]+2]);
p4=euclid.Point3(self.vertices[3*points_indices[3]],self.vertices[3*points_indices[3]+1],self.vertices[3*points_indices[3]+2]);
self.triangles.append(Triangle(points_indices[0],points_indices[1],points_indices[2],\
p1,p2,p3));
self.triangles.append(Triangle(points_indices[2],points_indices[3],points_indices[0],\
p3,p4,p1));
points_indices.clear();
points_indices.clear();
for i in range(len(self.triangle_indices)//3):
#put all indices for a triangle into points_indices
points_indices.extend((self.triangle_indices[3*i],self.triangle_indices[3*i+1],self.triangle_indices[3*i+2]));
p1=euclid.Point3(self.vertices[3*points_indices[0]],self.vertices[3*points_indices[0]+1],self.vertices[3*points_indices[0]+2]);
p2=euclid.Point3(self.vertices[3*points_indices[1]],self.vertices[3*points_indices[1]+1],self.vertices[3*points_indices[1]+2]);
p3=euclid.Point3(self.vertices[3*points_indices[2]],self.vertices[3*points_indices[2]+1],self.vertices[3*points_indices[2]+2]);
self.triangles.append(Triangle(points_indices[0],points_indices[1],points_indices[2],\
p1,p2,p3));
points_indices.clear();
def texture_setting(self,pic_path):
if len(self.text_indices)>0:
gl.glEnable(gl.GL_CULL_FACE);
# texture set up
pic = pyglet.image.load(pic_path);
self.texture = pic.get_texture();
print(self.texture.id)
gl.glEnable(self.texture.target);
gl.glBindTexture(self.texture.target, self.texture.id);
self.vertex_list = pyglet.graphics.vertex_list_indexed(len(self.vertices) // 3,self.triangle_indices,\
('v3f',self.vertices),('t2f',self.text_coord),('n3f',self.vertex_nomals));
def subdivision(self):
pass;
def draw(self):
gl.glPushMatrix();
# sets the position
gl.glTranslatef(self.x, self.y, self.z);
# sets the rotation
gl.glRotatef(self.rx, 1, 0, 0);
gl.glRotatef(self.ry, 0, 1, 0);
gl.glRotatef(self.rz, 0, 0, 1);
# sets the color
gl.glColor4f(*self.color);
# draw primitives with batch or vertex_list
# batch=pyglet.graphics.Batch();
# vertex_list=batch.add(len(self.vertices) // 3,gl.GL_QUADS,None,('v3f', self.vertices));
# batch.draw();
# # draw by texture rendering
if len(self.text_indices)>0:
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL );
print("text mode!")
self.vertex_list.draw(gl.GL_TRIANGLES);
# draw by wire-frame mode
else:
# sets wire-frame mode
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
# draws the quads
pyglet.graphics.draw_indexed(len(self.vertices) // 3, gl.GL_QUADS, self.quad_indices, ('v3f', self.vertices))
# draws the triangles
pyglet.graphics.draw_indexed(len(self.vertices) // 3, gl.GL_TRIANGLES, self.triangle_indices,('v3f', self.vertices))
gl.glPopMatrix();