def colour_at(self, obj_hit, hit_pos, normal, scene): material = obj_hit.material obj_colour = material.colour_at(hit_pos) to_cam = scene.camera - hit_pos specular_k = 50 colour = material.ambient * Colour.from_hex("#000000") # Light calculations for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) # Diffuse shading (Lambert) colour += ( obj_colour * material.diffuse * max(normal.dot_product(to_light.direction), 0) ) # Specular shading half_vector = (to_light.direction + to_cam).normalise() colour += ( light.colour * material.specular * max(normal.dot_product(half_vector), 0) ** specular_k ) return colour
def main(): width = 3200 height = 2000 camera = Vector(0, 0, -0.5) lights = [ Light(Point(0, -0.6, -6), Colour.from_hex("#FFFFFF")), Light(Point(5, -0.9, 10), Colour.from_hex("#FFFFFF")), Light(Point(-1, -0.9, 10), Colour.from_hex("#FFFFFF")) ] objects = [ Sphere(Point(-0.53, 0, 1), 0.5, Material(Colour.from_hex("#FF0000"))), Sphere(Point(0.53, 0, 1), 0.5, Material(Colour.from_hex("#F80000"))), Sphere(Point(0, 1000000.5, 1), 1000000, Material(Colour.from_hex("#FFFFFF"))) ] scene = Scene(camera, objects, lights, width, height) engine = RenderEngine() image = engine.render(scene) with open("render.ppm", "w") as img_file: image.write_ppm(img_file)
def __init__(self, colour=Colour.from_hex("#FFFFFF"), ambient=0.5, diffuse=1.0, specular=0.5): self.colour = colour self.ambient = ambient self.diffuse = diffuse self.specular = specular
def __init__(self, position, colour=Colour.from_hex("FFFFFF")): self.position = position self.colour = colour