|
| 1 | +import numpy as np |
| 2 | +import MeshData |
| 3 | +import MaterialData |
| 4 | +import Texture |
| 5 | +from Shader import * |
| 6 | +from OpenGL.GL import * |
| 7 | + |
| 8 | +''' |
| 9 | +this class loads .obj and .mat files from harddrive and render it on screen |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | +class Mesh: |
| 14 | + def __init__(self , OBJname , MATname): #loads .obj and .mat files |
| 15 | + self.meshData = [] |
| 16 | + self.materialData = [] |
| 17 | + self.count = 0 |
| 18 | + self.VBOs = [] |
| 19 | + self.VAOs = [] |
| 20 | + self.shader = Shader("./shaders/diffuseSpec") #loads the master shader |
| 21 | + self.textures = [] |
| 22 | + self.loadMesh(OBJname) |
| 23 | + self.loadMaterial(MATname) |
| 24 | + self.loadDataInGPU() |
| 25 | + |
| 26 | + def loadMaterial(self , fname): |
| 27 | + for line in open(fname, 'r'): |
| 28 | + if line.startswith('#'): continue |
| 29 | + values = line.split() |
| 30 | + if not values: continue |
| 31 | + if values[0] == 'o': |
| 32 | + data = [] |
| 33 | + data = MaterialData.MaterialData() |
| 34 | + self.materialData.append(data) |
| 35 | + if values[0] == 'diffuse': |
| 36 | + data.diffuse = values[2] |
| 37 | + self.textures.append(Texture.Texture("./res/" + data.diffuse)) |
| 38 | + if values[0] == 'specularIntensity': |
| 39 | + data.specularIntensity = np.float32(float(values[2])) |
| 40 | + if values[0] == 'specularPower': |
| 41 | + data.specularPower = np.float32(float(values[2])) |
| 42 | + if values[0] == 'reflectAmt': |
| 43 | + data.reflectAmt = np.float32(float(values[2])) |
| 44 | + if values[0] == 'reflectColor': |
| 45 | + data.reflectColor[0] = np.float32(float(values[1])) |
| 46 | + data.reflectColor[1] = np.float32(float(values[2])) |
| 47 | + data.reflectColor[2] = np.float32(float(values[3])) |
| 48 | + |
| 49 | + if values[0] == 'refractAmt': |
| 50 | + data.refractAmt = np.float32(float(values[2])) |
| 51 | + if values[0] == 'refractColor': |
| 52 | + data.refractColor[0]= np.float32(float(values[1])) |
| 53 | + data.refractColor[1]= np.float32(float(values[2])) |
| 54 | + data.refractColor[2]= np.float32(float(values[3])) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + def loadMesh(self, fname): |
| 59 | + data = [] |
| 60 | + for line in open(fname, 'r'): |
| 61 | + if line.startswith('#'): continue |
| 62 | + values = line.split() |
| 63 | + if not values: continue |
| 64 | + |
| 65 | + if values[0] == 'o': |
| 66 | + data = [] |
| 67 | + data = MeshData.MeshData() |
| 68 | + self.meshData.append(data) |
| 69 | + self.count = self.count + 1 |
| 70 | + for c in values[1:]: |
| 71 | + data.name += c |
| 72 | + |
| 73 | + if values[0] == 'v': |
| 74 | + data.vert_coords.append(values[1:4]) |
| 75 | + if values[0] == 'vt': |
| 76 | + data.text_coords.append(values[1:3]) |
| 77 | + if values[0] == 'vn': |
| 78 | + data.norm_coords.append(values[1:4]) |
| 79 | + |
| 80 | + if values[0] == 'f': |
| 81 | + face_i = [] |
| 82 | + text_i = [] |
| 83 | + norm_i = [] |
| 84 | + for v in values[1:4]: |
| 85 | + w = v.split('/') |
| 86 | + vOffset = 0 |
| 87 | + tOffset = 0 |
| 88 | + nOffset = 0 |
| 89 | + if (self.count >= 2): |
| 90 | + for g in range(self.count - 1): |
| 91 | + vOffset += len(self.meshData[g].vert_coords) |
| 92 | + tOffset += len(self.meshData[g].text_coords) |
| 93 | + nOffset += len(self.meshData[g].norm_coords) |
| 94 | + face_i.append(int(w[0]) - 1 - vOffset) |
| 95 | + text_i.append(int(w[1]) - 1 - tOffset) |
| 96 | + norm_i.append(int(w[2]) - 1 - nOffset) |
| 97 | + data.vertex_index.append(face_i) |
| 98 | + data.texture_index.append(text_i) |
| 99 | + data.normal_index.append(norm_i) |
| 100 | + |
| 101 | + for k in range(self.count): |
| 102 | + self.meshData[k].vertex_index = [y for x in self.meshData[k].vertex_index for y in x] |
| 103 | + self.meshData[k].texture_index = [y for x in self.meshData[k].texture_index for y in x] |
| 104 | + self.meshData[k].normal_index = [y for x in self.meshData[k].normal_index for y in x] |
| 105 | + |
| 106 | + for i in self.meshData[k].vertex_index: |
| 107 | + self.meshData[k].model.extend(self.meshData[k].vert_coords[i]) |
| 108 | + |
| 109 | + for i in self.meshData[k].texture_index: |
| 110 | + self.meshData[k].model.extend(self.meshData[k].text_coords[i]) |
| 111 | + |
| 112 | + for i in self.meshData[k].normal_index: |
| 113 | + self.meshData[k].model.extend(self.meshData[k].norm_coords[i]) |
| 114 | + |
| 115 | + self.meshData[k].model = np.array(self.meshData[k].model, dtype='float32') |
| 116 | + |
| 117 | + |
| 118 | + def loadDataInGPU(self): |
| 119 | + for i in range(self.count): |
| 120 | + texture_offset = len(self.meshData[i].vertex_index) * 12 |
| 121 | + normal_offset = (texture_offset + len(self.meshData[i].texture_index) * 8) |
| 122 | + self.VAOs.append(glGenVertexArrays(1)) |
| 123 | + glBindVertexArray(self.VAOs[i]) |
| 124 | + self.VBOs.append(glGenBuffers(1)) |
| 125 | + |
| 126 | + glBindBuffer(GL_ARRAY_BUFFER, self.VBOs[i]) |
| 127 | + glBufferData(GL_ARRAY_BUFFER, self.meshData[i].model.itemsize * len(self.meshData[i].model), |
| 128 | + self.meshData[i].model, GL_STATIC_DRAW) |
| 129 | + |
| 130 | + # position |
| 131 | + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 3, |
| 132 | + ctypes.c_void_p(0)) |
| 133 | + glEnableVertexAttribArray(0) |
| 134 | + # texture |
| 135 | + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 2, |
| 136 | + ctypes.c_void_p(texture_offset)) |
| 137 | + glEnableVertexAttribArray(1) |
| 138 | + #normal |
| 139 | + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.meshData[i].model.itemsize * 3, |
| 140 | + ctypes.c_void_p(normal_offset)) |
| 141 | + glEnableVertexAttribArray(2) |
| 142 | + |
| 143 | + glBindVertexArray(0) |
| 144 | + |
| 145 | + def renderALL(self , view, projection, model): |
| 146 | + self.shader.bind() |
| 147 | + |
| 148 | + for i in range(self.count): |
| 149 | + self.textures[i].bind(GL_TEXTURE0) |
| 150 | + self.shader.updateUniforms(view , projection , model ,self.materialData[i] ) |
| 151 | + glBindVertexArray(self.VAOs[i]) |
| 152 | + glDrawArrays(GL_TRIANGLES, 0, len(self.meshData[i].vertex_index)) |
| 153 | + glBindVertexArray(0) |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
0 commit comments