diff --git a/src/main/kotlin/Hit.kt b/src/main/kotlin/Hit.kt index a97525c..54a9f97 100644 --- a/src/main/kotlin/Hit.kt +++ b/src/main/kotlin/Hit.kt @@ -1 +1,5 @@ +import materials.Material +import math.Point +import math.Vector + data class Hit(val point: Point, val normal: Vector, val distance: Float, val material: Material) \ No newline at end of file diff --git a/src/main/kotlin/PointLight.kt b/src/main/kotlin/PointLight.kt deleted file mode 100644 index 08569bd..0000000 --- a/src/main/kotlin/PointLight.kt +++ /dev/null @@ -1 +0,0 @@ -data class PointLight(val point: Point, val color: MaterialColor) \ No newline at end of file diff --git a/src/main/kotlin/Ray.kt b/src/main/kotlin/Ray.kt index 0383cd2..8e2a2ea 100644 --- a/src/main/kotlin/Ray.kt +++ b/src/main/kotlin/Ray.kt @@ -1,11 +1,16 @@ +import lights.PointLight +import materials.MaterialColor +import materials.ReflectiveMaterial +import materials.WhateverMaterial +import materials.times +import math.Point +import math.Vector +import things.Plane +import things.Scene +import things.Sphere import java.io.File -data class Ray(val origin: Point, val direction: Vector) { - fun at(t: Float) = origin + t * direction -} - - fun main() { val bmp = Bitmap(1000, 1000) val origin = Point(500f, 500f, -500f) diff --git a/src/main/kotlin/lights/Light.kt b/src/main/kotlin/lights/Light.kt new file mode 100644 index 0000000..24acf27 --- /dev/null +++ b/src/main/kotlin/lights/Light.kt @@ -0,0 +1,3 @@ +package lights + +interface Light \ No newline at end of file diff --git a/src/main/kotlin/lights/PointLight.kt b/src/main/kotlin/lights/PointLight.kt new file mode 100644 index 0000000..7e9bcb1 --- /dev/null +++ b/src/main/kotlin/lights/PointLight.kt @@ -0,0 +1,6 @@ +package lights + +import materials.MaterialColor +import math.Point + +data class PointLight(val point: Point, val color: MaterialColor) : Light \ No newline at end of file diff --git a/src/main/kotlin/Material.kt b/src/main/kotlin/materials/Material.kt similarity index 55% rename from src/main/kotlin/Material.kt rename to src/main/kotlin/materials/Material.kt index 1de21fb..168b7f8 100644 --- a/src/main/kotlin/Material.kt +++ b/src/main/kotlin/materials/Material.kt @@ -1,3 +1,9 @@ +package materials + +import Hit +import math.Ray +import things.Scene + interface Material { fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor } \ No newline at end of file diff --git a/src/main/kotlin/MaterialColor.kt b/src/main/kotlin/materials/MaterialColor.kt similarity index 95% rename from src/main/kotlin/MaterialColor.kt rename to src/main/kotlin/materials/MaterialColor.kt index c16ca2d..0e192b3 100644 --- a/src/main/kotlin/MaterialColor.kt +++ b/src/main/kotlin/materials/MaterialColor.kt @@ -1,3 +1,7 @@ +package materials + +import Color + data class MaterialColor(val r: Float, val g: Float, val b: Float) { fun toColor(): Color = Color( (r * UByte.MAX_VALUE.toFloat()).toInt().toUByte(), diff --git a/src/main/kotlin/ReflectiveMaterial.kt b/src/main/kotlin/materials/ReflectiveMaterial.kt similarity index 93% rename from src/main/kotlin/ReflectiveMaterial.kt rename to src/main/kotlin/materials/ReflectiveMaterial.kt index c9d41b1..068d818 100644 --- a/src/main/kotlin/ReflectiveMaterial.kt +++ b/src/main/kotlin/materials/ReflectiveMaterial.kt @@ -1,3 +1,9 @@ +package materials + +import Hit +import math.Ray +import things.Scene +import math.times import kotlin.math.pow data class ReflectiveMaterial( diff --git a/src/main/kotlin/WhateverMaterial.kt b/src/main/kotlin/materials/WhateverMaterial.kt similarity index 91% rename from src/main/kotlin/WhateverMaterial.kt rename to src/main/kotlin/materials/WhateverMaterial.kt index a59b0c6..0730fbe 100644 --- a/src/main/kotlin/WhateverMaterial.kt +++ b/src/main/kotlin/materials/WhateverMaterial.kt @@ -1,3 +1,9 @@ +package materials + +import Hit +import math.Ray +import things.Scene +import math.times import kotlin.math.pow data class WhateverMaterial(val color: MaterialColor, diff --git a/src/main/kotlin/Point.kt b/src/main/kotlin/math/Point.kt similarity index 95% rename from src/main/kotlin/Point.kt rename to src/main/kotlin/math/Point.kt index 4d60505..e9c5ef4 100644 --- a/src/main/kotlin/Point.kt +++ b/src/main/kotlin/math/Point.kt @@ -1,6 +1,8 @@ +package math + data class Point(val x: Float, val y: Float, val z: Float) { operator fun plus(rhs: Vector) : Point = Point(x + rhs.x, y + rhs.y, z + rhs.z) operator fun minus(rhs: Point): Vector = Vector(x - rhs.x, y - rhs.y, z - rhs.z) fun rayTo(other: Point) = Ray(this, (other - this).normalized()) -} +} \ No newline at end of file diff --git a/src/main/kotlin/math/Ray.kt b/src/main/kotlin/math/Ray.kt new file mode 100644 index 0000000..2553567 --- /dev/null +++ b/src/main/kotlin/math/Ray.kt @@ -0,0 +1,5 @@ +package math + +data class Ray(val origin: Point, val direction: Vector) { + fun at(t: Float) = origin + t * direction +} \ No newline at end of file diff --git a/src/main/kotlin/Vector.kt b/src/main/kotlin/math/Vector.kt similarity index 98% rename from src/main/kotlin/Vector.kt rename to src/main/kotlin/math/Vector.kt index 69753c6..8bc2a96 100644 --- a/src/main/kotlin/Vector.kt +++ b/src/main/kotlin/math/Vector.kt @@ -1,3 +1,5 @@ +package math + import kotlin.math.sqrt data class Vector(val x: Float, val y: Float, val z: Float) { diff --git a/src/main/kotlin/Plane.kt b/src/main/kotlin/things/Plane.kt similarity index 77% rename from src/main/kotlin/Plane.kt rename to src/main/kotlin/things/Plane.kt index 97a91a6..be77d21 100644 --- a/src/main/kotlin/Plane.kt +++ b/src/main/kotlin/things/Plane.kt @@ -1,3 +1,11 @@ +package things + +import Hit +import math.Point +import math.Ray +import math.Vector +import materials.Material + data class Plane(val point: Point, val normal: Vector, val material: Material) : Thing { override fun intersects(ray: Ray): Hit? { val bottom = normal dot ray.direction diff --git a/src/main/kotlin/Scene.kt b/src/main/kotlin/things/Scene.kt similarity index 85% rename from src/main/kotlin/Scene.kt rename to src/main/kotlin/things/Scene.kt index 3acab8e..974ce7c 100644 --- a/src/main/kotlin/Scene.kt +++ b/src/main/kotlin/things/Scene.kt @@ -1,3 +1,10 @@ +package things + +import Hit +import materials.MaterialColor +import lights.PointLight +import math.Ray + data class Scene(val things: List, val light: PointLight): Thing { override fun intersects(ray: Ray): Hit? { var closest: Hit? = null diff --git a/src/main/kotlin/Sphere.kt b/src/main/kotlin/things/Sphere.kt similarity index 87% rename from src/main/kotlin/Sphere.kt rename to src/main/kotlin/things/Sphere.kt index 2978c82..4e6f23e 100644 --- a/src/main/kotlin/Sphere.kt +++ b/src/main/kotlin/things/Sphere.kt @@ -1,3 +1,9 @@ +package things + +import Hit +import math.Point +import math.Ray +import materials.Material import kotlin.math.min import kotlin.math.sqrt diff --git a/src/main/kotlin/Thing.kt b/src/main/kotlin/things/Thing.kt similarity index 55% rename from src/main/kotlin/Thing.kt rename to src/main/kotlin/things/Thing.kt index 62fc0f2..aa1636a 100644 --- a/src/main/kotlin/Thing.kt +++ b/src/main/kotlin/things/Thing.kt @@ -1,3 +1,8 @@ +package things + +import Hit +import math.Ray + interface Thing { fun intersects(ray: Ray): Hit? } \ No newline at end of file