Use infix dot product function.
This commit is contained in:
parent
18aefdf41d
commit
20e50ad7b9
5 changed files with 6 additions and 8 deletions
|
@ -1,8 +1,8 @@
|
||||||
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 = dot(normal, ray.direction)
|
val bottom = normal dot ray.direction
|
||||||
if (bottom == 0.0f) return null
|
if (bottom == 0.0f) return null
|
||||||
val t = dot(normal, point - ray.origin) / bottom
|
val t = (normal dot point - ray.origin) / bottom
|
||||||
if (t <= 0.0f) return null
|
if (t <= 0.0f) return null
|
||||||
return Hit(ray.at(t), normal, t, material)
|
return Hit(ray.at(t), normal, t, material)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,6 @@ data class Ray(val origin: Point, val direction: Vector) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun dot(lhs: Vector, rhs: Vector): Float = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -13,7 +13,7 @@ data class ReflectiveMaterial(
|
||||||
val rayToLight = offsetPoint.rayTo(light.point)
|
val rayToLight = offsetPoint.rayTo(light.point)
|
||||||
if (scene.intersects(rayToLight) == null) {
|
if (scene.intersects(rayToLight) == null) {
|
||||||
|
|
||||||
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
|
val lambert = java.lang.Float.max(0f, hit.normal dot rayToLight.direction)
|
||||||
color += lambert * light.color
|
color += lambert * light.color
|
||||||
|
|
||||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||||
|
|
|
@ -4,7 +4,7 @@ import kotlin.math.sqrt
|
||||||
data class Sphere(val center: Point, val radius: Float, val material: Material) : Thing {
|
data class Sphere(val center: Point, val radius: Float, val material: Material) : Thing {
|
||||||
override fun intersects(ray: Ray): Hit? {
|
override fun intersects(ray: Ray): Hit? {
|
||||||
val l = center - ray.origin
|
val l = center - ray.origin
|
||||||
val tc = dot(l, ray.direction)
|
val tc = l dot ray.direction
|
||||||
if (tc < 0f) return null
|
if (tc < 0f) return null
|
||||||
|
|
||||||
val d = l.squaredLength - tc * tc
|
val d = l.squaredLength - tc * tc
|
||||||
|
|
|
@ -10,12 +10,12 @@ data class WhateverMaterial(val color: MaterialColor,
|
||||||
val rayToLight = (hit.point + 0.0001f * hit.normal).rayTo(light.point)
|
val rayToLight = (hit.point + 0.0001f * hit.normal).rayTo(light.point)
|
||||||
if (scene.intersects(rayToLight) == null) {
|
if (scene.intersects(rayToLight) == null) {
|
||||||
|
|
||||||
val lambert = java.lang.Float.max(0f, dot(hit.normal, rayToLight.direction))
|
val lambert = java.lang.Float.max(0f, hit.normal dot rayToLight.direction)
|
||||||
color += lambert * light.color
|
color += lambert * light.color
|
||||||
|
|
||||||
val h = (-ray.direction + rayToLight.direction).normalized()
|
val h = (-ray.direction + rayToLight.direction).normalized()
|
||||||
|
|
||||||
val intensity = dot(hit.normal, h).pow(specular)
|
val intensity = (hit.normal dot h).pow(specular)
|
||||||
|
|
||||||
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
color += intensity * MaterialColor(0.3f, 0f, 0f)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue