rendering: improve camera (correct yaw and pitch handling), more infos on debug screen

This commit is contained in:
Bixilon 2021-02-13 16:12:24 +01:00
parent cb27476401
commit fa7b516d26
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 84 additions and 25 deletions

View File

@ -28,6 +28,10 @@ data class Location(val x: Double, val y: Double, val z: Double) {
return Vec3(x, y, z) return Vec3(x, y, z)
} }
fun toBlockPosition(): BlockPosition {
return BlockPosition((x - 0.5).toInt(), y.toInt(), (z - 0.5).toInt()) // ToDo
}
companion object { companion object {
@JvmStatic @JvmStatic
fun fromPosition(position: BlockPosition): Location { fun fromPosition(position: BlockPosition): Location {

View File

@ -42,6 +42,11 @@ data class BlockPosition(val x: Int, val y: Int, val z: Int) {
return InChunkLocation(x, this.y, z) return InChunkLocation(x, this.y, z)
} }
fun getInChunkSectionLocation(): InChunkSectionLocation {
val location = getInChunkLocation()
return InChunkSectionLocation(location.x, this.y % 16, location.z)
}
fun getSectionHeight(): Int { fun getSectionHeight(): Int {
return y / ProtocolDefinition.SECTION_HEIGHT_Y return y / ProtocolDefinition.SECTION_HEIGHT_Y
} }

View File

@ -26,14 +26,20 @@ class Camera(private var fov: Float, private val windowId: Long) {
xOffset *= mouseSensitivity xOffset *= mouseSensitivity
yOffset *= mouseSensitivity yOffset *= mouseSensitivity
yaw += xOffset yaw += xOffset
pitch += yOffset pitch -= yOffset
// make sure that when pitch is out of bounds, screen doesn't get flipped // make sure that when pitch is out of bounds, screen doesn't get flipped
if (this.pitch > 89.0) { if (this.pitch > 89.9) {
this.pitch = 89.0 this.pitch = 89.9
} else if (this.pitch < -89.0) { } else if (this.pitch < -89.9) {
this.pitch = -89.0 this.pitch = -89.9
} }
if (this.yaw > 180) {
this.yaw -= 360
} else if (this.yaw < -180) {
this.yaw += 360
}
this.yaw %= 180
setRotation(yaw, pitch) setRotation(yaw, pitch)
} }
@ -90,7 +96,7 @@ class Camera(private var fov: Float, private val windowId: Long) {
} }
fun setRotation(yaw: Double, pitch: Double) { fun setRotation(yaw: Double, pitch: Double) {
cameraFront = Vec3((cos(glm.radians(yaw)) * cos(glm.radians(pitch))).toFloat(), sin(glm.radians(pitch)).toFloat(), (sin(glm.radians(yaw)) * cos(glm.radians(pitch))).toFloat()).normalize() cameraFront = Vec3((cos(glm.radians(yaw + 90)) * cos(glm.radians(-pitch))).toFloat(), sin(glm.radians(-pitch)).toFloat(), (sin(glm.radians(yaw + 90)) * cos(glm.radians(-pitch))).toFloat()).normalize()
} }
companion object { companion object {

View File

@ -1,5 +1,9 @@
package de.bixilon.minosoft.gui.rendering.hud.elements.text package de.bixilon.minosoft.gui.rendering.hud.elements.text
import de.bixilon.minosoft.data.entities.Location
import de.bixilon.minosoft.data.world.BlockPosition
import de.bixilon.minosoft.data.world.ChunkLocation
import de.bixilon.minosoft.data.world.InChunkSectionLocation
import de.bixilon.minosoft.gui.rendering.font.FontBindings import de.bixilon.minosoft.gui.rendering.font.FontBindings
import org.lwjgl.opengl.GL11.* import org.lwjgl.opengl.GL11.*
import oshi.SystemInfo import oshi.SystemInfo
@ -11,27 +15,41 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex
private val systemInfoHardwareAbstractionLayer = systemInfo.hardware private val systemInfoHardwareAbstractionLayer = systemInfo.hardware
private val processorText = " ${runtime.availableProcessors()}x ${systemInfoHardwareAbstractionLayer.processor.processorIdentifier.name}" private val processorText = " ${runtime.availableProcessors()}x ${systemInfoHardwareAbstractionLayer.processor.processorIdentifier.name.replace("\\s{2,}".toRegex(), "")}"
private lateinit var gpuText: String private lateinit var gpuText: String
private lateinit var gpuVersionText: String private lateinit var gpuVersionText: String
private val maxMemoryText: String = getFormattedMaxMemory() private val maxMemoryText: String = getFormattedMaxMemory()
private val systemMemoryText: String = formatBytes(systemInfoHardwareAbstractionLayer.memory.total) private val systemMemoryText: String = formatBytes(systemInfoHardwareAbstractionLayer.memory.total)
private val osText: String = "${System.getProperty("os.name")}: ${systemInfo.operatingSystem.family} ${systemInfo.operatingSystem.bitness}bit"
// only calculate once per update values
private lateinit var location: Location
private lateinit var blockPosition: BlockPosition
private lateinit var chunkLocation: ChunkLocation
private var sectionHeight: Int = 0
private lateinit var inChunkSectionLocation: InChunkSectionLocation
override fun prepare(chatComponents: Map<FontBindings, MutableList<Any>>) { override fun prepare(chatComponents: Map<FontBindings, MutableList<Any>>) {
calculateDynamicValues()
chatComponents[FontBindings.LEFT_UP]!!.addAll(listOf( chatComponents[FontBindings.LEFT_UP]!!.addAll(listOf(
"§fFPS: ${getFPS()}", "FPS: ${getFPS()}",
"§fTimings: avg ${getAvgFrameTime()}ms, min ${getMinFrameTime()}ms, max ${getMaxFrameTime()}ms", "Timings: avg ${getAvgFrameTime()}ms, min ${getMinFrameTime()}ms, max ${getMaxFrameTime()}ms",
"§fXYZ ${getLocation()}", "Connected to: ${hudTextElement.connection.address}",
"§fConnected to: ${hudTextElement.connection.address}", "",
"XYZ ${getLocation()}",
"Block ${getBlockPosition()}",
"Chunk ${getChunkLocation()}",
"Facing ${getFacing()}",
"Dimension ${hudTextElement.connection.player.world.dimension}",
)) ))
chatComponents[FontBindings.RIGHT_UP]!!.addAll(listOf( chatComponents[FontBindings.RIGHT_UP]!!.addAll(listOf(
"§fJava: ${Runtime.version()} ${System.getProperty("sun.arch.data.model")}bit", "Java: ${Runtime.version()} ${System.getProperty("sun.arch.data.model")}bit",
"§fMemory: ${getUsedMemoryPercent()}% ${getFormattedUsedMemory()}/$maxMemoryText", "Memory: ${getUsedMemoryPercent()}% ${getFormattedUsedMemory()}/$maxMemoryText",
"§fAllocated: ${getAllocatedMemoryPercent()}% ${getFormattedAllocatedMemory()}", "Allocated: ${getAllocatedMemoryPercent()}% ${getFormattedAllocatedMemory()}",
"§fSystem: $systemMemoryText", "System: $systemMemoryText",
"", "",
"OS: ${System.getProperty("os.name")}", "OS: $osText",
"CPU: $processorText", "CPU: $processorText",
"", "",
"Display: ${getScreenDimensions()}", "Display: ${getScreenDimensions()}",
@ -98,18 +116,39 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex
return getAllocatedMemory() * 100 / runtime.maxMemory() return getAllocatedMemory() * 100 / runtime.maxMemory()
} }
private fun getLocation(): String {
val cameraPosition = hudTextElement.renderWindow.camera.cameraPosition
return "${formatCoordinate(cameraPosition.x)} / ${formatCoordinate(cameraPosition.y)} / ${formatCoordinate(cameraPosition.z)}"
}
private fun getScreenDimensions(): String { private fun getScreenDimensions(): String {
return "${hudTextElement.renderWindow.screenWidth}x${hudTextElement.renderWindow.screenHeight}" return "${hudTextElement.renderWindow.screenWidth}x${hudTextElement.renderWindow.screenHeight}"
} }
private fun getLocation(): String {
return "${formatCoordinate(location.x)} / ${formatCoordinate(location.y)} / ${formatCoordinate(location.z)}"
}
private fun getBlockPosition(): String {
return "${blockPosition.x} / ${blockPosition.y} / ${blockPosition.z}"
}
private fun getChunkLocation(): String {
return "${inChunkSectionLocation.x} ${inChunkSectionLocation.y} ${inChunkSectionLocation.z} in ${chunkLocation.x} $sectionHeight ${chunkLocation.z}"
}
private fun getFacing(): String {
val yaw = hudTextElement.renderWindow.camera.yaw
val pitch = hudTextElement.renderWindow.camera.pitch
return "todo (${formatRotation(yaw)} / ${formatRotation(pitch)})"
}
private fun calculateDynamicValues() {
location = Location(hudTextElement.renderWindow.camera.cameraPosition)
blockPosition = location.toBlockPosition()
chunkLocation = blockPosition.getChunkLocation()
sectionHeight = blockPosition.getSectionHeight()
inChunkSectionLocation = blockPosition.getInChunkSectionLocation()
}
companion object { companion object {
private val UNITS = listOf("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB") private val UNITS = listOf("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
fun formatBytes(bytes: Long): String { fun formatBytes(bytes: Long): String {
var lastFactor = 1L var lastFactor = 1L
var currentFactor = 1024L var currentFactor = 1024L
@ -126,8 +165,12 @@ class HUDDebugScreenElement(private val hudTextElement: HUDTextElement) : HUDTex
throw IllegalArgumentException() throw IllegalArgumentException()
} }
fun formatCoordinate(coordinate: Float): String { fun formatCoordinate(coordinate: Double): String {
return "%.4f".format(coordinate) return "%.3f".format(coordinate)
}
fun formatRotation(rotation: Double): String {
return "%.1f".format(rotation)
} }
} }
} }

View File

@ -50,10 +50,11 @@ class HUDTextElement(val connection: Connection, val hudRenderer: HUDRenderer, v
meshData.add(0f) meshData.add(0f)
meshData.add(0f) meshData.add(0f)
meshData.add(0f) meshData.add(0f)
meshData.add(0f) meshData.add(0f)
meshData.add(0f) meshData.add(0f)
meshData.add(0f) meshData.add(0f)
meshData.add(0.2f) meshData.add(0.3f)
} }
drawLetterVertex(start) drawLetterVertex(start)