def drawGL(self): if self.shader is not None: import numpy as np self.shader.bind() current_time = time.time() angle_x = self.rotation[0] * current_time angle_y = self.rotation[1] * current_time angle_z = self.rotation[2] * current_time mvp_rotX = np.matrix( [[1, 0, 0, 0], [0, np.cos(angle_x), -np.sin(angle_x), 0], [0, np.sin(angle_x), np.cos(angle_x), 0], [0, 0, 0, 1]], dtype=np.float32) mvp_rotY = np.matrix( [[np.cos(angle_y), 0, np.sin(angle_y), 0], [0, 1, 0, 0], [-np.sin(angle_y), 0, np.cos(angle_y), 0], [0, 0, 0, 1]], dtype=np.float32) mvp_rotZ = np.matrix( [[np.cos(angle_z), -np.sin(angle_z), 0, 0], [np.sin(angle_z), np.cos(angle_z), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], dtype=np.float32) mvp = mvp_rotX * mvp_rotY * mvp_rotZ mvp[0:3, 0:3] *= 0.25 self.shader.setUniform("modelViewProj", mvp) gl.Enable(gl.DEPTH_TEST) self.shader.drawIndexed(gl.TRIANGLES, 0, 12) gl.Disable(gl.DEPTH_TEST)
def draw_grid(shader, vp_mat): """Draws a simple grid plane in the viewport.""" grid_extent = 5 grid_step = 0.5 r = np.arange(-grid_extent, grid_extent + 1e-2, grid_step, dtype=np.float32) pos0 = np.zeros([3, r.shape[0] * 2], dtype=np.float32) pos0[:, 0::2] = np.stack( [r, np.zeros_like(r), -grid_extent * np.ones_like(r)]) pos0[:, 1::2] = np.stack([r, np.zeros_like(r), grid_extent * np.ones_like(r)]) pos1 = np.zeros([3, r.shape[0] * 2], dtype=np.float32) pos1[:, 0::2] = np.stack( [-grid_extent * np.ones_like(r), np.zeros_like(r), r]) pos1[:, 1::2] = np.stack([grid_extent * np.ones_like(r), np.zeros_like(r), r]) colors = 0.5 * np.ones(pos0.shape, dtype=np.float32) colors[:, colors.shape[1] // 2] *= 0 colors[:, colors.shape[1] // 2 - 1] *= 0 shader.setUniform("modelViewProj", vp_mat) shader.uploadAttrib("position", pos0) shader.uploadAttrib("color", colors) shader.drawArray(gl.LINES, 0, pos0.shape[1]) shader.setUniform("modelViewProj", vp_mat) shader.uploadAttrib("position", pos1) shader.uploadAttrib("color", colors) shader.drawArray(gl.LINES, 0, pos1.shape[1]) # Draw axis on top gl.Disable(gl.DEPTH_TEST) pos0 = np.zeros([3, 6], dtype=np.float32) pos0[0, 1] = grid_extent pos0[1, 3] = grid_extent pos0[2, 5] = grid_extent colors = np.ones(pos0.shape, dtype=np.float32) colors[1:, 0:2] *= 0 colors[2, 2:4] *= 0 colors[0, 2:4] *= 0 colors[0:2, 4:6] *= 0 shader.setUniform("modelViewProj", vp_mat) shader.uploadAttrib("position", pos0) shader.uploadAttrib("color", colors) shader.drawArray(gl.LINES, 0, pos0.shape[1]) gl.Enable(gl.DEPTH_TEST)
def draw_full_screen_quad(shader, vp_mat, depth_map): """Draws a simple grid plane in the viewport.""" tex = depth_map glBindTexture(GL_TEXTURE_2D, tex) glUniform1i(shader.uniform("renderedTexture"), 0) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, tex) gl.Disable(gl.DEPTH_TEST) pos0 = np.array([[-1.0, -1.0, 0.0], [1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]], dtype=np.float32).T uv = pos0[:2, :] * 0.5 + 0.5 pos0 *= 0.5 shader.uploadAttrib("position", pos0) shader.uploadAttrib("uv", uv) shader.drawArray(gl.TRIANGLES, 0, pos0.shape[1]) gl.Enable(gl.DEPTH_TEST)
def drawGL(self): gl.Enable(gl.DEPTH_TEST) self.view.draw() gl.Disable(gl.DEPTH_TEST)