Move classes to own files.
This commit is contained in:
parent
35ddb2166c
commit
18aefdf41d
11 changed files with 130 additions and 137 deletions
1
src/main/kotlin/Hit.kt
Normal file
1
src/main/kotlin/Hit.kt
Normal file
|
@ -0,0 +1 @@
|
|||
data class Hit(val point: Point, val normal: Vector, val distance: Float, val material: Material)
|
3
src/main/kotlin/Material.kt
Normal file
3
src/main/kotlin/Material.kt
Normal file
|
@ -0,0 +1,3 @@
|
|||
interface Material {
|
||||
fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor
|
||||
}
|
14
src/main/kotlin/MaterialColor.kt
Normal file
14
src/main/kotlin/MaterialColor.kt
Normal file
|
@ -0,0 +1,14 @@
|
|||
data class MaterialColor(val r: Float, val g: Float, val b: Float) {
|
||||
fun toColor(): Color = Color(
|
||||
(r * UByte.MAX_VALUE.toFloat()).toInt().toUByte(),
|
||||
(g * UByte.MAX_VALUE.toFloat()).toInt().toUByte(),
|
||||
(b * UByte.MAX_VALUE.toFloat()).toInt().toUByte()
|
||||
)
|
||||
|
||||
operator fun times(rhs: MaterialColor) = MaterialColor(r * rhs.r, g * rhs.g, b * rhs.b)
|
||||
operator fun plus(rhs: MaterialColor) = MaterialColor(r + rhs.r, g + rhs.g, b + rhs.b)
|
||||
|
||||
companion object {
|
||||
val Black: MaterialColor = MaterialColor(0f, 0f, 0f)
|
||||
}
|
||||
}
|
9
src/main/kotlin/Plane.kt
Normal file
9
src/main/kotlin/Plane.kt
Normal file
|
@ -0,0 +1,9 @@
|
|||
data class Plane(val point: Point, val normal: Vector, val material: Material) : Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
val bottom = dot(normal, ray.direction)
|
||||
if (bottom == 0.0f) return null
|
||||
val t = dot(normal, point - ray.origin) / bottom
|
||||
if (t <= 0.0f) return null
|
||||
return Hit(ray.at(t), normal, t, material)
|
||||
}
|
||||
}
|
1
src/main/kotlin/PointLight.kt
Normal file
1
src/main/kotlin/PointLight.kt
Normal file
|
@ -0,0 +1 @@
|
|||
data class PointLight(val point: Point, val color: MaterialColor)
|
|
@ -1,25 +1,4 @@
|
|||
import java.io.File
|
||||
import java.lang.Float.max
|
||||
import kotlin.math.min
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sqrt
|
||||
|
||||
data class Hit(val point: Point, val normal: Vector, val distance: Float, val material: Material)
|
||||
|
||||
data class MaterialColor(val r: Float, val g: Float, val b: Float) {
|
||||
fun toColor(): Color = Color(
|
||||
(r * UByte.MAX_VALUE.toFloat()).toInt().toUByte(),
|
||||
(g * UByte.MAX_VALUE.toFloat()).toInt().toUByte(),
|
||||
(b * UByte.MAX_VALUE.toFloat()).toInt().toUByte()
|
||||
)
|
||||
|
||||
operator fun times(rhs: MaterialColor) = MaterialColor(r * rhs.r, g * rhs.g, b * rhs.b)
|
||||
operator fun plus(rhs: MaterialColor) = MaterialColor(r + rhs.r, g + rhs.g, b + rhs.b)
|
||||
|
||||
companion object {
|
||||
val Black: MaterialColor = MaterialColor(0f, 0f, 0f)
|
||||
}
|
||||
}
|
||||
|
||||
operator fun Float.times(rhs: MaterialColor) = MaterialColor(this * rhs.r, this * rhs.g, this * rhs.b)
|
||||
|
||||
|
@ -27,125 +6,9 @@ data class Ray(val origin: Point, val direction: Vector) {
|
|||
fun at(t: Float) = origin + t * direction
|
||||
}
|
||||
|
||||
interface Thing {
|
||||
fun intersects(ray: Ray): Hit?
|
||||
}
|
||||
|
||||
interface Material {
|
||||
fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor
|
||||
}
|
||||
|
||||
data class WhateverMaterial(val color: MaterialColor,
|
||||
val ambient: MaterialColor = MaterialColor(0.1f, 0.1f, 0.1f),
|
||||
val specular: Float = 100f
|
||||
): Material {
|
||||
override fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor {
|
||||
var color = this.color * ambient
|
||||
val light = scene.light
|
||||
val rayToLight = (hit.point + 0.0001f * hit.normal).rayTo(light.point)
|
||||
if (scene.intersects(rayToLight) == null) {
|
||||
|
||||
val lambert = max(0f, dot(hit.normal, rayToLight.direction))
|
||||
color += lambert * light.color
|
||||
|
||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||
|
||||
val intensity = dot(hit.normal, h).pow(specular)
|
||||
|
||||
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
||||
}
|
||||
|
||||
return color
|
||||
}
|
||||
}
|
||||
|
||||
data class ReflectiveMaterial(
|
||||
val reflectivity: Float,
|
||||
val color: MaterialColor,
|
||||
val ambient: MaterialColor = MaterialColor(0.1f, 0.1f, 0.1f),
|
||||
val specular: Float = 10f
|
||||
): Material {
|
||||
override fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor {
|
||||
var color = this.color * ambient
|
||||
val light = scene.light
|
||||
val offsetPoint = hit.point + 0.0001f * hit.normal
|
||||
val rayToLight = offsetPoint.rayTo(light.point)
|
||||
if (scene.intersects(rayToLight) == null) {
|
||||
|
||||
val lambert = max(0f, dot(hit.normal, rayToLight.direction))
|
||||
color += lambert * light.color
|
||||
|
||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||
|
||||
val intensity = (hit.normal dot h).pow(specular)
|
||||
|
||||
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
||||
}
|
||||
|
||||
val reflectionDirection = ray.direction - 2 * (ray.direction dot hit.normal) * hit.normal
|
||||
val reflectedColor = scene.colorForRay(Ray(offsetPoint, reflectionDirection)) ?: MaterialColor.Black
|
||||
|
||||
return (1.0f - reflectivity) * color + reflectivity * reflectedColor
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
data class Plane(val point: Point, val normal: Vector, val material: Material) : Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
val bottom = dot(normal, ray.direction)
|
||||
if (bottom == 0.0f) return null
|
||||
val t = dot(normal, point - ray.origin) / bottom
|
||||
if (t <= 0.0f) return null
|
||||
return Hit(ray.at(t), normal, t, material)
|
||||
}
|
||||
}
|
||||
|
||||
data class Sphere(val center: Point, val radius: Float, val material: Material) : Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
val l = center - ray.origin
|
||||
val tc = dot(l, ray.direction)
|
||||
if (tc < 0f) return null
|
||||
|
||||
val d = l.squaredLength - tc * tc
|
||||
val radiusSquared = radius * radius
|
||||
|
||||
if (d > radiusSquared) return null
|
||||
|
||||
val thc = sqrt(radiusSquared - d)
|
||||
|
||||
val t = min(tc - thc, tc + thc)
|
||||
|
||||
val hitPoint = ray.at(t)
|
||||
return Hit(hitPoint, (hitPoint - center).normalized(), t, material)
|
||||
}
|
||||
}
|
||||
|
||||
fun dot(lhs: Vector, rhs: Vector): Float = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z
|
||||
|
||||
data class PointLight(val point: Point, val color: MaterialColor)
|
||||
|
||||
data class Scene(val things: List<Thing>, val light: PointLight): Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
var closest: Hit? = null
|
||||
for (thing in things) {
|
||||
val hit = thing.intersects(ray)
|
||||
if (hit != null && (closest == null || hit.distance < closest.distance)) {
|
||||
closest = hit
|
||||
}
|
||||
}
|
||||
|
||||
return closest
|
||||
}
|
||||
|
||||
fun colorForRay(ray: Ray): MaterialColor? {
|
||||
val hit = intersects(ray)
|
||||
if (hit != null) {
|
||||
return hit.material.shade(ray, hit, this)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val bmp = Bitmap(1000, 1000)
|
||||
val origin = Point(500f, 500f, -500f)
|
||||
|
|
31
src/main/kotlin/ReflectiveMaterial.kt
Normal file
31
src/main/kotlin/ReflectiveMaterial.kt
Normal file
|
@ -0,0 +1,31 @@
|
|||
import kotlin.math.pow
|
||||
|
||||
data class ReflectiveMaterial(
|
||||
val reflectivity: Float,
|
||||
val color: MaterialColor,
|
||||
val ambient: MaterialColor = MaterialColor(0.1f, 0.1f, 0.1f),
|
||||
val specular: Float = 10f
|
||||
): Material {
|
||||
override fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor {
|
||||
var color = this.color * ambient
|
||||
val light = scene.light
|
||||
val offsetPoint = hit.point + 0.0001f * hit.normal
|
||||
val rayToLight = offsetPoint.rayTo(light.point)
|
||||
if (scene.intersects(rayToLight) == null) {
|
||||
|
||||
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
|
||||
color += lambert * light.color
|
||||
|
||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||
|
||||
val intensity = (hit.normal dot h).pow(specular)
|
||||
|
||||
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
||||
}
|
||||
|
||||
val reflectionDirection = ray.direction - 2 * (ray.direction dot hit.normal) * hit.normal
|
||||
val reflectedColor = scene.colorForRay(Ray(offsetPoint, reflectionDirection)) ?: MaterialColor.Black
|
||||
|
||||
return (1.0f - reflectivity) * color + reflectivity * reflectedColor
|
||||
}
|
||||
}
|
21
src/main/kotlin/Scene.kt
Normal file
21
src/main/kotlin/Scene.kt
Normal file
|
@ -0,0 +1,21 @@
|
|||
data class Scene(val things: List<Thing>, val light: PointLight): Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
var closest: Hit? = null
|
||||
for (thing in things) {
|
||||
val hit = thing.intersects(ray)
|
||||
if (hit != null && (closest == null || hit.distance < closest.distance)) {
|
||||
closest = hit
|
||||
}
|
||||
}
|
||||
|
||||
return closest
|
||||
}
|
||||
|
||||
fun colorForRay(ray: Ray): MaterialColor? {
|
||||
val hit = intersects(ray)
|
||||
if (hit != null) {
|
||||
return hit.material.shade(ray, hit, this)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
22
src/main/kotlin/Sphere.kt
Normal file
22
src/main/kotlin/Sphere.kt
Normal file
|
@ -0,0 +1,22 @@
|
|||
import kotlin.math.min
|
||||
import kotlin.math.sqrt
|
||||
|
||||
data class Sphere(val center: Point, val radius: Float, val material: Material) : Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
val l = center - ray.origin
|
||||
val tc = dot(l, ray.direction)
|
||||
if (tc < 0f) return null
|
||||
|
||||
val d = l.squaredLength - tc * tc
|
||||
val radiusSquared = radius * radius
|
||||
|
||||
if (d > radiusSquared) return null
|
||||
|
||||
val thc = sqrt(radiusSquared - d)
|
||||
|
||||
val t = min(tc - thc, tc + thc)
|
||||
|
||||
val hitPoint = ray.at(t)
|
||||
return Hit(hitPoint, (hitPoint - center).normalized(), t, material)
|
||||
}
|
||||
}
|
3
src/main/kotlin/Thing.kt
Normal file
3
src/main/kotlin/Thing.kt
Normal file
|
@ -0,0 +1,3 @@
|
|||
interface Thing {
|
||||
fun intersects(ray: Ray): Hit?
|
||||
}
|
25
src/main/kotlin/WhateverMaterial.kt
Normal file
25
src/main/kotlin/WhateverMaterial.kt
Normal file
|
@ -0,0 +1,25 @@
|
|||
import kotlin.math.pow
|
||||
|
||||
data class WhateverMaterial(val color: MaterialColor,
|
||||
val ambient: MaterialColor = MaterialColor(0.1f, 0.1f, 0.1f),
|
||||
val specular: Float = 100f
|
||||
): Material {
|
||||
override fun shade(ray: Ray, hit: Hit, scene: Scene): MaterialColor {
|
||||
var color = this.color * ambient
|
||||
val light = scene.light
|
||||
val rayToLight = (hit.point + 0.0001f * hit.normal).rayTo(light.point)
|
||||
if (scene.intersects(rayToLight) == null) {
|
||||
|
||||
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
|
||||
color += lambert * light.color
|
||||
|
||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||
|
||||
val intensity = dot(hit.normal, h).pow(specular)
|
||||
|
||||
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
||||
}
|
||||
|
||||
return color
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue