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()
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) # 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)
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), ) if animation_time > 75.0: ms.rotate_y( ms.MatrixStack.model, -virtual_camera_rot_y * min(1.0, (animation_time - 75.0) / 5.0), ) if animation_time > 70.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
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)