Move code to packages.
This commit is contained in:
parent
687cb784fe
commit
130c8a5bd1
16 changed files with 81 additions and 7 deletions
|
@ -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)
|
|
@ -1 +0,0 @@
|
|||
data class PointLight(val point: Point, val color: MaterialColor)
|
|
@ -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)
|
||||
|
|
3
src/main/kotlin/lights/Light.kt
Normal file
3
src/main/kotlin/lights/Light.kt
Normal file
|
@ -0,0 +1,3 @@
|
|||
package lights
|
||||
|
||||
interface Light
|
6
src/main/kotlin/lights/PointLight.kt
Normal file
6
src/main/kotlin/lights/PointLight.kt
Normal file
|
@ -0,0 +1,6 @@
|
|||
package lights
|
||||
|
||||
import materials.MaterialColor
|
||||
import math.Point
|
||||
|
||||
data class PointLight(val point: Point, val color: MaterialColor) : Light
|
|
@ -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
|
||||
}
|
|
@ -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(),
|
|
@ -1,3 +1,9 @@
|
|||
package materials
|
||||
|
||||
import Hit
|
||||
import math.Ray
|
||||
import things.Scene
|
||||
import math.times
|
||||
import kotlin.math.pow
|
||||
|
||||
data class ReflectiveMaterial(
|
|
@ -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,
|
|
@ -1,3 +1,5 @@
|
|||
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)
|
5
src/main/kotlin/math/Ray.kt
Normal file
5
src/main/kotlin/math/Ray.kt
Normal file
|
@ -0,0 +1,5 @@
|
|||
package math
|
||||
|
||||
data class Ray(val origin: Point, val direction: Vector) {
|
||||
fun at(t: Float) = origin + t * direction
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
package math
|
||||
|
||||
import kotlin.math.sqrt
|
||||
|
||||
data class Vector(val x: Float, val y: Float, val z: Float) {
|
|
@ -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
|
|
@ -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 {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
var closest: Hit? = null
|
|
@ -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
|
||||
|
|
@ -1,3 +1,8 @@
|
|||
package things
|
||||
|
||||
import Hit
|
||||
import math.Ray
|
||||
|
||||
interface Thing {
|
||||
fun intersects(ray: Ray): Hit?
|
||||
}
|
Loading…
Add table
Reference in a new issue