-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmodel.py
More file actions
275 lines (225 loc) · 11.4 KB
/
model.py
File metadata and controls
275 lines (225 loc) · 11.4 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from OpenGL.GL import *
from OpenGL.GLUT import *
import PIL.Image as Image
from ctypes import c_float, c_void_p, sizeof
import numpy as np
def generate_grid_mesh(start, end, step=1.0):
shape = int((end - start) // step)
vertices = []
indices = []
r = np.arange(start, end, step)
for i in range(shape):
row_max = (i + 1) * shape - 1 # 行最大元素
for j in range(shape):
column_max = (shape - 1) * shape + j # 列最大元素
c_index = i * shape + j # 当前索引位置
c_right = c_index + 1 # 当前索引位置右边一个位置
c_down = c_index + shape # 当前索引下面一个位置
if c_right <= row_max: # 如果索引超过最右边
indices.extend([c_index, c_right])
if c_down <= column_max: # 如果索引超过最下边
indices.extend([c_index, c_down])
vertices.extend([r[i], r[j], 0, 0, 0])
return np.array(vertices, dtype=np.float32), indices
class Mesh:
def __init__(self, mesh, mesh_name, indices, texture_path, vertex_format):
self.vertices = mesh # include vertex positions and texture coords
self.indices = indices
self.name = mesh_name
self.vertex_format = vertex_format
self.vao = glGenVertexArrays(1)
self.init_data(texture_path)
def init_data(self, texture_path):
vbo = glGenBuffers(1)
glBindVertexArray(self.vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, self.vertices, GL_STATIC_DRAW)
if self.vertex_format == "VTN":
glVertexAttribPointer(0, 3, GL_FLOAT, False, 8 * sizeof(c_float), c_void_p(0 * sizeof(c_float)))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, False, 8 * sizeof(c_float), c_void_p(3 * sizeof(c_float)))
glEnableVertexAttribArray(1)
glVertexAttribPointer(2, 3, GL_FLOAT, False, 8 * sizeof(c_float), c_void_p(5 * sizeof(c_float)))
glEnableVertexAttribArray(2)
elif self.vertex_format == "VN":
glVertexAttribPointer(0, 3, GL_FLOAT, False, 6 * sizeof(c_float), c_void_p(0 * sizeof(c_float)))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, False, 6 * sizeof(c_float), c_void_p(3 * sizeof(c_float)))
glEnableVertexAttribArray(1)
elif self.vertex_format == "V":
glVertexAttribPointer(0, 3, GL_FLOAT, False, 3 * sizeof(c_float), c_void_p(0 * sizeof(c_float)))
glEnableVertexAttribArray(0)
else:
glVertexAttribPointer(0, 3, GL_FLOAT, False, 5 * sizeof(c_float), c_void_p(0 * sizeof(c_float)))
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, False, 5 * sizeof(c_float), c_void_p(3 * sizeof(c_float)))
glEnableVertexAttribArray(1)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
if texture_path:
if type(texture_path) == list:
self.texture = [self.load_texture(path) for path in texture_path]
else:
self.texture = self.load_texture(texture_path)
else:
self.texture = None
def load_texture(self, texture_path):
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
im = Image.open(texture_path)
# ix, iy, image = im.size[0], im.size[1], np.array(list(im.getdata()), dtype=np.uint8)
try:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBA", 0, -1)
except:
ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
glGenerateMipmap(GL_TEXTURE_2D)
return texture
def draw(self, shader_program, draw_type):
shader_program.use()
if self.texture:
if type(self.texture) == list:
for index, t in enumerate(self.texture):
glUniform1i(glGetUniformLocation(shader_program.id, "texture_" + str(index)), index)
glActiveTexture(GL_TEXTURE0 + index)
glBindTexture(GL_TEXTURE_2D, t)
else:
glUniform1i(glGetUniformLocation(shader_program.id, "texture1"), 0)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.texture)
glBindVertexArray(self.vao)
if self.indices:
glDrawElements(draw_type, len(self.indices), GL_UNSIGNED_INT, self.indices)
# glDrawArrays(draw_type, 0, int(len(self.vertices) / 6))
else:
vertex_length = 5
if self.vertex_format == "VTN":
vertex_length = 8
if self.vertex_format == "VN":
vertex_length = 6
if self.vertex_format == "V":
vertex_length = 3
glDrawArrays(draw_type, 0, int(len(self.vertices) / vertex_length))
# glBindVertexArray(0)
if self.texture:
glBindTexture(GL_TEXTURE_2D, 0)
class Model:
def __init__(self, meshes, mesh_names=None, indices=None, texture_path=None, vertex_format="VT"):
print(1)
self.meshes = [
Mesh(o, mesh_names[index] if mesh_names else None,
indices[index] if indices and len(indices) > 0 else None,
texture_path[index] if texture_path and len(texture_path) > 0 else None, vertex_format)
for index, o in enumerate(meshes)]
def draw(self, shader_program, draw_type=GL_TRIANGLES):
for mesh in self.meshes:
mesh.draw(shader_program, draw_type)
class ModelFromExport(Model):
def __init__(self, obj_path, vertex_format="VTN"):
self.obj_path = obj_path
meshes, mesh_names, indices, texture_path = self.__load_obj()
super(ModelFromExport, self).__init__(meshes, mesh_names, [], texture_path, vertex_format)
def __load_obj(self):
with open(self.obj_path) as f:
meshes, mesh_names, indices, texture_path = [], [], [], []
vertices, normals, texture_coords, materials = [], [], [], {}
line = f.readline().strip()
while line:
s = line.split()[0]
if s != "#":
if s == "o":
if len(vertices) > 0:
meshes[-1] = np.array(meshes[-1], dtype=np.float32)
mesh_names.append(line.split()[1])
meshes.append([])
indices.append([])
texture_path.append([])
if s == "v":
vertices.append(line.split()[1:])
if s == "vn":
normals.append(line.split()[1:])
if s == "vt":
texture_coords.append(line.split()[1:])
if s == "f":
if len(texture_coords) == 0 and line.find("//") > -1: # no texture //
if len(line.split()[1:]) == 3:
for index, t in enumerate(line.split()[1:]):
v_t_n = [int(i) - 1 for i in t.split('//')]
temp = []
temp.extend(vertices[v_t_n[0]])
temp.extend(normals[v_t_n[1]])
meshes[-1].extend(temp)
else:
for index, t in enumerate(line.split()[1:]):
if index == 3:
break
v_t_n = [int(i) - 1 for i in t.split('//')]
temp = []
temp.extend(vertices[v_t_n[0]])
if len(normals) > 0:
temp.extend(normals[v_t_n[1]])
meshes[-1].extend(temp)
for index, t in enumerate(line.split()[1:]):
if index == 1:
continue
v_t_n = [int(i) - 1 for i in t.split('//')]
temp = []
temp.extend(vertices[v_t_n[0]])
if len(normals) > 0:
temp.extend(normals[v_t_n[1]])
meshes[-1].extend(temp)
elif line.find("/") > -1: # /
for t in line.split()[1:]:
v_t_n = [int(i) - 1 for i in t.split('/')]
temp = []
temp.extend(vertices[v_t_n[0]])
temp.extend(texture_coords[v_t_n[1]])
temp.extend(normals[v_t_n[2]])
meshes[-1].extend(temp)
# indices[-1].append()
else:
for t in line.split()[1:]:
v_t_n = [int(i) - 1 for i in t.split('/')]
temp = []
temp.extend(vertices[v_t_n[0]])
# temp.extend(texture_coords[v_t_n[1]])
# temp.extend(normals[v_t_n[2]])
meshes[-1].extend(temp)
if s == "mtllib":
m = self.__load_mtl(self.obj_path[:self.obj_path.rfind('/') + 1] + line.split()[1])
materials = {}
for i in m:
materials[i["material_name"]] = i.get("texture_path")
if s == "usemtl":
# print(line.split()[1])
path_ = materials[line.split()[1]]
if path_:
texture_path[-1].append(
self.obj_path[:self.obj_path.rfind('/') + 1] + path_)
line = f.readline().strip()
else:
line = f.readline().strip()
meshes[-1] = np.array(meshes[-1], dtype=np.float32)
return meshes, mesh_names, indices, texture_path
def __load_mtl(self, mtl_path):
with open(mtl_path) as f:
materials = []
line = f.readline().strip()
while line:
s = line.split()[0]
if s != "#":
if s == "newmtl":
materials.append({})
materials[-1]["material_name"] = line.split()[1]
if s == "map_Kd":
materials[-1]["texture_path"] = line.split()[1]
line = f.readline().strip()
else:
line = f.readline().strip()
return materials
if __name__ == "__main__":
ModelFromExport("models/hair.obj")