def draw_axises(grayed_out=False): with ms.push_matrix(ms.MatrixStack.model): ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) # x axis with ms.push_matrix(ms.MatrixStack.model): ms.rotate_z(ms.MatrixStack.model, math.radians(-90.0)) glColor3f(1.0, 0.0, 0.0) if grayed_out: glColor3f(0.5, 0.5, 0.5) draw_y_axis() # z glColor3f(0.0, 0.0, 1.0) # blue z with ms.push_matrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) glColor3f(0.0, 0.0, 1.0) if grayed_out: glColor3f(0.5, 0.5, 0.5) draw_y_axis() # y glColor3f(0.0, 1.0, 0.0) # green y if grayed_out: glColor3f(0.5, 0.5, 0.5) draw_y_axis()
if len(axes_list) >= 1 and axes_list[0]: if math.fabs(float(axes_list[0][0])) > 0.19: camera.x += 10.0 * axes_list[0][0] * math.cos(camera.rot_y) camera.z -= 10.0 * axes_list[0][0] * math.sin(camera.rot_y) if math.fabs(float(axes_list[0][1])) > 0.19: camera.x += 10.0 * axes_list[0][1] * math.sin(camera.rot_y) camera.z += 10.0 * axes_list[0][1] * math.cos(camera.rot_y) if math.fabs(axes_list[0][3]) > 0.19: camera.rot_y -= 3.0 * axes_list[0][3] * 0.01 if math.fabs(axes_list[0][4]) > 0.19: camera.rot_x += axes_list[0][4] * 0.01 # note - opengl matricies use degrees ms.rotate_x(ms.MatrixStack.view, -camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) ms.translate(ms.MatrixStack.view, -camera.x, -camera.y, -camera.z) with ms.push_matrix(ms.MatrixStack.model): # draw paddle 1 # Unlike in previous demos, because the transformations # are on a stack, the fns on the model stack can # be read forwards, where each operation translates/rotates/scales # the current space ms.translate( ms.MatrixStack.model, paddle1.position[0], paddle1.position[1], 0.0, ) ms.rotate_z(ms.MatrixStack.model, paddle1.rotation)
# ms.ortho(left=-150.0, # right=150.0, # back=-150.0, # top=150.0, # near=1.0, # far=10000.0) glMatrixMode(GL_PROJECTION) # ascontiguousarray puts the array in column major order glLoadMatrixf( np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.projection).T)) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -moving_camera_r) ms.rotate_x(ms.MatrixStack.view, moving_camera_rot_x) ms.rotate_y(ms.MatrixStack.view, -moving_camera_rot_y) glMatrixMode(GL_MODELVIEW) draw_ground() if animation_time < 5.0 or (animation_time > 40.0 and animation_time < 45.0): draw_axises() else: draw_axises(grayed_out=True) with ms.PushMatrix(ms.MatrixStack.model): if animation_time > 5.0: # draw paddle 1 ms.translate( ms.MatrixStack.model,
# set the projection matrix to be ortho if NDC: ms.ortho(left=-1.0, right=1.0, back=-1.0, top=1.0, near=0.0, far=550.0) else: ms.ortho(left=-100.0, right=100.0, back=-100.0, top=100.0, near=0.0, far=550.0) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -camera.r) ms.rotate_x(ms.MatrixStack.view, camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) # draw NDC in global space, so that we can see the camera space # go to NDC with ms.PushMatrix(ms.MatrixStack.model): cube.render(animation_time) with ms.PushMatrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) ground.render(animation_time) if animation_time > 60.0: ms.translate( ms.MatrixStack.model, -virtual_camera_position[0] * min(1.0, (animation_time - 60.0) / 5.0),
def handle_inputs() -> None: global rotation_around_paddle1 if glfw.get_key(window, glfw.KEY_E) == glfw.PRESS: rotation_around_paddle1 += 0.1 global square_rotation if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS: square_rotation += 0.1 global camera move_multiple = 15.0 if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS: camera.rot_y -= 0.03 if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS: camera.rot_y += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_UP) == glfw.PRESS: camera.rot_x += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_DOWN) == glfw.PRESS: camera.rot_x -= 0.03 # NEW -- reason about moving in 3D space just like placing # objects. If the up button is pressed if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS: # describe the vector to move forwards. # -1 unit in the z direction is forwards # # Although we have not covered linear algebra and # matrix multiplication in depth, all coordinates # in OpenGL are in 4D space, (x,y,z,w), # where to convert to NDC, use (x/w,y/w,z/w) # # For most purposes, by setting w to 1, we can # think in normal 3D space # forwards = np.array([0.0, 0.0, -1.0, 1.0]) # push matrix on the view stack, as we are going # to use the view matrix to determine the new position, # but then will reset the value of the view matrix with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ forwards) if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS: backwards = np.array([0.0, 0.0, 1.0, 1.0]) with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ backwards) global paddle1, paddle2 if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS: paddle1.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS: paddle1.position[1] += 10.0 if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS: paddle2.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS: paddle2.position[1] += 10.0 global paddle_1_rotation, paddle_2_rotation if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS: paddle1.rotation += 0.1 if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS: paddle1.rotation -= 0.1 if glfw.get_key(window, glfw.KEY_J) == glfw.PRESS: paddle2.rotation += 0.1 if glfw.get_key(window, glfw.KEY_L) == glfw.PRESS: paddle2.rotation -= 0.1
# ms.ortho(left=-150.0, # right=150.0, # back=-150.0, # top=150.0, # near=1.0, # far=10000.0) glMatrixMode(GL_PROJECTION) # ascontiguousarray puts the array in column major order glLoadMatrixf( np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.projection).T)) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -camera.r) ms.rotate_x(ms.MatrixStack.view, camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) glMatrixMode(GL_MODELVIEW) # draw NDC in global space, so that we can see the camera space # go to NDC with ms.PushMatrix(ms.MatrixStack.model): ms.scale(ms.MatrixStack.model, 5.0, 5.0, 5.0) draw_ndc() draw_ground() if animation_time > 80.0: ms.rotate_x( ms.MatrixStack.model, -virtual_camera_rot_x * min(1.0, (animation_time - 80.0) / 5.0),
def render(self, time, grayed_out=False): glDisable(GL_DEPTH_TEST) glUseProgram(self.shader) glBindVertexArray(self.vao) with ms.push_matrix(ms.MatrixStack.model): # x axis with ms.push_matrix(ms.MatrixStack.model): ms.rotate_z(ms.MatrixStack.model, math.radians(-90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 1.0, 0.0, 0.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # z # glColor3f(0.0,0.0,1.0) # blue z with ms.push_matrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 0.0, 1.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # y if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 1.0, 0.0) # glColor3f(0.0,1.0,0.0) # green y if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32 ), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32 ), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray( ms.getCurrentMatrix(ms.MatrixStack.projection), dtype=np.float32 ), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) glBindVertexArray(0) glEnable(GL_DEPTH_TEST)
def render(self, time, grayed_out=False): glDisable(GL_DEPTH_TEST) glUseProgram(self.shader) glBindVertexArray(self.vao) # pass projection parameters to the shader fov_loc = glGetUniformLocation(self.shader, "fov") glUniform1f(fov_loc, 45.0) aspect_loc = glGetUniformLocation(self.shader, "aspectRatio") glUniform1f(aspect_loc, 1.0) nearZ_loc = glGetUniformLocation(self.shader, "nearZ") glUniform1f(nearZ_loc, -5.0) farZ_loc = glGetUniformLocation(self.shader, "farZ") glUniform1f(farZ_loc, -150.00) # TODO, set the color with ms.push_matrix(ms.MatrixStack.model): # x axis with ms.push_matrix(ms.MatrixStack.model): ms.rotate_z(ms.MatrixStack.model, math.radians(-90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 1.0, 0.0, 0.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # z # glColor3f(0.0,0.0,1.0) # blue z with ms.push_matrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 0.0, 1.0) if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) # y if enlarged_axis: ms.scale(ms.MatrixStack.model, 10.0, 10.0, 10.0) glUniform3f(self.colorLoc, 0.0, 1.0, 0.0) # glColor3f(0.0,1.0,0.0) # green y if grayed_out: glUniform3f(self.colorLoc, 0.5, 0.5, 0.5) # ascontiguousarray puts the array in column major order glUniformMatrix4fv( self.mMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.model), dtype=np.float32), ) glUniformMatrix4fv( self.vMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.view), dtype=np.float32), ) glUniformMatrix4fv( self.pMatrixLoc, 1, GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.projection), dtype=np.float32), ) glDrawArrays(GL_LINES, 0, self.numberOfVertices) glBindVertexArray(0) glEnable(GL_DEPTH_TEST)