def resize(self, width, height): glViewport(0, 0, width, height) self.perspective_mat = hm.perspective(hm.identity(), 60, float(width) / height, 0.1, 6.0) self.modelview_mat = hm.lookat(hm.identity(), self.campos, self.center) self.mvp = np.dot(self.perspective_mat, self.modelview_mat)
def loadsquirrel(self): # When loading the data, pad the normals to 4 floats (16bytes) since GPUs hate unaligned memory. obj = wf.ObjFileParser("squirrel.obj", padnormals = 4) self.objscale = 1 / np.max(obj.scale / 2) self.objcenter = obj.minpos + (obj.scale / 2) self.obj_mat = hm.scale(hm.identity(), [self.objscale * 0.2] * 3) self.obj_mat = hm.translation(self.obj_mat, -self.objcenter) # Generate a GL compatible indexed vertex buffer for the object self.vbdata, ibdata = obj.generateIndexedBuffer([0,1], np.uint16) vbdata = self.vbdata self.elementnum = np.shape(ibdata)[0] # VAO self.vertobj = glGenVertexArrays(1) glBindVertexArray(self.vertobj) # Setup the VBO for the vertex data. self.vertbuf = VBO(vbdata, GL_STATIC_DRAW, GL_ARRAY_BUFFER) self.vertbuf.bind() glVertexAttribPointer(self.positionloc, 4, GL_FLOAT, GL_TRUE, 8 * 4, ctypes.c_void_p(0)) glVertexAttribPointer(self.normalloc, 4, GL_FLOAT, GL_TRUE, 8 * 4, ctypes.c_void_p(16)) glEnableVertexAttribArray(self.positionloc) glEnableVertexAttribArray(self.normalloc) # Indexbuffer self.indexbuf = VBO(ibdata, GL_STATIC_DRAW, GL_ELEMENT_ARRAY_BUFFER) self.indexbuf.bind() glBindVertexArray(0) self.vertbuf.unbind() self.indexbuf.unbind() #Animation init... self.rotation = 0
def display(self, timediff): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.shader.use() glUniformMatrix4fv(self.objploc, 1, GL_TRUE, np.dot(hm.rotation(hm.identity(), self.rotation, [0,1,0]), self.obj_mat).tolist()) glUniformMatrix4fv(self.mvploc, 1, GL_TRUE, hm.rotation(self.mvp, self.rotation / 4, [0.577350269189626] * 3).tolist()) glBindVertexArray(self.vertobj) glDrawElementsInstanced(GL_TRIANGLES, self.elementnum, GL_UNSIGNED_SHORT, ctypes.c_void_p(0), 125) glBindVertexArray(0) self.rotation += timediff / 5 * 360