wip camera debug view #80

This commit is contained in:
Bixilon 2022-05-13 12:15:33 +02:00
parent 8a00410527
commit 7ba79fe1d1
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 75 additions and 19 deletions

View File

@ -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() {

View File

@ -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)
}
}
}

View File

@ -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)

View File

@ -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 {

View File

@ -348,7 +348,7 @@ class RenderWindowInputHandler(
}
fun draw(delta: Double) {
cameraInput.update()
cameraInput.update(delta)
interactionManager.draw(delta)
}
}

View File

@ -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
}