summaryrefslogtreecommitdiff
path: root/src/Scene.scala
blob: 9b59f7b520dc416b189cd99d7c7072f832bc8b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package net.iximeow.raytrace

import Objects._

import java.awt.image.BufferedImage
import javax.imageio._
import java.io.File

case class Scene(walls: Seq[Segment]) {
  val buffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB)

  def render(scale: Double = 1, xoff: Int = 0, yoff: Int = 0, color: Int = 0x808000, normals: Boolean = false): Unit = {
    for (wall <- walls) {
      wall.renderTo(buffer, scale, 400, 300, color = color)
    }
    for (wall <- walls) {
      wall.normal.renderTo(buffer, scale, 400, 300, color = 0xc00000)
    }
  }

  def save(path: String = "render.png"): Unit = {
    ImageIO.write(buffer, "png", new File(path))
  }

  def cast(r: Ray, steps: Int): Seq[Segment] = {
    (0 until steps).foldLeft(Seq.empty[Segment] -> r) { case (p: (Seq[Segment], Ray), i: Int) => {
      val (prevRay, nextRay)  = castSingle(p._2)
      ((p._1 :+ prevRay.toSegment) -> nextRay): (Seq[Segment], Ray)
    }}._1
  }

  def castSingle(r: Ray): (Ray, Ray) = {
    val asSeg = r.toSegment

    def reflect(firstIntersection: (Segment, Point)): (Ray, Ray) = {
      val minAngle = {
        val fromStart = Raymath.angleBetween(
          r.initial,
          firstIntersection._2,
          firstIntersection._1.at(0)
        )
        val fromEnd = Raymath.angleBetween(
          r.initial,
          firstIntersection._2,
          firstIntersection._1.at(1)
        )

        println("Fromstart: " + Raymath.toDegrees(fromStart))
        println("Fromend:  " + Raymath.toDegrees(fromEnd))

        if (Math.abs(fromStart) < Math.PI / 2) {
          fromStart
        } else {
          fromEnd
        }

        fromStart
      }

      val maxAngle = Math.PI - minAngle

      val baseAngle = Math.atan2(firstIntersection._1.y, firstIntersection._1.x)
      println("base angle: " + Raymath.toDegrees(baseAngle))

      val reflectedAngle = baseAngle + minAngle

      if (minAngle < 0 || minAngle > Math.PI * 2) {
        println("lol")
        (r.endingAt(firstIntersection._2), r.endingAt(firstIntersection._2)) //Ray(0, 0, firstIntersection._2))
      } else {
        val (x, y) = (
          Math.cos(reflectedAngle) * 3,
          Math.sin(reflectedAngle) * 3
        )

        // Sure hope this is right...
        (r.endingAt(firstIntersection._2), Ray(x, y, firstIntersection._2))
      }
    }
    val intersections: Seq[(Segment, Point)] = walls.flatMap(w => {
      w.intersectChecked(asSeg)
        .map(x => (w, x))
    })
      .filter { case (w: Segment, x: Point) => asSeg.tFor(x).map(_ > 0.0000001).getOrElse(false) }

    def isBehind(start: Segment, wall: Segment): Boolean = {
      val normal = Ray(-wall.y, wall.x, Point(0, 0))
      val rebased = Ray(start.x, start.y, Point(0, 0))
      val cosAngle = normal.dot(rebased) / (normal.mag * rebased.mag)
      cosAngle > 0
    }

    val continuedIntersections = intersections
      .filter(i => {
        val otherT = i._1.tFor(i._2)
        otherT.map(t => t >= 0 && t <= 1).getOrElse(true)
      })

    val stoppedIntersections = intersections
      .filter(i => {
        val otherT = i._1.tFor(i._2)
        otherT.map(t => t >= 0 && t <= 1 && isBehind(asSeg, i._1)).getOrElse(false)
      })

    def fnMin(x: (Segment, Point), y: (Segment, Point)) = if (asSeg.tFor(x._2).get < asSeg.tFor(y._2).get) x else y
    val firstStop: Option[(Segment, Point)] = stoppedIntersections.reduceOption(fnMin(_, _))
    val firstReflect: Option[(Segment, Point)] = continuedIntersections.reduceOption(fnMin(_, _))

    (firstStop, firstReflect) match {
      case (None, None) =>
        (r, Ray(r.x, r.y, r.toSegment.at(1)))
      case (Some(stop), None) =>
        (r.endingAt(stop._2), Ray(0, 0, r.initial))
      case (None, Some(cont)) => reflect(cont)/* reflect */
      case (Some(stop), Some(cont)) => {
        if (fnMin(stop, cont) == stop) {
          (r.endingAt(stop._2), Ray(0, 0, r.initial))
          // stop
        } else {
          reflect(cont)
          // reflect
        }
      }
    }
  }
}

object Scene {
  def generateMirror(r: Double, segments: Int, arcSize: Double, at: Point, rotated: Double): Seq[Segment] = {
    val sizePerSegment = arcSize / segments
    val points = (0 to segments) map { i =>
      val angle = i * sizePerSegment + rotated
      at + Point(Math.cos(angle) * r, Math.sin(angle) * r)
    }
    points.sliding(2).map { case Seq(start, end) =>
      Segment.fromPoints(start, end)
    }.toSeq
  }

  def generateParabola(a: Double, b: Double, w: Double, w_i: Double, segments: Int, at: Point, rotated: Double): Seq[Segment] = {
    val points = (-segments / 2 to segments / 2) map { i =>
      val w_curr = (i / segments.toDouble) * w + w_i
      at + Point(a * w_curr, b * w_curr * w_curr)
    }
    points.sliding(2).map { case Seq(start, end) =>
      Segment.fromPoints(start, end)
    }.toSeq
  }

  def rotate(walls: Seq[Segment], angle: Double) =
    walls.map(_.rotate(angle))
}