def maybe_update_matrices(self, gl): update_transform = self.must_update_view or self.must_update_projection if self.must_update_view: self.update_view(gl) self.must_update_view = False if self.must_update_projection: self.update_perspective(gl) self.must_update_projection = False if update_transform: # Device coordinates are a square from (-1, -1) to (1, 1). Screen # coordinates are (0, 0) to (width, height). Both coordinates # are useful in different circumstances, so we'll pre-compute # matrices to do the transformations from world coordinates # into each of these. transform_to_device = Matrix4x4.multiply_MM(self.projection_matrix, self.view_matrix) translate = Matrix4x4.create_translation(1.0, 1.0, 0.0) scale = Matrix4x4.create_scaling(self.render_state.screen_width * 0.5, self.render_state.screen_height * 0.5, 1) transform_to_screen = \ Matrix4x4.multiply_MM(Matrix4x4.multiply_MM(scale, translate), transform_to_device) self.render_state.set_tranformation_matrices(transform_to_device, transform_to_screen)
def update_view(self, gl): # Get a vector perpendicular to both, pointing to the right, by taking # lookDir cross up. look_dir = self.render_state.look_dir.copy() up_dir = self.render_state.up_dir.copy() right = cross_product(look_dir, up_dir) if self.DEBUG_MODE != None: from src.units.Vector3 import Vector3 look_dir = Vector3(Debug.LOOKDIRVECTORS[self.DEBUG_MODE][0], Debug.LOOKDIRVECTORS[self.DEBUG_MODE][1], Debug.LOOKDIRVECTORS[self.DEBUG_MODE][2]) up_dir = Vector3(Debug.UPDIRVECTORS[self.DEBUG_MODE][0], Debug.UPDIRVECTORS[self.DEBUG_MODE][1], Debug.UPDIRVECTORS[self.DEBUG_MODE][2]) right = Vector3(Debug.RIGHTVECTORS[self.DEBUG_MODE][0], Debug.RIGHTVECTORS[self.DEBUG_MODE][1], Debug.RIGHTVECTORS[self.DEBUG_MODE][2]) self.view_matrix = Matrix4x4.create_view(look_dir, up_dir, right) gl.glMatrixMode(gl.GL_MODELVIEW) adjust_matrix = Matrix4x4.Matrix4x4([0.0, 0.0, -1.0, 0.0, 0.0, 1.0, -0.0, 0.0, 1.0, 0.0, -0.0, 0.0, 0.0, 0.0, 0.0, 1.0]) matrix = Matrix4x4.multiply_MM(self.view_matrix, adjust_matrix) # Invert the left/right rotation of the matrix matrix.values[2] *= -1 matrix.values[8] *= -1 # Invert these so that we don't rotate in and out of the unit sphere matrix.values[1] *= -1 matrix.values[4] *= -1 matrix = np.array(matrix.values, dtype=np.float32) #matrix = np.array(self.view_matrix.values, dtype=np.float32) gl.glLoadMatrixf(matrix)
def update_perspective(self, gl): if self.DEBUG_MODE != None: self.render_state.radius_of_view = Debug.RADIUSOFVIEW self.projection_matrix = Matrix4x4.create_perspective_projection( self.get_width(), self.get_height(), self.render_state.radius_of_view * 3.141593 / 360.0) gl.glMatrixMode(gl.GL_PROJECTION) matrix = np.array(self.projection_matrix.values, dtype=np.float32) gl.glLoadMatrixf(matrix) # The image is mirrored so scale it to make the polygon fronts into backs gl.glScalef(-1.0, 1.0, 1.0) # Switch back to the model view matrix. gl.glMatrixMode(gl.GL_MODELVIEW)