From a67d925bc972451675f0ed32bfb1393ff1f500b5 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 28 Nov 2022 21:18:38 +0100 Subject: [PATCH] abstract camera view --- .../minosoft/gui/rendering/camera/Camera.kt | 38 ++------- .../gui/rendering/camera/CameraDefinition.kt | 23 ++++++ .../gui/rendering/camera/MatrixHandler.kt | 79 ++++++------------ .../gui/rendering/camera/view/CameraView.kt | 45 ++++++++++ .../gui/rendering/camera/view/DebugView.kt | 77 +++++++++++++++++ .../rendering/camera/view/FirstPersonView.kt | 67 +++++++++++++++ .../gui/rendering/camera/view/ViewManager.kt | 82 +++++++++++++++++++ .../minecraft/player/LocalPlayerModel.kt | 2 +- .../overlays/simple/FirstPersonOverlay.kt | 2 +- .../gui/rendering/input/CameraInput.kt | 75 +++++------------ .../input/key/RenderWindowInputHandler.kt | 4 +- .../gui/rendering/sky/clouds/CloudRenderer.kt | 2 +- .../gui/rendering/sound/AudioPlayer.kt | 4 +- .../world/border/WorldBorderRenderer.kt | 2 +- .../world/chunk/ChunkBorderRenderer.kt | 2 +- 15 files changed, 357 insertions(+), 147 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/camera/CameraDefinition.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/CameraView.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/DebugView.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/FirstPersonView.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt 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 f882d1634..4c3d6c174 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 @@ -17,60 +17,36 @@ import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.kutil.concurrent.pool.ThreadPool import de.bixilon.kutil.concurrent.pool.ThreadPoolRunnable import de.bixilon.kutil.latch.CountUpAndDownLatch -import de.bixilon.kutil.time.TimeUtil -import de.bixilon.minosoft.config.key.KeyActions -import de.bixilon.minosoft.config.key.KeyBinding -import de.bixilon.minosoft.config.key.KeyCodes +import de.bixilon.kutil.time.TimeUtil.millis import de.bixilon.minosoft.data.entities.entities.player.local.LocalPlayerEntity import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.camera.target.TargetHandler +import de.bixilon.minosoft.gui.rendering.camera.view.ViewManager 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( - private val renderWindow: RenderWindow, + 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.eyePosition - } - } + val view = ViewManager(this) - val renderPlayer: Boolean - get() = !debugView || !firstPerson fun init() { matrixHandler.init() - - renderWindow.inputHandler.registerKeyCallback( - "minosoft:camera_debug_view".toResourceLocation(), - KeyBinding( - 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()}") - } + view.init() } fun draw() { val entity = matrixHandler.entity entity.tryTick() if (entity is LocalPlayerEntity) { - entity._draw(TimeUtil.millis) + entity._draw(millis()) } + view.draw() matrixHandler.draw() val latch = CountUpAndDownLatch(2) DefaultThreadPool += ThreadPoolRunnable(ThreadPool.Priorities.HIGHER) { visibilityGraph.draw();latch.dec() } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/CameraDefinition.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/CameraDefinition.kt new file mode 100644 index 000000000..6d39f190c --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/CameraDefinition.kt @@ -0,0 +1,23 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.camera + +import de.bixilon.kotlinglm.vec3.Vec3 + +object CameraDefinition { + const val NEAR_PLANE = 0.01f + const val FAR_PLANE = 10000.0f + val CAMERA_UP_VEC3 = Vec3(0.0, 1.0, 0.0) + +} 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 08612bd19..0a249707a 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 @@ -18,9 +18,11 @@ import de.bixilon.kotlinglm.func.rad import de.bixilon.kotlinglm.mat4x4.Mat4 import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec3.Vec3 -import de.bixilon.minosoft.data.entities.EntityRotation import de.bixilon.minosoft.data.entities.entities.Entity import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.CAMERA_UP_VEC3 +import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.FAR_PLANE +import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.NEAR_PLANE import de.bixilon.minosoft.gui.rendering.camera.frustum.Frustum import de.bixilon.minosoft.gui.rendering.events.CameraMatrixChangeEvent import de.bixilon.minosoft.gui.rendering.events.CameraPositionChangeEvent @@ -41,26 +43,20 @@ class MatrixHandler( private val connection = renderWindow.connection private val profile = renderWindow.connection.profiles.rendering.camera val frustum = Frustum(this, connection.world) + + @Deprecated("outsource to connection") var entity: Entity = renderWindow.connection.player set(value) { field = value upToDate = false } - - var eyePosition = Vec3.EMPTY - private set - var rotation = EntityRotation(0.0, 0.0) - private set + private var eyePosition = Vec3.EMPTY private var previousFOV = 0.0 - var cameraFront = Vec3(0.0, 0.0, -1.0) - private set - var cameraRight = Vec3(0.0, 0.0, -1.0) - private set - var cameraUp = Vec3(0.0, 1.0, 0.0) - private set - + private var front = Vec3.EMPTY + private var right = Vec3(0.0, 0.0, -1.0) + private var up = Vec3(0.0, 1.0, 0.0) var zoom = 0.0f set(value) { @@ -77,13 +73,6 @@ class MatrixHandler( var viewProjectionMatrix = projectionMatrix * viewMatrix private set - private var previousDebugView = false - 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() { val fov = profile.fov / (zoom + 1.0) @@ -95,8 +84,8 @@ class MatrixHandler( } - private fun calculateViewMatrix(eyePosition: Vec3 = entity.eyePosition) { - viewMatrix = GLM.lookAt(eyePosition, eyePosition + cameraFront, CAMERA_UP_VEC3) + private fun updateViewMatrix(position: Vec3, front: Vec3) { + viewMatrix = GLM.lookAt(position, position + front, CAMERA_UP_VEC3) } private fun calculateProjectionMatrix(screenDimensions: Vec2 = renderWindow.window.sizef) { @@ -112,36 +101,33 @@ class MatrixHandler( } fun draw() { - val debugView = camera.debugView val fov = fov - val eyePosition = entity.eyePosition - val rotation = entity.rotation - val debugPosition = debugPosition - val debugRotation = debugRotation - if ((upToDate && eyePosition == this.eyePosition && rotation == this.rotation && fov == previousFOV) && previousDebugView == debugView && (!debugView || (previousDebugPosition == debugPosition && previousDebugRotation == debugRotation))) { + val view = camera.view.view + val eyePosition = view.eyePosition + val front = view.front + if (upToDate && eyePosition == this.eyePosition && front == this.front && fov == previousFOV) { return } - this.previousDebugView = debugView - this.previousDebugPosition = debugPosition - this.previousDebugRotation = debugRotation this.eyePosition = eyePosition - this.rotation = rotation + this.front = front val cameraBlockPosition = eyePosition.blockPosition if (fov != previousFOV) { calculateProjectionMatrix() } previousFOV = fov - updateRotation(if (debugView) debugRotation else rotation) - updateViewMatrix(if (debugView) debugPosition else eyePosition) + updateFront(front) + updateViewMatrix(eyePosition, front) updateViewProjectionMatrix() - if (!debugView) { + val usePosition = if (view.updateFrustum) eyePosition else entity.eyePosition + + if (view.updateFrustum) { frustum.recalculate() camera.visibilityGraph.updateCamera(cameraBlockPosition.chunkPosition, cameraBlockPosition.sectionHeight) } - connection.fire(CameraPositionChangeEvent(renderWindow, eyePosition)) + connection.fire(CameraPositionChangeEvent(renderWindow, usePosition)) connection.fire( CameraMatrixChangeEvent( @@ -152,23 +138,18 @@ class MatrixHandler( ) ) - updateShaders(if (debugView) debugPosition else eyePosition) + updateShaders(usePosition) upToDate = true } - private fun updateViewMatrix(eyePosition: Vec3) { - calculateViewMatrix(eyePosition) - } - private fun updateViewProjectionMatrix() { viewProjectionMatrix = projectionMatrix * viewMatrix } - private fun updateRotation(rotation: EntityRotation = entity.rotation) { - cameraFront = rotation.front - - cameraRight = (cameraFront cross CAMERA_UP_VEC3).normalize() - cameraUp = (cameraRight cross cameraFront).normalize() + private fun updateFront(front: Vec3) { + this.front = front + this.right = (front cross CAMERA_UP_VEC3).normalize() + this.up = (this.right cross front).normalize() } private fun updateShaders(cameraPosition: Vec3) { @@ -181,10 +162,4 @@ class MatrixHandler( } } } - - companion object { - const val NEAR_PLANE = 0.01f - const val FAR_PLANE = 10000.0f - val CAMERA_UP_VEC3 = Vec3(0.0, 1.0, 0.0) - } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/CameraView.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/CameraView.kt new file mode 100644 index 000000000..f0249f8d7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/CameraView.kt @@ -0,0 +1,45 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.camera.view + +import de.bixilon.kotlinglm.vec2.Vec2d +import de.bixilon.kotlinglm.vec3.Vec3 +import de.bixilon.minosoft.data.entities.EntityRotation +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.input.camera.MovementInput + +interface CameraView { + val renderWindow: RenderWindow + + val renderSelf: Boolean get() = true + val renderOverlays: Boolean get() = false + + val updateFrustum: Boolean get() = true + + val eyePosition: Vec3 + + val rotation: EntityRotation + val front: Vec3 + + + fun onInput(input: MovementInput, delta: Double) = Unit + fun onMouse(delta: Vec2d) = Unit + fun onScroll(scroll: Double) = Unit + + fun onAttach(previous: CameraView?) = Unit + fun onDetach(next: CameraView) = Unit + + + fun draw() = Unit +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/DebugView.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/DebugView.kt new file mode 100644 index 000000000..270b2d343 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/DebugView.kt @@ -0,0 +1,77 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.camera.view + +import de.bixilon.kotlinglm.vec2.Vec2d +import de.bixilon.kotlinglm.vec3.Vec3 +import de.bixilon.minosoft.data.entities.EntityRotation +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.camera.Camera +import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.CAMERA_UP_VEC3 +import de.bixilon.minosoft.gui.rendering.input.camera.MovementInput +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY + +class DebugView(private val camera: Camera) : CameraView { + override val renderWindow: RenderWindow get() = camera.renderWindow + override val updateFrustum: Boolean get() = false + + override var eyePosition = Vec3.EMPTY + + override var rotation = EntityRotation.EMPTY + override var front = Vec3.EMPTY + + + override fun onInput(input: MovementInput, delta: Double) { + var speedMultiplier = 10 + if (input.sprinting) { + speedMultiplier *= 3 + } + if (input.sneaking) { + speedMultiplier *= 2 + } + + val movement = Vec3.EMPTY + + if (input.movementForward != 0.0f) { + movement += front * input.movementForward + } + if (input.movementSideways != 0.0f) { + val cameraRight = (CAMERA_UP_VEC3 cross front).normalize() + movement += cameraRight * input.movementSideways + } + + if (movement.length2() != 0.0f) { + movement.normalizeAssign() + } + movement *= speedMultiplier + movement *= delta + + eyePosition = eyePosition + movement + } + + override fun onMouse(delta: Vec2d) { + val rotation = renderWindow.inputHandler.cameraInput.calculateRotation(delta, this.rotation) + if (rotation == this.rotation) { + return + } + this.rotation = rotation + this.front = rotation.front + } + + override fun onAttach(previous: CameraView?) { + this.eyePosition = previous?.eyePosition ?: Vec3.EMPTY + this.rotation = previous?.rotation ?: EntityRotation.EMPTY + this.front = previous?.front ?: Vec3.EMPTY + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/FirstPersonView.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/FirstPersonView.kt new file mode 100644 index 000000000..38cca7514 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/FirstPersonView.kt @@ -0,0 +1,67 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.camera.view + +import de.bixilon.kotlinglm.vec2.Vec2d +import de.bixilon.kotlinglm.vec3.Vec3 +import de.bixilon.minosoft.data.entities.EntityRotation +import de.bixilon.minosoft.data.entities.entities.player.local.LocalPlayerEntity +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.camera.Camera +import de.bixilon.minosoft.gui.rendering.input.camera.MovementInput +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY + +class FirstPersonView(private val camera: Camera) : CameraView { + override val renderWindow: RenderWindow get() = camera.renderWindow + + override val renderSelf: Boolean get() = false + override val renderOverlays: Boolean get() = true + + override var eyePosition: Vec3 = Vec3.EMPTY + + override var rotation = EntityRotation.EMPTY + override var front = Vec3.EMPTY + + + override fun onInput(input: MovementInput, delta: Double) { + val isCamera = camera.matrixHandler.entity is LocalPlayerEntity + camera.renderWindow.connection.player.input = if (isCamera) input else MovementInput() + } + + override fun onMouse(delta: Vec2d) { + val entity = camera.matrixHandler.entity + if (entity !is LocalPlayerEntity) { + return + } + val rotation = camera.renderWindow.inputHandler.cameraInput.calculateRotation(delta, entity.rotation) + entity.rotation = rotation + this.rotation = rotation + this.front = rotation.front + } + + private fun update() { + val entity = camera.matrixHandler.entity + this.eyePosition = entity.eyePosition + this.rotation = entity.rotation + this.front = this.rotation.front + } + + override fun onAttach(previous: CameraView?) { + update() + } + + override fun draw() { + update() + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt new file mode 100644 index 000000000..aae2d1cb9 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/view/ViewManager.kt @@ -0,0 +1,82 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.camera.view + +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.camera.Camera +import de.bixilon.minosoft.util.KUtil.format +import de.bixilon.minosoft.util.KUtil.toResourceLocation + +class ViewManager(private val camera: Camera) { + private val debug = DebugView(camera) + private val firstPerson = FirstPersonView(camera) + private val thirdPerson = FirstPersonView(camera) // TODO + + var view: CameraView = firstPerson + private set + + private var isDebug = false + private var isThirdPerson = false + + + fun init() { + camera.renderWindow.inputHandler.registerKeyCallback( + "minosoft:camera_debug_view".toResourceLocation(), + KeyBinding( + KeyActions.MODIFIER to setOf(KeyCodes.KEY_F4), + KeyActions.STICKY to setOf(KeyCodes.KEY_V), + ) + ) { + this.isDebug = it + updateView() + camera.renderWindow.connection.util.sendDebugMessage("Camera debug view: ${it.format()}") + } + + camera.renderWindow.inputHandler.registerKeyCallback( + "minosoft:camera_third_person".toResourceLocation(), + KeyBinding( + KeyActions.STICKY to setOf(KeyCodes.KEY_F5), + ) + ) { + this.isThirdPerson = it + updateView() + } + + view.onAttach(null) + } + + + private fun updateView(debug: Boolean = isDebug, thirdPerson: Boolean = isThirdPerson) { + val next = getView(debug, thirdPerson) + if (next == this.view) { + return + } + val previous = this.view + previous.onDetach(next) + this.view = next + this.view.onAttach(previous) + } + + private fun getView(debug: Boolean, thirdPerson: Boolean): CameraView { + if (debug) return this.debug + if (thirdPerson) return this.thirdPerson + return this.firstPerson + } + + fun draw() { + view.draw() + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/entity/models/minecraft/player/LocalPlayerModel.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/entity/models/minecraft/player/LocalPlayerModel.kt index 2d6fdc235..8be9466a4 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/entity/models/minecraft/player/LocalPlayerModel.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/entity/models/minecraft/player/LocalPlayerModel.kt @@ -20,7 +20,7 @@ import de.bixilon.minosoft.gui.rendering.entity.EntityRenderer open class LocalPlayerModel(renderer: EntityRenderer, player: PlayerEntity) : PlayerModel(renderer, player) { override val hideSkeletalModel: Boolean - get() = super.hideSkeletalModel || renderWindow.camera.renderPlayer + get() = super.hideSkeletalModel || !renderWindow.camera.view.view.renderSelf init { renderer.profile.hitbox::showLocal.observe(this, true) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/world/overlay/overlays/simple/FirstPersonOverlay.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/world/overlay/overlays/simple/FirstPersonOverlay.kt index a31504ca1..2b3c43567 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/world/overlay/overlays/simple/FirstPersonOverlay.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/framebuffer/world/overlay/overlays/simple/FirstPersonOverlay.kt @@ -18,5 +18,5 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow abstract class FirstPersonOverlay(renderWindow: RenderWindow, z: Float) : SimpleOverlay(renderWindow, z) { protected val player = renderWindow.connection.player override val render: Boolean - get() = renderWindow.camera.firstPerson + get() = renderWindow.camera.view.view.renderOverlays } 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 3025c11e0..114dc4c11 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 @@ -15,7 +15,6 @@ package de.bixilon.minosoft.gui.rendering.input import de.bixilon.kotlinglm.GLM import de.bixilon.kotlinglm.vec2.Vec2d -import de.bixilon.kotlinglm.vec3.Vec3 import de.bixilon.minosoft.config.key.KeyActions import de.bixilon.minosoft.config.key.KeyBinding import de.bixilon.minosoft.config.key.KeyCodes @@ -23,7 +22,6 @@ import de.bixilon.minosoft.data.entities.EntityRotation import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.camera.MatrixHandler import de.bixilon.minosoft.gui.rendering.input.camera.MovementInput -import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY import de.bixilon.minosoft.util.KUtil.toResourceLocation class CameraInput( @@ -31,81 +29,55 @@ class CameraInput( val matrixHandler: MatrixHandler, ) { private val connection = renderWindow.connection - private val player = connection.player private val controlsProfile = connection.profiles.controls - private val ignoreInput: Boolean - get() { - val entity = matrixHandler.entity - if (entity != player) { - return true - } - if (renderWindow.camera.debugView) { - return true - } - - return false - } - private fun registerKeyBindings() { renderWindow.inputHandler.registerCheckCallback( MOVE_SPRINT_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_CONTROL), + KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_CONTROL), ), MOVE_FORWARDS_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_W), + KeyActions.CHANGE to setOf(KeyCodes.KEY_W), ), MOVE_BACKWARDS_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_S), + KeyActions.CHANGE to setOf(KeyCodes.KEY_S), ), MOVE_LEFT_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_A), + KeyActions.CHANGE to setOf(KeyCodes.KEY_A), ), MOVE_RIGHT_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_D), + KeyActions.CHANGE to setOf(KeyCodes.KEY_D), ), FLY_UP_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_SPACE), + KeyActions.CHANGE to setOf(KeyCodes.KEY_SPACE), ), FLY_DOWN_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_SHIFT), + KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_SHIFT), ), JUMP_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_SPACE), + KeyActions.CHANGE to setOf(KeyCodes.KEY_SPACE), ), SNEAK_KEYBINDING to KeyBinding( - KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_SHIFT), + KeyActions.CHANGE to setOf(KeyCodes.KEY_LEFT_SHIFT), ), TOGGLE_FLY_KEYBINDING to KeyBinding( - KeyActions.DOUBLE_PRESS to setOf(KeyCodes.KEY_SPACE), + KeyActions.DOUBLE_PRESS to setOf(KeyCodes.KEY_SPACE), ), ) - renderWindow.inputHandler.registerKeyCallback(ZOOM_KEYBINDING, KeyBinding( + renderWindow.inputHandler.registerKeyCallback( + ZOOM_KEYBINDING, KeyBinding( KeyActions.CHANGE to setOf(KeyCodes.KEY_C), - )) { matrixHandler.zoom = if (it) 2.0f else 0.0f } + ) + ) { matrixHandler.zoom = if (it) 2.0f else 0.0f } } fun init() { registerKeyBindings() } - private fun updateDebugPosition(input: MovementInput, delta: Double) { - val cameraFront = renderWindow.camera.matrixHandler.debugRotation.front - val speedMultiplier = if (input.sprinting) 25 else 10 - val movement = Vec3.EMPTY - if (input.movementForward != 0.0f) { - movement += cameraFront * input.movementForward - } - if (input.movementSideways != 0.0f) { - val cameraRight = (MatrixHandler.CAMERA_UP_VEC3 cross cameraFront).normalize() - movement += cameraRight * input.movementSideways - } - renderWindow.camera.matrixHandler.debugPosition = renderWindow.camera.matrixHandler.debugPosition + (if (movement.length2() != 0.0f) movement.normalize() else return) * delta * speedMultiplier - } - - fun update(delta: Double) { + fun updateInput(delta: Double) { val input = MovementInput( pressingForward = renderWindow.inputHandler.isKeyBindingDown(MOVE_FORWARDS_KEYBINDING), pressingBack = renderWindow.inputHandler.isKeyBindingDown(MOVE_BACKWARDS_KEYBINDING), @@ -118,22 +90,15 @@ class CameraInput( flyUp = renderWindow.inputHandler.isKeyBindingDown(FLY_UP_KEYBINDING), toggleFlyDown = renderWindow.inputHandler.isKeyBindingDown(TOGGLE_FLY_KEYBINDING), ) - if (renderWindow.camera.debugView) { - updateDebugPosition(input, delta) - } - connection.player.input = if (ignoreInput || renderWindow.camera.debugView) MovementInput() else input + renderWindow.camera.view.view.onInput(input, delta) } - fun mouseCallback(delta: Vec2d) { - if (renderWindow.camera.debugView) { - matrixHandler.debugRotation = mouseCallback(delta, matrixHandler.debugRotation) - } else if (!ignoreInput) { - player.rotation = mouseCallback(delta, player.rotation) - } + fun updateMouse(movement: Vec2d) { + renderWindow.camera.view.view.onMouse(movement) } - private fun mouseCallback(delta: Vec2d, rotation: EntityRotation): EntityRotation { - delta *= 0.1f * controlsProfile.mouse.sensitivity + fun calculateRotation(delta: Vec2d, rotation: EntityRotation): EntityRotation { + val delta = delta * 0.1f * controlsProfile.mouse.sensitivity var yaw = delta.x + rotation.yaw if (yaw > 180) { yaw -= 360 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 d88b7744f..8a4ff5272 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 @@ -112,7 +112,7 @@ class RenderWindowInputHandler( return@listen } - cameraInput.mouseCallback(it.delta) + cameraInput.updateMouse(it.delta) } profile::keyBindings.observeMap(this) { @@ -349,7 +349,7 @@ class RenderWindowInputHandler( } fun draw(delta: Double) { - cameraInput.update(delta) + cameraInput.updateInput(delta) interactionManager.draw(delta) } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/clouds/CloudRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/clouds/CloudRenderer.kt index 2b75eca21..451fb7705 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/sky/clouds/CloudRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/sky/clouds/CloudRenderer.kt @@ -205,7 +205,7 @@ class CloudRenderer( private fun setYOffset() { - val y = renderWindow.camera.matrixHandler.eyePosition.y + val y = renderWindow.camera.matrixHandler.entity.eyePosition.y var yOffset = 0.0f if (baseHeight - y > maxDistance) { yOffset = y - baseHeight + maxDistance diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/sound/AudioPlayer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/sound/AudioPlayer.kt index 017d00aff..2e8539b4d 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/sound/AudioPlayer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/sound/AudioPlayer.kt @@ -22,7 +22,7 @@ import de.bixilon.kutil.observer.DataObserver.Companion.observe import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.world.audio.AbstractAudioPlayer import de.bixilon.minosoft.gui.rendering.Rendering -import de.bixilon.minosoft.gui.rendering.camera.MatrixHandler +import de.bixilon.minosoft.gui.rendering.camera.CameraDefinition.CAMERA_UP_VEC3 import de.bixilon.minosoft.gui.rendering.events.CameraPositionChangeEvent import de.bixilon.minosoft.gui.rendering.sound.sounds.Sound import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY @@ -96,7 +96,7 @@ class AudioPlayer( connection.events.listen { queue += { listener.position = Vec3(it.newPosition) - listener.setOrientation(it.renderWindow.camera.matrixHandler.cameraFront, MatrixHandler.CAMERA_UP_VEC3) + listener.setOrientation(it.renderWindow.camera.view.view.front, CAMERA_UP_VEC3) } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/world/border/WorldBorderRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/world/border/WorldBorderRenderer.kt index 41b074274..201aa70bd 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/world/border/WorldBorderRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/world/border/WorldBorderRenderer.kt @@ -75,7 +75,7 @@ class WorldBorderRenderer( } val textureOffset = (offsetReset - time) / ANIMATION_SPEED.toFloat() shader.textureOffset = 1.0f - textureOffset - shader.cameraHeight = renderWindow.camera.matrixHandler.eyePosition.y + shader.cameraHeight = renderWindow.camera.matrixHandler.entity.eyePosition.y val distance = border.getDistanceTo(renderWindow.connection.player.position) val strength = 1.0f - (distance.toFloat().clamp(0.0f, 100.0f) / 100.0f) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/world/chunk/ChunkBorderRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/world/chunk/ChunkBorderRenderer.kt index 2ef2247aa..5c9441332 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/world/chunk/ChunkBorderRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/world/chunk/ChunkBorderRenderer.kt @@ -74,7 +74,7 @@ class ChunkBorderRenderer( this.unload = true return } - val eyePosition = renderWindow.camera.matrixHandler.eyePosition.blockPosition + val eyePosition = renderWindow.camera.matrixHandler.entity.eyePosition.blockPosition val chunkPosition = eyePosition.chunkPosition val sectionHeight = eyePosition.sectionHeight if (chunkPosition == this.chunkPosition && sectionHeight == this.sectionHeight && mesh != null) {