Add plane at bottom
This commit is contained in:
parent
30e5534447
commit
258530bef1
1 changed files with 43 additions and 7 deletions
50
src/Ray.kt
50
src/Ray.kt
|
@ -4,7 +4,7 @@ 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 Hit(val point: Point, val normal: Vector, val distance: Float, val color: MaterialColor)
|
||||
|
||||
data class MaterialColor(val r: Float, val g: Float, val b: Float) {
|
||||
fun toColor(): Color = Color(
|
||||
|
@ -16,14 +16,30 @@ data class MaterialColor(val r: Float, val g: Float, val b: Float) {
|
|||
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? {
|
||||
interface Thing {
|
||||
fun intersects(ray: Ray): Hit?
|
||||
}
|
||||
|
||||
|
||||
data class Plane(val point: Point, val normal: Vector, val color: MaterialColor) : 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, color)
|
||||
}
|
||||
}
|
||||
|
||||
data class Sphere(val center: Point, val radius: Float, val color: MaterialColor) : Thing {
|
||||
override fun intersects(ray: Ray): Hit? {
|
||||
val l = center - ray.origin
|
||||
val tc = dot(l, ray.direction)
|
||||
if (tc < 0f) return null
|
||||
|
@ -37,9 +53,8 @@ data class Sphere(val center: Point, val radius: Float, val color: MaterialColor
|
|||
|
||||
val t = min(tc - thc, tc + thc)
|
||||
|
||||
|
||||
val hitPoint = ray.at(t)
|
||||
return Hit(hitPoint, (hitPoint - center).normalized(), color)
|
||||
return Hit(hitPoint, (hitPoint - center).normalized(), t, color)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,11 +62,32 @@ fun dot(lhs: Vector, rhs: Vector): Float = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z
|
|||
|
||||
data class PointLight(val point: Point, val color: MaterialColor)
|
||||
|
||||
data class Scene(val things: List<Thing>): 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 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 plane = Plane(
|
||||
Point(0f, -500f, 0f),
|
||||
normal = Vector(0f, 1f, 0f),
|
||||
color = MaterialColor(0.5f, 0.5f, 0.5f)
|
||||
)
|
||||
|
||||
val scene = Scene(listOf(sphere, plane))
|
||||
|
||||
val ambient = MaterialColor(0.1f, 0.1f, 0.1f)
|
||||
|
||||
|
@ -63,12 +99,12 @@ fun main() {
|
|||
val point = Point(x.toFloat(), y.toFloat(), 0f)
|
||||
val ray = origin.rayTo(point)
|
||||
|
||||
val hit = sphere.intersects(ray)
|
||||
val hit = scene.intersects(ray)
|
||||
if (hit != null) {
|
||||
var color = hit.color * ambient
|
||||
|
||||
val rayToLight = hit.point.rayTo(light.point)
|
||||
if (sphere.intersects(rayToLight) == null) {
|
||||
if (scene.intersects(rayToLight) == null) {
|
||||
|
||||
val lambert = max(0f, dot(hit.normal, rayToLight.direction))
|
||||
color += lambert * light.color
|
||||
|
|
Loading…
Add table
Reference in a new issue