Move code to packages.

This commit is contained in:
Sven Weidauer 2024-12-01 12:28:06 +01:00
parent 687cb784fe
commit 130c8a5bd1
16 changed files with 81 additions and 7 deletions

View file

@ -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) data class Hit(val point: Point, val normal: Vector, val distance: Float, val material: Material)

View file

@ -1 +0,0 @@
data class PointLight(val point: Point, val color: MaterialColor)

View file

@ -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 import java.io.File
data class Ray(val origin: Point, val direction: Vector) {
fun at(t: Float) = origin + t * direction
}
fun main() { fun main() {
val bmp = Bitmap(1000, 1000) val bmp = Bitmap(1000, 1000)
val origin = Point(500f, 500f, -500f) val origin = Point(500f, 500f, -500f)

View file

@ -0,0 +1,3 @@
package lights
interface Light

View file

@ -0,0 +1,6 @@
package lights
import materials.MaterialColor
import math.Point
data class PointLight(val point: Point, val color: MaterialColor) : Light

View file

@ -1,3 +1,9 @@
package materials
import Hit
import math.Ray
import things.Scene
interface Material { interface Material {
fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor
} }

View file

@ -1,3 +1,7 @@
package materials
import Color
data class MaterialColor(val r: Float, val g: Float, val b: Float) { data class MaterialColor(val r: Float, val g: Float, val b: Float) {
fun toColor(): Color = Color( fun toColor(): Color = Color(
(r * UByte.MAX_VALUE.toFloat()).toInt().toUByte(), (r * UByte.MAX_VALUE.toFloat()).toInt().toUByte(),

View file

@ -1,3 +1,9 @@
package materials
import Hit
import math.Ray
import things.Scene
import math.times
import kotlin.math.pow import kotlin.math.pow
data class ReflectiveMaterial( data class ReflectiveMaterial(

View file

@ -1,3 +1,9 @@
package materials
import Hit
import math.Ray
import things.Scene
import math.times
import kotlin.math.pow import kotlin.math.pow
data class WhateverMaterial(val color: MaterialColor, data class WhateverMaterial(val color: MaterialColor,

View file

@ -1,3 +1,5 @@
package math
data class Point(val x: Float, val y: Float, val z: Float) { 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 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) operator fun minus(rhs: Point): Vector = Vector(x - rhs.x, y - rhs.y, z - rhs.z)

View file

@ -0,0 +1,5 @@
package math
data class Ray(val origin: Point, val direction: Vector) {
fun at(t: Float) = origin + t * direction
}

View file

@ -1,3 +1,5 @@
package math
import kotlin.math.sqrt import kotlin.math.sqrt
data class Vector(val x: Float, val y: Float, val z: Float) { data class Vector(val x: Float, val y: Float, val z: Float) {

View file

@ -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 { data class Plane(val point: Point, val normal: Vector, val material: Material) : Thing {
override fun intersects(ray: Ray): Hit? { override fun intersects(ray: Ray): Hit? {
val bottom = normal dot ray.direction val bottom = normal dot ray.direction

View file

@ -1,3 +1,10 @@
package things
import Hit
import materials.MaterialColor
import lights.PointLight
import math.Ray
data class Scene(val things: List<Thing>, val light: PointLight): Thing { data class Scene(val things: List<Thing>, val light: PointLight): Thing {
override fun intersects(ray: Ray): Hit? { override fun intersects(ray: Ray): Hit? {
var closest: Hit? = null var closest: Hit? = null

View file

@ -1,3 +1,9 @@
package things
import Hit
import math.Point
import math.Ray
import materials.Material
import kotlin.math.min import kotlin.math.min
import kotlin.math.sqrt import kotlin.math.sqrt

View file

@ -1,3 +1,8 @@
package things
import Hit
import math.Ray
interface Thing { interface Thing {
fun intersects(ray: Ray): Hit? fun intersects(ray: Ray): Hit?
} }