Skip to content

Commit 49a6cf0

Browse files
committed
Shade an intersection in the world
(Implemented using Anthropic's Claude)
1 parent 70ba52f commit 49a6cf0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/main/scala/World.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,18 @@ def intersectWorld(w: World, r: ray.Ray): intersection.Intersections = {
5555
def contains(w: World, s: sphere.Sphere): Boolean = {
5656
w.objects.contains(s)
5757
}
58+
59+
def shadeHit(w: World, comps: intersection.Computations): color.Color = {
60+
w.lightSource match {
61+
case Some(light) =>
62+
material.lighting(
63+
comps.obj.objectMaterial,
64+
light,
65+
comps.point,
66+
comps.eyev,
67+
comps.normalv
68+
)
69+
case None =>
70+
color.Color(0, 0, 0) // No light source, return black
71+
}
72+
}

src/test/scala/WorldSuite.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,33 @@ class WorldSuite extends munit.FunSuite {
9292

9393
assertEquals(contains(w, randomSphere), false)
9494
}
95+
96+
test("Shading an intersection") {
97+
val w = defaultWorld()
98+
val r = ray.ray(tuple.makePoint(0, 0, -5), tuple.makeVector(0, 0, 1))
99+
val shape = w.objects(0) // the first object in w
100+
val i = com.samuelcantrell.raytracer.intersection.intersection(4, shape)
101+
val comps =
102+
com.samuelcantrell.raytracer.intersection.prepareComputations(i, r)
103+
val c = shadeHit(w, comps)
104+
val expected = color.Color(0.38066, 0.47583, 0.2855)
105+
106+
assertEquals(color.isEqual(c, expected, 0.00001), true)
107+
}
108+
109+
test("Shading an intersection from the inside") {
110+
val w = defaultWorld()
111+
val newLight =
112+
light.pointLight(tuple.makePoint(0, 0.25, 0), color.Color(1, 1, 1))
113+
val wWithNewLight = w.copy(lightSource = Some(newLight))
114+
val r = ray.ray(tuple.makePoint(0, 0, 0), tuple.makeVector(0, 0, 1))
115+
val shape = wWithNewLight.objects(1) // the second object in w
116+
val i = com.samuelcantrell.raytracer.intersection.intersection(0.5, shape)
117+
val comps =
118+
com.samuelcantrell.raytracer.intersection.prepareComputations(i, r)
119+
val c = shadeHit(wWithNewLight, comps)
120+
val expected = color.Color(0.90498, 0.90498, 0.90498)
121+
122+
assertEquals(color.isEqual(c, expected, 0.00001), true)
123+
}
95124
}

0 commit comments

Comments
 (0)