import math from Vector import * from OpenGL.GL import * from OpenGL.GLU import * class Camera: def __init__(self): self.position = Vector([0,0,0]) self.xRot = 0 self.yRot = 0 def applyProjection(self): glMatrixMode(GL_PROJECTION) glLoadIdentity() glMatrixMode(GL_MODELVIEW) def apply(self): self.applyProjection() glLoadIdentity() glRotatef(-self.xRot, 1, 0, 0) glRotatef(-self.yRot, 0, 1, 0) glTranslatef(-self.position[0], -self.position[1], -self.position[2]) def turn(self, angle): self.yRot += angle def pitch(self, angle): self.xRot += angle def moveForward(self, distance): self.move(self.forward() * distance) def moveBackward(self, distance): self.move(self.backward() * distance) def moveLeft(self, distance): self.move(self.left() * distance) def moveRight(self, distance): self.move(self.right() * distance) def moveUp(self, distance): self.move(self.up() * distance) def moveDown(self, distance): self.move(self.down() * distance) def move(self, delta): self.position += delta def forward(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([-math.sin(yr)*math.cos(xr), math.sin(xr), -math.cos(yr) * math.cos(xr)]) def left(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([-math.cos(yr), 0, math.sin(yr)]) def up(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([math.sin(yr)*math.sin(xr), math.cos(xr), math.cos(yr) * math.sin(xr)]) def backward(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([math.sin(yr)*math.cos(xr), -math.sin(xr), math.cos(yr) * math.cos(xr)]) def right(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([math.cos(yr), 0, -math.sin(yr)]) def down(self): xr = self.xRot * math.pi / 180.0 yr = self.yRot * math.pi / 180.0 return Vector([-math.sin(yr)*math.sin(xr), -math.cos(xr), -math.cos(yr) * math.sin(xr)]) class PerspCamera(Camera): def __init__(self, fovy=45.0, aspect=1.0, near=1.0, far=100.0): Camera.__init__(self) self.fovy = fovy self.aspect = aspect self.near = near self.far = far def applyProjection(self): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(self.fovy, self.aspect, self.near, self.far) glMatrixMode(GL_MODELVIEW) def zoom(self, angle): newFovy = self.fovy + angle if (newFovy > 0.0) and (newFovy < 180.0): self.fovy = newFovy class OrthoCamera(Camera): def __init__(self, left=0, right=1, bottom=0, top=1, near=-1, far=1): Camera.__init__(self) self.left = left self.right = right self.bottom = bottom self.top = top self.near = near self.far = far def applyProjection(self): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(self.left, self.right, self.bottom, self.top, self.near, self.far) glMatrixMode(GL_MODELVIEW)