This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| light = { 'position': np.array([5, 5, 5]), 'ambient': np.array([1, 1, 1]), 'diffuse': np.array([1, 1, 1]), 'specular': np.array([1, 1, 1]) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| objects = [ | |
| { 'center': np.array([-0.2, 0, -1]), 'radius': 0.7, 'ambient': np.array([0.1, 0, 0]), 'diffuse': np.array([0.7, 0, 0]), 'specular': np.array([1, 1, 1]), 'shininess': 100 }, | |
| { 'center': np.array([0.1, -0.3, 0]), 'radius': 0.1, 'ambient': np.array([0.1, 0, 0.1]), 'diffuse': np.array([0.7, 0, 0.7]), 'specular': np.array([1, 1, 1]), 'shininess': 100 }, | |
| { 'center': np.array([-0.3, 0, 0]), 'radius': 0.15, 'ambient': np.array([0, 0.1, 0]), 'diffuse': np.array([0, 0.6, 0]), 'specular': np.array([1, 1, 1]), 'shininess': 100 } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| intersection = origin + min_distance * direction | |
| normal_to_surface = normalize(intersection - nearest_object['center']) | |
| shifted_point = intersection + 1e-5 * normal_to_surface | |
| intersection_to_light = normalize(light['position'] - shifted_point) | |
| _, min_distance = nearest_intersected_object(objects, shifted_point, intersection_to_light) | |
| intersection_to_light_distance = np.linalg.norm(light['position'] - intersection) | |
| is_shadowed = min_distance < intersection_to_light_distance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| intersection = origin + min_distance * direction | |
| intersection_to_light = normalize(light['position'] - intersection) | |
| _, min_distance = nearest_intersected_object(objects, intersection, intersection_to_light) | |
| intersection_to_light_distance = np.linalg.norm(light['position'] - intersection) | |
| is_shadowed = min_distance < intersection_to_light_distance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def normalize(vector): | |
| return vector / np.linalg.norm(vector) | |
| def sphere_intersect(center, radius, ray_origin, ray_direction): | |
| b = 2 * np.dot(ray_direction, ray_origin - center) | |
| c = np.linalg.norm(ray_origin - center) ** 2 - radius ** 2 | |
| delta = b ** 2 - 4 * c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| objects = [ | |
| { 'center': np.array([-0.2, 0, -1]), 'radius': 0.7 }, | |
| { 'center': np.array([0.1, -0.3, 0]), 'radius': 0.1 }, | |
| { 'center': np.array([-0.3, 0, 0]), 'radius': 0.15 } | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nearest_intersected_object(objects, ray_origin, ray_direction): | |
| distances = [sphere_intersect(obj['center'], obj['radius'], ray_origin, ray_direction) for obj in objects] | |
| nearest_object = None | |
| min_distance = np.inf | |
| for index, distance in enumerate(distances): | |
| if distance and distance < min_distance: | |
| min_distance = distance | |
| nearest_object = objects[index] | |
| return nearest_object, min_distance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sphere_intersect(center, radius, ray_origin, ray_direction): | |
| b = 2 * np.dot(ray_direction, ray_origin - center) | |
| c = np.linalg.norm(ray_origin - center) ** 2 - radius ** 2 | |
| delta = b ** 2 - 4 * c | |
| if delta > 0: | |
| t1 = (-b + np.sqrt(delta)) / 2 | |
| t2 = (-b - np.sqrt(delta)) / 2 | |
| if t1 > 0 and t2 > 0: | |
| return min(t1, t2) | |
| return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def normalize(vector): | |
| return vector / np.linalg.norm(vector) | |
| width = 300 | |
| height = 200 | |
| camera = np.array([0, 0, 1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| width = 300 | |
| height = 200 | |
| camera = np.array([0, 0, 1]) | |
| ratio = float(width) / height | |
| screen = (-1, 1 / ratio, 1, -1 / ratio) # left, top, right, bottom |