def __init__(self, scene, screen_width=100, screen_height=100): self.scene = scene self.ray_generator = RayGenerator( self.scene.direction, screen_width, screen_height ) self.ray_tracer = RayTracer(scene) self.screen = Screen(screen_width, screen_height)
def run(): window_surface = pygame.display.set_mode((400, 400)) sphere = Sphere(Point(0, 0, 0), radius=1.) view_point = ViewPoint(Point(0, 0, -5.1)) pixels = pygame.PixelArray(window_surface) image = Image(Rectangle(Point(-1, -1, -4.1), Vector(0.005, 0., 0.), Vector(0, 0.005, 0.)), pixels) ray_generator = RayGenerator(view_point, image) rays = ray_generator.generate() theta, phi = 0. , 0. light_source = LightSource(Vector(math.sin(theta)*math.cos(phi), math.sin(theta)*math.sin(phi), math.cos(theta)), 0.5) ray_intersector = RaySphereIntersector(sphere, view_point.point().as_vector()) ray_shader = RayShader() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: theta += 0.1 if event.key == pygame.K_UP: theta -= 0.1 if event.key == pygame.K_LEFT: phi -= 0.1 if event.key == pygame.K_RIGHT: phi += 0.1 window_surface.fill((0, 0, 0,)) draw(rays, ray_intersector, ray_shader, light_source, sphere, image) light_source = LightSource( Vector(math.sin(theta) * math.cos(phi), math.sin(theta) * math.sin(phi), math.cos(theta)), 0.5) pygame.display.flip() print("theta " + str(theta)) print("phi " + str(phi)) print(" ")
class RayTracerMain(object): def __init__(self, scene, screen_width=100, screen_height=100): self.scene = scene self.ray_generator = RayGenerator( self.scene.direction, screen_width, screen_height ) self.ray_tracer = RayTracer(scene) self.screen = Screen(screen_width, screen_height) def trace_scene(self): for (x, y), ray in self.ray_generator.yield_primary_rays(): # TODO: create a ray class to encapsulate ray + position so this # class doesn't have to know about the scene pixel_color = self.ray_tracer.find_pixel_color_for_ray( ray, self.scene.position ) self.screen.write_pixel(x, y, pixel_color) def export_png(self, filename): self.screen.dump_to_png(filename)