Valeria Vega
Graphics Python Samples
#################################
# Valeria Vega
# Sample 1
# Computer Graphics
#################################
import math
from matrix import *
from ray import *
import raster
class Camera(object):
"""Generic parent class for OrthographicCamera and PerspectiveCamera."""
def __init__(self, location, up, normal, width, height):
"""Initializer takes the location in the world, a vector pointing up, a vector perpendicular to the plane (normal), a width and a height, which are essential elements for the focal plane of the camera."""
self.location = location
self.up = up.normalize()
self.normal = normal.normalize()
self.width = width
self.height = height
def generateRay(point):
"""Unimplemented class that must be able to generate a Ray for each screen-space coordinate. Takes a point as an argument."""
pass
class OrthographicCamera(Camera):
"""Returns a ray originated from screen coordinates. """
def __init__(self,location, up, normal, width, height):
"""Initializer takes as input the location of the lower left hand corner of a focal plane, a vector pointing up, a vector normal to the plane, and a width and height (in scene/world-coordinates)."""
self.location = location
self.up = up.normalize()
self.normal = normal.normalize()
self.width = width
self.height = height
super(OrthographicCamera, self).__init__(location, up, normal, width, height)
def generateRay(self, point):
"""generateRay takes a Point2d with x,y values between 0-1 and returns a Ray with the appropriate origin and direction. """
horizontal = self.up.cross(self.normal).normalize() #This gives us the horizontal and normalizes it
origin = self.location.add(self.up.scalarMult(point.y * self.height)).add(horizontal.scalarMult(point.x * self.width)) #Points of origin
ray = Ray(self.normal, origin) #takes as arguments the direction and the origin
return ray
class PerspectiveCamera(Camera):
"""Returns a ray originated from the virtual eye. """
def __init__(self,location, up, normal, width, he