Skip to content

Commit

Permalink
Refactor color into separate class, breaks Codon (exaloop/codon#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenchi2xh committed Apr 28, 2024
1 parent 81dc002 commit 4a4b3db
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion rtow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .ray import Ray
from .vec3 import Color, Point3, Vec3
from .vec3 import Point3, Vec3
from .color import Color
from .interval import Interval
from .aabb import AABB
7 changes: 4 additions & 3 deletions rtow/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from typing import Callable, List, Tuple

from .tracer import Tracer, Camera
from .vec3 import Vec3, Point3, Color
from .vec3 import Vec3, Point3
from .color import Color
from .objects import Sphere, HittableList
from .materials import Lambertian, Metal, Dielectric
from .textures import Checker
Expand All @@ -13,7 +14,7 @@
def bouncing_spheres():
world = HittableList()

checker = Checker.from_colors(0.32, Color(0.2, 0.3, 0.1), Color.all(0.9))
checker = Checker.from_colors(0.32, Color(0.2, 0.3, 0.1), Color.gray(0.9))
world.add(Sphere(1000, Lambertian(checker), Point3(0, -1000, 0)))

for a in range(-11, 11):
Expand Down Expand Up @@ -64,7 +65,7 @@ def bouncing_spheres():
def checkered_spheres():
world = HittableList()

checker = Checker.from_colors(0.32, Color(0.2, 0.3, 0.1), Color.all(0.9))
checker = Checker.from_colors(0.32, Color(0.2, 0.3, 0.1), Color.gray(0.9))

world.add(Sphere(10, Lambertian(checker), Point3(0, -10, 0)))
world.add(Sphere(10, Lambertian(checker), Point3(0, 10, 0)))
Expand Down
4 changes: 2 additions & 2 deletions rtow/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .ppm import save
from .interval import Interval
from .vec3 import Color
from .color import Color, black


intensity = Interval(0.000, 0.999)
Expand All @@ -21,7 +21,7 @@ class Buffer:
def __init__(self, w: int, h: int):
self.w = w
self.h = h
self.buffer = [[Color() for x in range(w)] for y in range(h)]
self.buffer = [[black for x in range(w)] for y in range(h)]

def save_ppm(self, name: str):
save(self.raw_buffer(), name)
Expand Down
21 changes: 21 additions & 0 deletions rtow/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .vec3 import Vec3


class Color(Vec3):
r: float
g: float
b: float

def __init__(self, r: float = 0, g: float = 0, b: float = 0):
super().__init__(r, g, b)
self.r = r
self.g = g
self.b = b

@staticmethod
def gray(intensity: float):
return Color(intensity, intensity, intensity)


black = Color(0, 0, 0)
white = Color(1, 1, 1)
3 changes: 2 additions & 1 deletion rtow/materials/lambertian.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .. import Ray, Color, Vec3
from ..objects import Hit
from ..textures import Texture, SolidColor
from ..color import black
from .material import Material, Scatter


Expand All @@ -14,7 +15,7 @@ def __init__(self, texture: Texture):
self.texture = texture

@staticmethod
def from_color(albedo: Color = Color(0, 0, 0)):
def from_color(albedo: Color = black):
return Lambertian(SolidColor(albedo))

def scatter(self, r_in: Ray, hit: Hit) -> Optional[Scatter]:
Expand Down
3 changes: 2 additions & 1 deletion rtow/materials/metal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from .. import Ray, Color, Vec3
from ..objects import Hit
from ..color import black
from .material import Material, Scatter


class Metal(Material):
albedo: Color
fuzz: float

def __init__(self, albedo: Color = Color(0, 0, 0), fuzz: float = 0.0):
def __init__(self, albedo: Color = black, fuzz: float = 0.0):
self.albedo = albedo
self.fuzz = fuzz

Expand Down
11 changes: 6 additions & 5 deletions rtow/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from .interval import Interval
from .objects import Hittable, HittableList
from .ray import Ray
from .vec3 import Color, Point3, Vec3
from .vec3 import Point3, Vec3
from .color import Color, black, white
from .bvh import BVHNode
from .camera import Camera

Expand Down Expand Up @@ -83,7 +84,7 @@ def __init__(
def ray_color(self, r: Ray, depth: int, world: Hittable) -> Color:
# If we've exceeded the ray bounce limit, no more light is gathered
if depth <= 0:
return Color(0, 0, 0)
return black

# Min distance is 0.001 to avoid floating point precision errors
# That way if the ray starts just below a surface,
Expand All @@ -92,12 +93,12 @@ def ray_color(self, r: Ray, depth: int, world: Hittable) -> Color:

if rec:
if self.mode == "normals":
return 0.5 * (rec.hit.normal + Color(1, 1, 1))
return 0.5 * (rec.hit.normal + white)

scatter = rec.mat.scatter(r, rec.hit)
if scatter:
return scatter.attenuation * self.ray_color(scatter.scattered, depth - 1, world)
return Color(0, 0, 0)
return black

# Sky
unit_direction = r.direction.unit()
Expand Down Expand Up @@ -136,7 +137,7 @@ def render(self, world: HittableList) -> Buffer:
for j in range(self.image_height):
row = []
for i in range(self.image_width):
pixel_color = Color(0, 0, 0)
pixel_color = black
for _ in range(self.samples_per_pixel):
r = self.get_ray(i, j)
pixel_color += self.ray_color(r, self.max_depth, bvh)
Expand Down
1 change: 0 additions & 1 deletion rtow/vec3.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def random_on_hemisphere(normal: Vec3):


Point3 = Vec3
Color = Vec3


if __name__ == "__main__":
Expand Down

0 comments on commit 4a4b3db

Please sign in to comment.