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
|
package phenax.extensions
import kotlin.math.absoluteValue
import org.openrndr.*
import org.openrndr.color.*
import org.openrndr.draw.*
import org.openrndr.math.Quaternion
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import phenax.utils.*
enum class ActionType {
PAN,
ROTATE
}
class CameraManager(public var cameraPosition: Vector3, public var cameraRotation: Vector3) :
Extension {
override var enabled: Boolean = true
public var cameraQuaternion: Quaternion = Quaternion.IDENTITY
public var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0)
var font: FontImageMap? = null
var actionType: ActionType? = null
var lastPos: Vector3? = null
constructor() : this(Vector3(0.0, 1.0, 0.0), Vector3.ZERO) {}
constructor(
cameraPosition: Vector3,
cameraRotation: Vector3,
lightPos: Vector3
) : this(cameraPosition, cameraRotation) {
lightPosition = lightPos
}
override fun setup(program: Program) {
if (recordingEnabled || screenshotEnabled) return
cameraQuaternion = Quaternion.fromAngles(cameraRotation.x, cameraRotation.y, cameraRotation.z)
font = program.loadFont("data/fonts/default.otf", 16.0)
program.keyboard.keyDown.listen {
when (it.name) {
"escape" -> program.application.exit()
"x" -> cameraQuaternion = Quaternion.IDENTITY
"y" -> cameraQuaternion = Quaternion.fromAngles(0.0, -90.0, 0.0)
"z" -> cameraQuaternion = Quaternion.fromAngles(90.0, 0.0, 0.0)
}
}
program.mouse.moved.listen {
val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0)
var pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens)
pos = cameraQuaternion.inversed.matrix * pos
val increment = lastPos?.let { pos - it }
if (actionType == ActionType.PAN) {
increment?.let { cameraPosition -= it }
} else if (actionType == ActionType.ROTATE) {
increment?.let {
val mag = 10.0
val rot =
if (it.x.absoluteValue >= it.y.absoluteValue) Vector2(it.x * mag, 0.0)
else Vector2(0.0, it.y * mag)
cameraQuaternion *= Quaternion.fromAngles(rot.x, rot.y, 0.0)
}
// lastPos?.let { lastPos -> lightPosition += (pos - lastPos) }
}
actionType?.let { lastPos = pos }
}
program.mouse.buttonDown.listen {
val isCtrl = KeyModifier.CTRL in it.modifiers
actionType = if (isCtrl) ActionType.ROTATE else ActionType.PAN
}
program.mouse.scrolled.listen {
val isCtrl = KeyModifier.CTRL in it.modifiers
if (isCtrl) {
cameraQuaternion *= Quaternion.fromAngles(it.rotation.y, 0.0, 0.0)
} else {
cameraPosition += cameraQuaternion.inversed.matrix * Vector3(0.0, 0.0, it.rotation.y * 0.1)
}
}
program.mouse.buttonUp.listen {
actionType = null
lastPos = null
}
}
fun setUniforms(cs: ComputeShader) {
cs.uniform("camera_position", cameraPosition)
cs.uniform("camera_transformation", cameraQuaternion.matrix)
cs.uniform("key_light_position", lightPosition)
}
override fun beforeDraw(drawer: Drawer, program: Program) {}
override fun afterDraw(drawer: Drawer, program: Program) {
if (recordingEnabled || screenshotEnabled) return
drawer.fontMap = font
drawer.fill = ColorRGBa.WHITE
drawer.text(
"C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z),
8.0,
16.0
)
drawer.text(
"L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z),
8.0,
32.0
)
drawer.text(
"L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z),
8.0,
32.0
)
}
}
|