def update(dt):
    global cameraPos, cameraRot

    wasCameraModified = False

    if keysDown[key.W]:
        acceleration = cameraRot.mulByVec(Vector3(0, 0, -cameraSpeed*dt))
        cameraPos = cameraPos.add(acceleration)
        wasCameraModified = True
    elif keysDown[key.S]:
        acceleration = cameraRot.mulByVec(Vector3(0, 0, cameraSpeed*dt))
        cameraPos = cameraPos.add(acceleration)
        wasCameraModified = True

    if keysDown[key.A]:
        acceleration = cameraRot.mulByVec(Vector3(-cameraSpeed*dt, 0, 0))
        cameraPos = cameraPos.add(acceleration)
        wasCameraModified = True
    elif keysDown[key.D]:
        acceleration = cameraRot.mulByVec(Vector3(cameraSpeed*dt, 0, 0))
        cameraPos = cameraPos.add(acceleration)
        wasCameraModified = True

    if keysDown[key.LEFT]:
        deltaRot = Quaternion.fromAxisAngle(Vector3(0,1,0), cameraRotSpeed*dt)
        cameraRot = deltaRot.mulByQuat(cameraRot)
        wasCameraModified = True
    elif keysDown[key.RIGHT]:
        deltaRot = Quaternion.fromAxisAngle(Vector3(0,1,0), -cameraRotSpeed*dt)
        cameraRot = deltaRot.mulByQuat(cameraRot)
        wasCameraModified = True

    if keysDown[key.UP]:
        deltaRot = Quaternion.fromAxisAngle(Vector3(1,0,0), -cameraRotSpeed*dt)
        cameraRot = cameraRot.mulByQuat(deltaRot)
        wasCameraModified = True
    elif keysDown[key.DOWN]:
        deltaRot = Quaternion.fromAxisAngle(Vector3(1,0,0), cameraRotSpeed*dt)
        cameraRot = cameraRot.mulByQuat(deltaRot)
        wasCameraModified = True

    if wasCameraModified:
        updateCameraLookVectors()
        updateCameraFrustum()
        chunkStore.setCamera(cameraPos, cameraRot, cameraFrustum)

    chunkStore.update(dt)
def on_key_press(symbol, modifiers):
    global useWireframe, keysDown, cameraRot

    keysDown[symbol] = True

    if symbol == key.R:
        useWireframe = not useWireframe
    elif symbol == key.I:
        cameraRot = Quaternion.fromAxisAngle(Vector3(0,1,0), 0)
    elif symbol == key.P:
        print "Camera Position:", cameraPos
        print "Camera Rotation:", Quaternion.toAxisAngle(cameraRot)
        print "Camera Frustum:", cameraFrustum
    elif symbol == key.ESCAPE:
        chunkStore.sync()
        pyglet.app.exit()

    return pyglet.event.EVENT_HANDLED
        map(Chunk.draw, self.visibleChunks)
        glDisableClientState(GL_TEXTURE_COORD_ARRAY)
        glDisableClientState(GL_NORMAL_ARRAY)
        glDisableClientState(GL_VERTEX_ARRAY)


    def setCamera(self, p, r, fr):
        self.cameraPos = p
        self.cameraRot = r
        self.cameraFrustum = fr
        self._updateInternalActiveAndVisibleChunksCache()


if __name__ == "__main__":
    cameraPos = Vector3(0,0,0)
    cameraRot = Quaternion.fromAxisAngle(Vector3(0,1,0), 0)
    cameraSpeed = 5.0
    cameraRotSpeed = 1.0
    cameraFrustum = Frustum()
    cameraEye = Vector3(0,0,0)
    cameraCenter = Vector3(0,0,0)
    cameraUp = Vector3(0,0,0)

    cameraEye, cameraCenter, cameraUp = \
        math3D.getCameraEyeCenterUp(cameraPos, cameraRot)
    cameraFrustum.setCamInternals(65, 640.0/480.0, .1, 1000)
    cameraFrustum.setCamDef(cameraEye, cameraCenter, cameraUp)

    chunkStore = ChunkStore(0)
    chunkStore.setCamera(cameraPos, cameraRot, cameraFrustum)