def main(): WIDTH = 800 HEIGHT = 400 camera = Vector(0, 0, -1) objects = [ Sphere(Point(0, 0, 0), 0.5, Material(Color.fromHEX("#FF0000"))), Sphere(Point(1, 0, 0), 0.5, Material(Color(0, 1, 0))), Sphere(Point(-1, 0, 0), 0.5, Material(Color(0, 0, 1))) ] lights = [Light(Point(10, 5, -10.0), Color.fromHEX("#FFFFFF"))] scene = Scene(camera, objects, lights, WIDTH, HEIGHT) engine = RenderEngine() image = engine.render(scene) with open("test.ppm", "w") as img_file: image.writePPM(img_file)
def __init__(self, color=Color.fromHEX("#FFFFFF"), ambient=0.05, diffuse=1.0, specular=1.0): self.color = color self.ambient = ambient self.diffuse = diffuse self.specular = specular
def colorAt(self, objHit, hitPos, normal, scene): material = objHit.material objColor = material.colorAt(hitPos) toCam = scene.camera - hitPos specular_k = 50 color = material.ambient * Color.fromHEX("#000000") # Light Calculations for light in scene.lights: toLight = Ray(hitPos, light.position - hitPos) # Diffuse shading (Lambert) color += objColor * material.diffuse * max( normal.dotProduct(toLight.direction), 0) # Specular Shading halfVector = (toLight.direction + toCam).normalize() color += light.color * material.specular * max( normal.dotProduct(halfVector), 0)**specular_k return color
def __init__(self, position, color=Color.fromHEX("#FFFFFF")): self.position = position self.color = color