From 36c2794a4877ee960cca7c8ad520119a7b682efb Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Wed, 18 Mar 2020 16:49:54 +0100 Subject: [PATCH] OrbitalCamera: implement .isolated() and .applyTo() --- orx-camera/src/main/kotlin/OrbitalCamera.kt | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/orx-camera/src/main/kotlin/OrbitalCamera.kt b/orx-camera/src/main/kotlin/OrbitalCamera.kt index c1c56a28..93556db5 100644 --- a/orx-camera/src/main/kotlin/OrbitalCamera.kt +++ b/orx-camera/src/main/kotlin/OrbitalCamera.kt @@ -165,4 +165,31 @@ class OrbitalCamera(eye: Vector3 = Vector3.ZERO, lookAt: Vector3 = Vector3.UNIT_ } } +/** + * Temporarily enables this camera, calls function to draw using + * that camera, then disables it by popping the last matrix changes. + * It makes it easy to combine perspective and orthographic projections + * in the same program. + * @param function the function that is called in the isolation + */ +fun OrbitalCamera.isolated(drawer: Drawer, function: Drawer.() -> Unit) { + drawer.pushTransforms() + drawer.pushStyle() + applyTo(drawer) + function(drawer) + + drawer.popStyle() + drawer.popTransforms() +} + +/** + * Enables the perspective camera. Use this faster method instead of .isolated() + * if you don't need to revert back to the orthographic projection. + */ +fun OrbitalCamera.applyTo(drawer: Drawer) { + drawer.perspective(fov, drawer.width.toDouble() / drawer.height, near, far) + drawer.view = viewMatrix() + drawer.drawStyle.depthWrite = true + drawer.drawStyle.depthTestPass = DepthTestPass.LESS_OR_EQUAL +} \ No newline at end of file