-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mesh.py
166 lines (130 loc) · 6.13 KB
/
Mesh.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
import numpy as np
import MeshData
import MaterialData
import Texture
from Shader import *
from OpenGL.GL import *
'''
this class loads .obj and .mat files from harddrive and render it on screen
'''
class Mesh:
def __init__(self , OBJname , MATname): #loads .obj and .mat files
self.meshData = []
self.materialData = []
self.count = 0
self.VBOs = []
self.VAOs = []
self.shader = Shader("./shaders/diffuseSpec") #loads the master shader
self.textures = []
self.loadMesh(OBJname)
self.loadMaterial(MATname)
self.loadDataInGPU()
def loadMaterial(self , fname):
for line in open(fname, 'r'):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'o':
data = []
data = MaterialData.MaterialData()
self.materialData.append(data)
if values[0] == 'diffuse':
data.diffuse = values[2]
self.textures.append(Texture.Texture("./res/" + data.diffuse))
if values[0] == 'specularIntensity':
data.specularIntensity = np.float32(float(values[2]))
if values[0] == 'specularPower':
data.specularPower = np.float32(float(values[2]))
if values[0] == 'reflectAmt':
data.reflectAmt = np.float32(float(values[2]))
if values[0] == 'reflectColor':
data.reflectColor[0] = np.float32(float(values[1]))
data.reflectColor[1] = np.float32(float(values[2]))
data.reflectColor[2] = np.float32(float(values[3]))
if values[0] == 'refractAmt':
data.refractAmt = np.float32(float(values[2]))
if values[0] == 'refractColor':
data.refractColor[0]= np.float32(float(values[1]))
data.refractColor[1]= np.float32(float(values[2]))
data.refractColor[2]= np.float32(float(values[3]))
def loadMesh(self, fname):
data = []
for line in open(fname, 'r'):
if line.startswith('#'): continue
values = line.split()
if not values: continue
if values[0] == 'o':
data = []
data = MeshData.MeshData()
self.meshData.append(data)
self.count = self.count + 1
for c in values[1:]:
data.name += c
if values[0] == 'v':
data.vert_coords.append(values[1:4])
if values[0] == 'vt':
data.text_coords.append(values[1:3])
if values[0] == 'vn':
data.norm_coords.append(values[1:4])
if values[0] == 'f':
face_i = []
text_i = []
norm_i = []
for v in values[1:4]:
w = v.split('/')
vOffset = 0
tOffset = 0
nOffset = 0
if (self.count >= 2):
for g in range(self.count - 1):
vOffset += len(self.meshData[g].vert_coords)
tOffset += len(self.meshData[g].text_coords)
nOffset += len(self.meshData[g].norm_coords)
face_i.append(int(w[0]) - 1 - vOffset)
text_i.append(int(w[1]) - 1 - tOffset)
norm_i.append(int(w[2]) - 1 - nOffset)
data.vertex_index.append(face_i)
data.texture_index.append(text_i)
data.normal_index.append(norm_i)
for k in range(self.count):
self.meshData[k].vertex_index = [y for x in self.meshData[k].vertex_index for y in x]
self.meshData[k].texture_index = [y for x in self.meshData[k].texture_index for y in x]
self.meshData[k].normal_index = [y for x in self.meshData[k].normal_index for y in x]
for i in self.meshData[k].vertex_index:
self.meshData[k].model.extend(self.meshData[k].vert_coords[i])
for i in self.meshData[k].texture_index:
self.meshData[k].model.extend(self.meshData[k].text_coords[i])
for i in self.meshData[k].normal_index:
self.meshData[k].model.extend(self.meshData[k].norm_coords[i])
self.meshData[k].model = np.array(self.meshData[k].model, dtype='float32')
def loadDataInGPU(self):
for i in range(self.count):
texture_offset = len(self.meshData[i].vertex_index) * 12
normal_offset = (texture_offset + len(self.meshData[i].texture_index) * 8)
self.VAOs.append(glGenVertexArrays(1))
glBindVertexArray(self.VAOs[i])
self.VBOs.append(glGenBuffers(1))
glBindBuffer(GL_ARRAY_BUFFER, self.VBOs[i])
glBufferData(GL_ARRAY_BUFFER, self.meshData[i].model.itemsize * len(self.meshData[i].model),
self.meshData[i].model, GL_STATIC_DRAW)
# position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 3,
ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# texture
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 2,
ctypes.c_void_p(texture_offset))
glEnableVertexAttribArray(1)
#normal
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 3,
ctypes.c_void_p(normal_offset))
glEnableVertexAttribArray(2)
glBindVertexArray(0)
def renderALL(self , view, projection, model):
self.shader.bind()
for i in range(self.count):
self.textures[i].bind(GL_TEXTURE0)
self.shader.updateUniforms(view , projection , model ,self.materialData[i] )
glBindVertexArray(self.VAOs[i])
glDrawArrays(GL_TRIANGLES, 0, len(self.meshData[i].vertex_index))
glBindVertexArray(0)