def __init__(self, root): self.root = root self.ui_list = ['lab6', 'graphic', 'save-load', 'screw', 'camera'] self.polyhedron = lib.Polyhedron.Cube(lib.Point(0, 0, 0), 100) self.camera = lib.Camera.ortho() self.transform = lib.Transform.identity() self.current = 0 self.menu_var = tk.StringVar() self.menu_var.set(self.ui_list[self.current]) self.menu = tk.OptionMenu(root, self.menu_var, *self.ui_list) self.menu.grid(row=0, column=0) self.frames = [ Lab6(root), Graphic(root), SaveLoad(root), Screw(root), CameraFrame(root) ] self.frames[self.current].grid(row=1, column=0) self.rebind(-1, 0) self.menu_var.trace("w", self.change_menu)
def main(): global cameraFrame cameraFrame = CameraFrame() cameraFrame.moveBack(2) pygame.init() screen = pygame.display.set_mode((512,512), OPENGL|DOUBLEBUF) clock = pygame.time.Clock() init() time = 0.0 while True: clock.tick(30) time += 0.01 # Event queued input for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYUP and event.key == K_ESCAPE: return # Polling input is better for a real time camera pressed = pygame.key.get_pressed() # arrow keys for movement: displacement = 0.01 if pressed[K_RIGHT]: cameraFrame.moveRight(displacement) if pressed[K_LEFT]: cameraFrame.moveRight(-displacement) if pressed[K_UP]: cameraFrame.moveBack(-displacement) if pressed[K_DOWN]: cameraFrame.moveBack(displacement) # wasd for rotation: displacement = 0.01 if pressed[K_d]: cameraFrame.yaw(-displacement) if pressed[K_a]: cameraFrame.yaw(displacement) if pressed[K_w]: cameraFrame.pitch(-displacement) if pressed[K_s]: cameraFrame.pitch(displacement) display(time) pygame.display.flip()
def main(): global cameraFrame, screen, proj cameraFrame = CameraFrame() cameraFrame.moveBack(3) pygame.init() pygame.mouse.set_cursor(*pygame.cursors.broken_x) width, height = 1024,768 screen = pygame.display.set_mode((width, height), OPENGL|DOUBLEBUF) # compute projection aspectRatio = float(width)/float(height) near = 0.01 far = 100.0 lens = 4.0 # "longer" lenses mean more telephoto clock = pygame.time.Clock() init() time = 0.0 while True: clock.tick(30) time += 0.01 # Event queued input for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYUP and event.key == K_ESCAPE: return # Polling input is better for a real time camera pressed = pygame.key.get_pressed() # keys for zoom: if pressed[K_z]: lens *= 1.015 if pressed[K_x]: lens /= 1.015 # redo projection: yRatio = 1.0/lens xRatio = aspectRatio*yRatio right = near*xRatio top = near*yRatio proj = projection(near,far,right,top) # arrow keys for movement: movespeed = 0.05 if pressed[K_d] | pressed[K_RIGHT]: cameraFrame.moveRight(movespeed) if pressed[K_a] | pressed[K_LEFT]: cameraFrame.moveRight(-movespeed) if pressed[K_w] | pressed[K_UP]: cameraFrame.moveBack(-movespeed) if pressed[K_s] | pressed[K_DOWN]: cameraFrame.moveBack(movespeed) # mouse for rotation rotspeed = 0.1 mousespeed = 0.5*rotspeed x,y = pygame.mouse.get_pos() if (x > 0) & (y > 0): xDisplacement = x - 0.5*width yDisplacement = y - 0.5*height # normalize: xNormed = xDisplacement/width yNormed = -yDisplacement/height newx = int(x - xDisplacement*mousespeed) newy = int(y - yDisplacement*mousespeed) if (newx != x) | (newy != y): cameraFrame.yaw(-xNormed*xRatio*rotspeed) cameraFrame.pitch(-yNormed*yRatio*rotspeed) pygame.mouse.set_pos((newx,newy)) display(time) pygame.display.flip()