mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
rendering: replace some cos, sin and rad functions with extension functions (glm)
This commit is contained in:
parent
bdfe1345fa
commit
56eec8fb73
@ -62,15 +62,9 @@ enum class Directions(val directionVector: Vec3i) {
|
|||||||
|
|
||||||
fun getFaceSize(start: Vec3, end: Vec3): FaceSize {
|
fun getFaceSize(start: Vec3, end: Vec3): FaceSize {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
DOWN, UP -> {
|
DOWN, UP -> FaceSize(Vec2i(start.x, start.z), Vec2i(end.x, end.z))
|
||||||
FaceSize(Vec2i(start.x, start.z), Vec2i(end.x, end.z))
|
NORTH, SOUTH -> FaceSize(Vec2i(start.x, start.y), Vec2i(end.x, end.y))
|
||||||
}
|
EAST, WEST -> FaceSize(Vec2i(start.y, start.z), Vec2i(end.y, end.z))
|
||||||
NORTH, SOUTH -> {
|
|
||||||
FaceSize(Vec2i(start.x, start.y), Vec2i(end.x, end.y))
|
|
||||||
}
|
|
||||||
EAST, WEST -> {
|
|
||||||
FaceSize(Vec2i(start.y, start.z), Vec2i(end.y, end.z))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,12 +30,13 @@ import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositio
|
|||||||
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositionSending
|
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositionSending
|
||||||
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerRotationSending
|
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerRotationSending
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
|
import glm_.func.cos
|
||||||
|
import glm_.func.rad
|
||||||
|
import glm_.func.sin
|
||||||
import glm_.glm
|
import glm_.glm
|
||||||
import glm_.mat4x4.Mat4
|
import glm_.mat4x4.Mat4
|
||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import kotlin.math.cos
|
|
||||||
import kotlin.math.sin
|
|
||||||
|
|
||||||
class Camera(
|
class Camera(
|
||||||
val connection: Connection,
|
val connection: Connection,
|
||||||
@ -227,7 +228,7 @@ class Camera(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4 {
|
private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4 {
|
||||||
return glm.perspective(glm.radians(fov / (zoom + 1.0f)), screenDimensions.x / screenDimensions.y, 0.1f, 1000f)
|
return glm.perspective((fov / (zoom + 1.0f)).rad, screenDimensions.x / screenDimensions.y, 0.1f, 1000f)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateViewMatrix(): Mat4 {
|
private fun calculateViewMatrix(): Mat4 {
|
||||||
@ -240,10 +241,10 @@ class Camera(
|
|||||||
|
|
||||||
fun setRotation(yaw: Double, pitch: Double) {
|
fun setRotation(yaw: Double, pitch: Double) {
|
||||||
cameraFront = Vec3(
|
cameraFront = Vec3(
|
||||||
(cos(glm.radians(yaw + 90)) * cos(glm.radians(-pitch))).toFloat(),
|
(yaw + 90).rad.cos * (-pitch).rad.cos,
|
||||||
sin(glm.radians(-pitch)).toFloat(),
|
(-pitch).rad.sin,
|
||||||
(sin(glm.radians(yaw + 90)) * cos(glm.radians(-pitch))).toFloat())
|
(yaw + 90).rad.sin * (-pitch).rad.cos
|
||||||
.normalize()
|
).normalize()
|
||||||
|
|
||||||
cameraRight = (cameraFront cross CAMERA_UP_VEC3).normalize()
|
cameraRight = (cameraFront cross CAMERA_UP_VEC3).normalize()
|
||||||
cameraUp = (cameraRight cross cameraFront).normalize()
|
cameraUp = (cameraRight cross cameraFront).normalize()
|
||||||
|
@ -5,7 +5,9 @@ import de.bixilon.minosoft.gui.rendering.Camera
|
|||||||
import de.bixilon.minosoft.gui.rendering.RenderConstants
|
import de.bixilon.minosoft.gui.rendering.RenderConstants
|
||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.rotate
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil.rotate
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
import glm_.glm
|
import glm_.func.cos
|
||||||
|
import glm_.func.rad
|
||||||
|
import glm_.func.sin
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
class Frustum(private val camera: Camera) {
|
class Frustum(private val camera: Camera) {
|
||||||
@ -28,18 +30,18 @@ class Frustum(private val camera: Camera) {
|
|||||||
|
|
||||||
private fun calculateSideNormals() {
|
private fun calculateSideNormals() {
|
||||||
val cameraRealUp = (camera.cameraRight cross camera.cameraFront).normalize()
|
val cameraRealUp = (camera.cameraRight cross camera.cameraFront).normalize()
|
||||||
val angle = glm.radians(camera.fov - 90.0f)
|
val angle = (camera.fov - 90.0f).rad
|
||||||
val sin = glm.sin(angle)
|
val sin = angle.sin
|
||||||
val cos = glm.cos(angle)
|
val cos = angle.cos
|
||||||
normals.add(camera.cameraFront.rotate(cameraRealUp, sin, cos).normalize())
|
normals.add(camera.cameraFront.rotate(cameraRealUp, sin, cos).normalize())
|
||||||
normals.add(camera.cameraFront.rotate(cameraRealUp, -sin, cos).normalize()) // negate angle -> negate sin
|
normals.add(camera.cameraFront.rotate(cameraRealUp, -sin, cos).normalize()) // negate angle -> negate sin
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateVerticalNormals() {
|
private fun calculateVerticalNormals() {
|
||||||
val aspect = camera.renderWindow.screenDimensions.x / camera.renderWindow.screenDimensions.y // ToDo: x/y or y/x
|
val aspect = camera.renderWindow.screenDimensions.x / camera.renderWindow.screenDimensions.y // ToDo: x/y or y/x
|
||||||
val angle = glm.radians(camera.fov * aspect - 90.0f)
|
val angle = (camera.fov * aspect - 90.0f).rad
|
||||||
val sin = glm.sin(angle)
|
val sin = angle.sin
|
||||||
val cos = glm.cos(angle)
|
val cos = angle.cos
|
||||||
normals.add(camera.cameraFront.rotate(camera.cameraRight, sin, cos).normalize())
|
normals.add(camera.cameraFront.rotate(camera.cameraRight, sin, cos).normalize())
|
||||||
normals.add(camera.cameraFront.rotate(camera.cameraRight, -sin, cos).normalize()) // negate angle -> negate sin
|
normals.add(camera.cameraFront.rotate(camera.cameraRight, -sin, cos).normalize()) // negate angle -> negate sin
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,10 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.gui.rendering.chunk.models
|
package de.bixilon.minosoft.gui.rendering.chunk.models
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement
|
||||||
import glm_.vec2.Vec2i
|
import glm_.vec2.Vec2i
|
||||||
|
|
||||||
data class FaceSize(
|
data class FaceSize(
|
||||||
val start: Vec2i = Vec2i(0, 0),
|
val start: Vec2i = Vec2i(0, 0),
|
||||||
val end: Vec2i = Vec2i(16, 16),
|
val end: Vec2i = Vec2i(BlockModelElement.BLOCK_RESOLUTION, BlockModelElement.BLOCK_RESOLUTION),
|
||||||
)
|
)
|
||||||
|
@ -18,7 +18,8 @@ import de.bixilon.minosoft.data.Axes
|
|||||||
import de.bixilon.minosoft.data.Directions
|
import de.bixilon.minosoft.data.Directions
|
||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.rotate
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil.rotate
|
||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec3
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec3
|
||||||
import glm_.glm
|
import glm_.func.cos
|
||||||
|
import glm_.func.rad
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
open class BlockModelElement(data: JsonObject) {
|
open class BlockModelElement(data: JsonObject) {
|
||||||
@ -41,7 +42,7 @@ open class BlockModelElement(data: JsonObject) {
|
|||||||
|
|
||||||
data["rotation"]?.asJsonObject?.let {
|
data["rotation"]?.asJsonObject?.let {
|
||||||
val axis = Axes.valueOf(it["axis"].asString.toUpperCase())
|
val axis = Axes.valueOf(it["axis"].asString.toUpperCase())
|
||||||
val angle = glm.radians(it["angle"].asFloat)
|
val angle = it["angle"].asFloat.rad
|
||||||
val rescale = it["rescale"]?.asBoolean ?: false
|
val rescale = it["rescale"]?.asBoolean ?: false
|
||||||
rotatePositions(transformedPositions, axis, angle, it["origin"].asJsonArray.toVec3(), rescale)
|
rotatePositions(transformedPositions, axis, angle, it["origin"].asJsonArray.toVec3(), rescale)
|
||||||
}
|
}
|
||||||
@ -81,7 +82,7 @@ open class BlockModelElement(data: JsonObject) {
|
|||||||
var transformedPosition = position - origin
|
var transformedPosition = position - origin
|
||||||
transformedPosition = transformedPosition.rotate(angle, axis)
|
transformedPosition = transformedPosition.rotate(angle, axis)
|
||||||
if (rescale) {
|
if (rescale) {
|
||||||
transformedPosition = transformedPosition / glm.cos(angle)
|
transformedPosition = transformedPosition / angle.cos
|
||||||
}
|
}
|
||||||
positions[i] = transformedPosition + origin
|
positions[i] = transformedPosition + origin
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,8 @@ import com.google.gson.JsonObject
|
|||||||
import de.bixilon.minosoft.data.Directions
|
import de.bixilon.minosoft.data.Directions
|
||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil
|
||||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.readUV
|
import de.bixilon.minosoft.gui.rendering.util.VecUtil.readUV
|
||||||
import glm_.Java.Companion.glm
|
import glm_.func.cos
|
||||||
|
import glm_.func.sin
|
||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -99,8 +100,8 @@ class BlockModelFace {
|
|||||||
if (angle == 0.0f) {
|
if (angle == 0.0f) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val sin = glm.sin(angle)
|
val sin = angle.sin
|
||||||
val cos = glm.cos(angle)
|
val cos = angle.cos
|
||||||
for ((i, position) in positions.withIndex()) {
|
for ((i, position) in positions.withIndex()) {
|
||||||
val offset = position - TEXTURE_MIDDLE
|
val offset = position - TEXTURE_MIDDLE
|
||||||
positions[i] = VecUtil.getRotatedValues(offset.x, offset.y, sin, cos) + TEXTURE_MIDDLE
|
positions[i] = VecUtil.getRotatedValues(offset.x, offset.y, sin, cos) + TEXTURE_MIDDLE
|
||||||
|
@ -18,7 +18,8 @@ import com.google.gson.JsonElement
|
|||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import de.bixilon.minosoft.data.Axes
|
import de.bixilon.minosoft.data.Axes
|
||||||
import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement
|
import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModelElement
|
||||||
import glm_.glm
|
import glm_.func.cos
|
||||||
|
import glm_.func.sin
|
||||||
import glm_.vec2.Vec2
|
import glm_.vec2.Vec2
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
|
|
||||||
@ -44,15 +45,15 @@ object VecUtil {
|
|||||||
}
|
}
|
||||||
return when (axis) {
|
return when (axis) {
|
||||||
Axes.X -> {
|
Axes.X -> {
|
||||||
val rotatedValues = getRotatedValues(this.y, this.z, glm.sin(angle), glm.cos(angle))
|
val rotatedValues = getRotatedValues(this.y, this.z, angle.sin, angle.cos)
|
||||||
Vec3(this.x, rotatedValues)
|
Vec3(this.x, rotatedValues)
|
||||||
}
|
}
|
||||||
Axes.Y -> {
|
Axes.Y -> {
|
||||||
val rotatedValues = getRotatedValues(this.x, this.z, glm.sin(angle), glm.cos(angle))
|
val rotatedValues = getRotatedValues(this.x, this.z, angle.sin, angle.cos)
|
||||||
Vec3(rotatedValues.x, this.y, rotatedValues.y)
|
Vec3(rotatedValues.x, this.y, rotatedValues.y)
|
||||||
}
|
}
|
||||||
Axes.Z -> {
|
Axes.Z -> {
|
||||||
val rotatedValues = getRotatedValues(this.x, this.y, glm.sin(angle), glm.cos(angle))
|
val rotatedValues = getRotatedValues(this.x, this.y, angle.sin, angle.cos)
|
||||||
Vec3(rotatedValues.x, rotatedValues.y, this.z)
|
Vec3(rotatedValues.x, rotatedValues.y, this.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,19 +63,6 @@ object VecUtil {
|
|||||||
return this * cos + (axis cross this) * sin + axis * (axis dot this) * (1 - cos)
|
return this * cos + (axis cross this) * sin + axis * (axis dot this) * (1 - cos)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Vec3.oneContainsIgnoreZero(vec3: Vec3): Boolean {
|
|
||||||
if (x == vec3.x) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if (y == vec3.y) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if (z == vec3.z) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
fun JsonArray.readUV(): Pair<Vec2, Vec2> {
|
fun JsonArray.readUV(): Pair<Vec2, Vec2> {
|
||||||
return Pair(Vec2(this[0].asFloat, this[3].asFloat), Vec2(this[2].asFloat, this[1].asFloat))
|
return Pair(Vec2(this[0].asFloat, this[3].asFloat), Vec2(this[2].asFloat, this[1].asFloat))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user