From 7ba79fe1d18a91e5b28a5e055c7e92d4e21f4a9b Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 13 May 2022 12:15:33 +0200 Subject: [PATCH] wip camera debug view #80 --- .../minosoft/gui/rendering/camera/Camera.kt | 28 +++++++++++++++++- .../gui/rendering/camera/MatrixHandler.kt | 26 ++++++++++++----- .../rendering/entity/EntityHitboxRenderer.kt | 2 +- .../gui/rendering/input/CameraInput.kt | 29 +++++++++++++++---- .../input/key/RenderWindowInputHandler.kt | 2 +- .../java/de/bixilon/minosoft/util/KUtil.kt | 7 ++--- 6 files changed, 75 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/Camera.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/Camera.kt index d424193d9..a8858ccc1 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/Camera.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/Camera.kt @@ -13,22 +13,48 @@ package de.bixilon.minosoft.gui.rendering.camera +import de.bixilon.minosoft.config.key.KeyActions +import de.bixilon.minosoft.config.key.KeyBinding +import de.bixilon.minosoft.config.key.KeyCodes import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.camera.target.TargetHandler import de.bixilon.minosoft.gui.rendering.world.view.WorldVisibilityGraph +import de.bixilon.minosoft.util.KUtil.format +import de.bixilon.minosoft.util.KUtil.toResourceLocation class Camera( - renderWindow: RenderWindow, + private val renderWindow: RenderWindow, ) { val fogManager = FogManager(renderWindow) val matrixHandler = MatrixHandler(renderWindow, fogManager, this) val targetHandler = TargetHandler(renderWindow, this) val visibilityGraph = WorldVisibilityGraph(renderWindow, this) + @Deprecated("ToDo: Not yet implemented!") val firstPerson: Boolean = true // ToDo + var debugView: Boolean = false + set(value) { + field = value + if (value) { + matrixHandler.debugRotation = matrixHandler.entity.rotation + matrixHandler.debugPosition = matrixHandler.entity.cameraPosition + } + } fun init() { matrixHandler.init() + + + renderWindow.inputHandler.registerKeyCallback("minosoft:camera_debug_view".toResourceLocation(), + KeyBinding( + mapOf( + KeyActions.MODIFIER to setOf(KeyCodes.KEY_F4), + KeyActions.STICKY to setOf(KeyCodes.KEY_V), + ), + )) { + debugView = it + renderWindow.connection.util.sendDebugMessage("Camera debug view: ${it.format()}") + } } fun draw() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt index ebd61a824..4f2e507f6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/MatrixHandler.kt @@ -76,6 +76,11 @@ class MatrixHandler( var viewProjectionMatrix = projectionMatrix * viewMatrix private set + private var previousDebugPosition = Vec3.EMPTY + private var previousDebugRotation = EntityRotation(0.0, 0.0) + var debugPosition = Vec3.EMPTY + var debugRotation = EntityRotation(0.0, 0.0) + private val fov: Double get() { @@ -105,13 +110,18 @@ class MatrixHandler( } fun draw() { + val debugView = camera.debugView val fov = fov val eyePosition = entity.eyePosition val rotation = entity.rotation val fogEnd = fogManager.fogEnd - if (upToDate && eyePosition == this.eyePosition && rotation == this.rotation && fov == previousFOV) { + val debugPosition = debugPosition + val debugRotation = debugRotation + if ((upToDate && eyePosition == this.eyePosition && rotation == this.rotation && fov == previousFOV) && (!debugView || (previousDebugPosition == debugPosition && previousDebugRotation == debugRotation))) { return } + this.previousDebugPosition = debugPosition + this.previousDebugRotation = debugRotation this.eyePosition = eyePosition this.rotation = rotation val cameraBlockPosition = eyePosition.blockPosition @@ -122,10 +132,12 @@ class MatrixHandler( } previousFOV = fov - updateRotation(rotation) - updateViewMatrix(eyePosition) + updateRotation(if (debugView) debugRotation else rotation) + updateViewMatrix(if (debugView) debugPosition else eyePosition) updateViewProjectionMatrix() - frustum.recalculate() + if (!debugView) { + frustum.recalculate() + } connection.fireEvent(CameraPositionChangeEvent(renderWindow, eyePosition)) @@ -136,7 +148,7 @@ class MatrixHandler( viewProjectionMatrix = viewProjectionMatrix, )) - updateShaders() + updateShaders(if (debugView) debugPosition else eyePosition) upToDate = true } @@ -155,13 +167,13 @@ class MatrixHandler( cameraUp = (cameraRight cross cameraFront).normalize() } - private fun updateShaders() { + private fun updateShaders(cameraPosition: Vec3) { for (shader in renderWindow.renderSystem.shaders) { if ("uViewProjectionMatrix" in shader.uniforms) { shader.use().setMat4("uViewProjectionMatrix", viewProjectionMatrix) } if ("uCameraPosition" in shader.uniforms) { - shader.use().setVec3("uCameraPosition", connection.player.cameraPosition) + shader.use().setVec3("uCameraPosition", cameraPosition) } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/entity/EntityHitboxRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/entity/EntityHitboxRenderer.kt index fe9ce0dfe..982ab14c8 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/entity/EntityHitboxRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/entity/EntityHitboxRenderer.kt @@ -109,7 +109,7 @@ class EntityHitboxRenderer( if (setAvailable) { connection.world.entities.lock.acquire() for (entity in connection.world.entities) { - if (entity is LocalPlayerEntity && !profile.showLocal) { + if (entity is LocalPlayerEntity && (renderWindow.camera.firstPerson && !profile.showLocal) && !renderWindow.camera.debugView) { continue } meshes[entity] = EntityHitbox(this, entity, visibilityGraph) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/CameraInput.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/CameraInput.kt index 389131f12..2913df646 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/CameraInput.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/CameraInput.kt @@ -38,6 +38,9 @@ class CameraInput( if (entity != player) { return true } + if (renderWindow.camera.debugView) { + return true + } return false } @@ -108,7 +111,16 @@ class CameraInput( registerKeyBindings() } - fun update() { + fun update(delta: Double) { + if (renderWindow.camera.debugView) { + val cameraFront = renderWindow.camera.matrixHandler.debugRotation.front + val speedMultiplier = if (renderWindow.inputHandler.isKeyBindingDown(SNEAK_KEYBINDING)) 25 else 10 + if (renderWindow.inputHandler.isKeyBindingDown(MOVE_FORWARDS_KEYBINDING)) { + renderWindow.camera.matrixHandler.debugPosition = renderWindow.camera.matrixHandler.debugPosition + (cameraFront * delta * speedMultiplier) + } + connection.player.input = MovementInput() + return + } val input = if (ignoreInput) { MovementInput() } else { @@ -129,17 +141,24 @@ class CameraInput( } fun mouseCallback(delta: Vec2d) { + if (renderWindow.camera.debugView) { + matrixHandler.debugRotation = mouseCallback(delta, matrixHandler.debugRotation) + } else if (!ignoreInput) { + player.rotation = mouseCallback(delta, player.rotation) + } + } + + private fun mouseCallback(delta: Vec2d, rotation: EntityRotation): EntityRotation { delta *= 0.1f * controlsProfile.mouse.sensitivity - var yaw = delta.x + player.rotation.yaw + var yaw = delta.x + rotation.yaw if (yaw > 180) { yaw -= 360 } else if (yaw < -180) { yaw += 360 } yaw %= 180 - val pitch = GLM.clamp(delta.y + player.rotation.pitch, -89.9, 89.9) - val rotation = EntityRotation(yaw, pitch) - player.rotation = rotation + val pitch = GLM.clamp(delta.y + rotation.pitch, -89.9, 89.9) + return EntityRotation(yaw, pitch) } private companion object { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/RenderWindowInputHandler.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/RenderWindowInputHandler.kt index dd34dea63..56ff83a3f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/RenderWindowInputHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/input/key/RenderWindowInputHandler.kt @@ -348,7 +348,7 @@ class RenderWindowInputHandler( } fun draw(delta: Double) { - cameraInput.update() + cameraInput.update(delta) interactionManager.draw(delta) } } diff --git a/src/main/java/de/bixilon/minosoft/util/KUtil.kt b/src/main/java/de/bixilon/minosoft/util/KUtil.kt index 730a3c4e8..790f1e774 100644 --- a/src/main/java/de/bixilon/minosoft/util/KUtil.kt +++ b/src/main/java/de/bixilon/minosoft/util/KUtil.kt @@ -188,10 +188,9 @@ object KUtil { if (this == null || other == null) { return null } - this.compareTo(other).let { - if (it != 0) { - return it - } + val compared = this.compareTo(other) + if (compared != 0) { + return compared } return null }