commit c7d89dc55a297b03a4ade5de89cd35a9eb581183 Author: Sven Weidauer Date: Wed Jan 18 22:14:14 2023 +0100 First version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2100949 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +*.tga +*.iml +out/ diff --git a/src/Bitmap.kt b/src/Bitmap.kt new file mode 100644 index 0000000..fd9e2bd --- /dev/null +++ b/src/Bitmap.kt @@ -0,0 +1,54 @@ +import java.io.File +import java.io.OutputStream + +const val BYTES_PER_PIXEL = 4 + +data class Color(val r: UByte, val g: UByte, val b: UByte, val alpha: UByte = UByte.MAX_VALUE) + +class Bitmap(val width: Int, val height: Int) { + val data = UByteArray(width * height * BYTES_PER_PIXEL) + + fun setPixel(x: Int, y: Int, color: Color) { + val offset = (y * width + x) * BYTES_PER_PIXEL + data[offset + 1] = color.g + data[offset + 0] = color.b + data[offset + 2] = color.r + data[offset + 3] = color.alpha + } + + fun getPixel(x: Int, y: Int): Color { + val offset = (y * width + x) * BYTES_PER_PIXEL + return Color( + r = data[offset + 2], + g = data[offset + 1], + b = data[offset + 0], + alpha = data[offset + 3] + ) + } +} + +fun File.writeBitmap(bitmap: Bitmap) { + outputStream().use { stream -> + stream.write(0) // ID length + stream.write(0) // No color map + stream.write(2) // Uncompressed true color image + + stream.writeWord(0) // Color map starts at index 0 + stream.writeWord(0) // No color map entries + stream.write(0) // 0 bits per color map entry + + stream.writeWord(0) // X origin + stream.writeWord(0) // Y origin + stream.writeWord(bitmap.width.toShort()) + stream.writeWord(bitmap.height.toShort()) + stream.write(BYTES_PER_PIXEL * 8) + stream.write(8) + stream.write(bitmap.data.toByteArray()) + } +} + +fun OutputStream.writeWord(word: Short) { + write(word.toInt() and 0xFF) + write((word.toInt() shr 8) and 0xFF) +} + diff --git a/src/Point.kt b/src/Point.kt new file mode 100644 index 0000000..4d60505 --- /dev/null +++ b/src/Point.kt @@ -0,0 +1,6 @@ +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()) +} diff --git a/src/Ray.kt b/src/Ray.kt new file mode 100644 index 0000000..1f7a689 --- /dev/null +++ b/src/Ray.kt @@ -0,0 +1,89 @@ +import java.io.File +import java.lang.Float.max +import java.lang.Math.pow +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.sqrt + +data class Hit(val point: Point, val normal: Vector, val color: MaterialColor) + +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) +} +operator fun Float.times(rhs: MaterialColor) = MaterialColor(this * rhs.r, this * rhs.g, this * rhs.b) + +data class Ray(val origin: Point, val direction: Vector) { + fun at(t: Float) = origin + t * direction +} + +data class Sphere(val center: Point, val radius: Float, val color: MaterialColor) { + 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(), color) + } +} + +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) + +fun main() { + val bmp = Bitmap(1000, 1000) + val origin = Point(500f, 500f, -500f) + + val sphere = Sphere(Point(500f, 500f, 500f), radius = 500f, color = MaterialColor(0f, 1.0f, 0f)) + + val ambient = MaterialColor(0.1f, 0.1f, 0.1f) + + val light = PointLight(Point(1000f, 1000f, -500f), color = MaterialColor(0.5f, 0.5f, 0.5f)) + val specular = 2.4f + + for (y in 0 until bmp.height) { + for (x in 0 until bmp.width) { + val point = Point(x.toFloat(), y.toFloat(), 0f) + val ray = origin.rayTo(point) + + val hit = sphere.intersects(ray) + if (hit != null) { + var color = hit.color * ambient + + val rayToLight = hit.point.rayTo(light.point) + if (sphere.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) + } + + bmp.setPixel(x, y, color.toColor()) + } + } + } + File("test.tga").writeBitmap(bmp) +} diff --git a/src/Vector.kt b/src/Vector.kt new file mode 100644 index 0000000..a3f3a8f --- /dev/null +++ b/src/Vector.kt @@ -0,0 +1,16 @@ +import kotlin.math.sqrt + +data class Vector(val x: Float, val y: Float, val z: Float) { + val length: Float get() = sqrt(squaredLength) + val squaredLength: Float get() = x * x + y * y + z * z + + fun normalized(): Vector = this / length + + operator fun plus(rhs: Vector): Vector = Vector(x + rhs.x, y + rhs.y, z + rhs.z) + operator fun times(rhs: Float): Vector = Vector(x * rhs, y * rhs, z * rhs) + operator fun unaryMinus(): Vector = Vector(-x, -y, -z) + operator fun minus(rhs: Vector): Vector = Vector(x - rhs.x, y - rhs.y, z - rhs.z) + operator fun div(rhs: Float): Vector = Vector(x / rhs, y / rhs, z / rhs) +} + +operator fun Float.times(rhs: Vector): Vector = rhs * this