diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/ChunkPreparer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/ChunkPreparer.kt index df9f0eb5a..d3d7d1960 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/ChunkPreparer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/ChunkPreparer.kt @@ -71,19 +71,17 @@ object ChunkPreparer { } } } - // if(block.fullIdentifier != "minecraft:dispenser"){ - // continue - // } + fun drawFace() { - block.blockModel?.render(position, direction)?.let { data.addAll(it) } + block.blockModel.render(position, direction, data) } if (blockToCheck == null) { blockToCheck = section.getBlock(position.getLocationByDirection(direction)) } if (blockToCheck != null) { - val blockTransparent = block.blockModel?.isTransparent(direction) ?: false - val checkTransparent = blockToCheck!!.blockModel?.isTransparent(direction) ?: false + val blockTransparent = block.blockModel.isTransparent(direction) + val checkTransparent = blockToCheck!!.blockModel.isTransparent(direction) if (blockTransparent && checkTransparent) { continue } @@ -91,9 +89,10 @@ object ChunkPreparer { drawFace() continue } - if (block.blockModel?.isCullFace(direction) == true && blockToCheck!!.blockModel?.isCullFace(direction.inverse()) == true) { + if (block.blockModel.isCullFace(direction) && blockToCheck!!.blockModel.isCullFace(direction.inverse())) { continue } + // ToDo: Block rotations (this is buggy) } drawFace() diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/Mesh.java b/src/main/java/de/bixilon/minosoft/gui/rendering/Mesh.java index 162e0b50d..d7084fe1e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/Mesh.java +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/Mesh.java @@ -1,6 +1,5 @@ package de.bixilon.minosoft.gui.rendering; -import de.bixilon.minosoft.data.world.ChunkLocation; import de.bixilon.minosoft.gui.rendering.shader.Shader; import glm_.vec3.Vec3; import org.lwjgl.opengl.GL11; @@ -16,11 +15,11 @@ import static org.lwjgl.opengl.GL30.*; public class Mesh { int vAO; int vBO; - Vec3 chunkPosition; + Vec3 worldPosition; int trianglesCount; - public Mesh(float[] data, ChunkLocation location, int sectionHeight) { - this.chunkPosition = new Vec3(location.getX(), sectionHeight, location.getZ()); + public Mesh(float[] data, Vec3 worldPosition) { + this.worldPosition = worldPosition; this.trianglesCount = data.length / 6; // <- bytes per vertex this.vAO = glGenVertexArrays(); this.vBO = glGenBuffers(); @@ -41,7 +40,7 @@ public class Mesh { } public void draw(Shader chunkShader) { - chunkShader.setVec3("chunkPosition", this.chunkPosition); + chunkShader.setVec3("worldPosition", this.worldPosition); glBindVertexArray(this.vAO); glDrawArrays(GL_TRIANGLES, 0, this.trianglesCount); } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 53107f5f9..186bd1c21 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -89,8 +89,8 @@ class RenderWindow(private val connection: Connection) { glfwSetInputMode(windowId, GLFW_CURSOR, GLFW_CURSOR_DISABLED) glfwSetCursorPosCallback(windowId) { windowId: Long, xPos: Double, yPos: Double -> camera.mouseCallback(xPos, yPos) } MemoryStack.stackPush().let { stack -> - val pWidth = stack.mallocInt(1) // int* - val pHeight = stack.mallocInt(1) // int* + val pWidth = stack.mallocInt(1) + val pHeight = stack.mallocInt(1) // Get the window size passed to glfwCreateWindow glfwGetWindowSize(windowId, pWidth, pHeight) @@ -111,7 +111,7 @@ class RenderWindow(private val connection: Connection) { // Make the window visible glfwShowWindow(windowId) GL.createCapabilities() - glClearColor(0.2f, 0.3f, 0.3f, 1.0f) + glClearColor(137 / 256f, 207 / 256f, 240 / 256f, 1.0f) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) @@ -129,7 +129,7 @@ class RenderWindow(private val connection: Connection) { latch?.countDown() } - fun startLoop() { + fun startRenderLoop() { var framesLastSecond = 0 var lastCalcTime = glfwGetTime() var frameTimeLastCalc = 0.0 @@ -154,28 +154,27 @@ class RenderWindow(private val connection: Connection) { } } - glfwSwapBuffers(windowId) // swap the color buffers + glfwSwapBuffers(windowId) - // Poll for window events. The key callback above will only be - // invoked during this call. glfwPollEvents() - for (renderQueueElement in renderQueue) { - renderQueueElement.run() - renderQueue.remove(renderQueueElement) - } - camera.handleInput(deltaTime) frameTimeLastCalc += glfwGetTime() - currentFrame if (glfwGetTime() - lastCalcTime >= 0.5) { - glfwSetWindowTitle(windowId, "FPS: ${framesLastSecond * 2} (${(0.5 * framesLastSecond / (frameTimeLastCalc)).roundToInt()})") + glfwSetWindowTitle(windowId, "Minosoft | FPS: ${framesLastSecond * 2} (${(0.5 * framesLastSecond / (frameTimeLastCalc)).roundToInt()})") lastCalcTime = glfwGetTime() framesLastSecond = 0 frameTimeLastCalc = 0.0 } framesLastSecond++ + + + for (renderQueueElement in renderQueue) { + renderQueueElement.run() + renderQueue.remove(renderQueueElement) + } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/Renderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/Renderer.kt index faa9e5253..cf78fb53a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/Renderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/Renderer.kt @@ -24,7 +24,7 @@ class Renderer(private val connection: Connection) { Thread({ Log.info("Hello LWJGL " + Version.getVersion() + "!") renderWindow.init(latch) - renderWindow.startLoop() + renderWindow.startRenderLoop() renderWindow.exit() }, "Rendering").start() } @@ -38,13 +38,13 @@ class Renderer(private val connection: Connection) { fun prepareChunkSection(chunkLocation: ChunkLocation, sectionHeight: Int, section: ChunkSection) { executor.execute { - latch.waitUntilZero() + latch.waitUntilZero() // Wait until rendering is started val data = prepareChunk(connection.player.world, chunkLocation, sectionHeight, section) val sectionMap = renderWindow.chunkSectionsToDraw[chunkLocation]!! renderWindow.renderQueue.add { sectionMap[sectionHeight]?.unload() sectionMap.remove(sectionHeight) - sectionMap[sectionHeight] = Mesh(data, chunkLocation, sectionHeight) + sectionMap[sectionHeight] = Mesh(data, Vec3(chunkLocation.x, sectionHeight, chunkLocation.z)) } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModel.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModel.kt index e581a46ac..72e3f6a15 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModel.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModel.kt @@ -13,7 +13,7 @@ open class BlockModel(val parent: BlockModel? = null, json: JsonObject) { private val textureMapping: MutableMap = mutableMapOf() private var elements: MutableList = parent?.elements?.toMutableList() ?: mutableListOf() private var rotation: Vec3 - private var uvLock = false + private var uvLock = false // ToDo init { json["textures"]?.asJsonObject?.let { @@ -51,24 +51,19 @@ open class BlockModel(val parent: BlockModel? = null, json: JsonObject) { } - open fun render(position: InChunkSectionLocation, direction: Directions): List { - // if (rotation.x == 0f && rotation.y == 0f && rotation.z == 0f) { - // return emptyList() // ToDo - // } - val data: MutableList = mutableListOf() - + open fun render(position: InChunkSectionLocation, direction: Directions, data: MutableList) { var model = Mat4().translate(Vec3(position.x, position.y, position.z)) if (rotation.x > 0 || rotation.y > 0 || rotation.z > 0) { model = model.rotate(glm.radians(rotation.x), Vec3(-1, 0, 0)) .rotate(glm.radians(rotation.y), Vec3(0, -1, 0)) .rotate(glm.radians(rotation.z), Vec3(0, 0, -1)) + // ToDo: this should be made easier/effizienter } for (element in elements) { - data.addAll(element.render(textureMapping, model, direction, rotation)) + element.render(textureMapping, model, direction, rotation, data) } - return data } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModelElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModelElement.kt index 6f09a8335..f2910ccbb 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModelElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/models/BlockModelElement.kt @@ -40,9 +40,9 @@ open class BlockModelElement(data: JsonObject) { private val positionDownRightFront = Vec3(BlockModel.positionToFloat(to.x), BlockModel.positionToFloat(from.y), BlockModel.positionToFloat(from.z)) private val positionDownRightBack = Vec3(BlockModel.positionToFloat(to.x), BlockModel.positionToFloat(from.y), BlockModel.positionToFloat(to.z)) - open fun render(textureMapping: MutableMap, model: Mat4, direction: Directions, rotation: Vec3): List { - val face = faces[direction] ?: return emptyList() - val data: MutableList = mutableListOf() + open fun render(textureMapping: MutableMap, model: Mat4, direction: Directions, rotation: Vec3, data: MutableList) { + val face = faces[direction] ?: return // Not our face + val texture = textureMapping[face.textureName]?.id ?: TextureArray.DEBUG_TEXTURE.id fun addToData(vec3: Vec3, textureCoordinates: Vec2) { @@ -56,25 +56,23 @@ open class BlockModelElement(data: JsonObject) { data.add(texture.toFloat()) // ToDo: Compact this } - fun createQuad(first: Vec3, second: Vec3, third: Vec3, fourth: Vec3, fifth: Vec2 = face.texturLeftDown, sixth: Vec2 = face.texturRightDown, seventh: Vec2 = face.texturRightUp, eighth: Vec2 = face.texturLeftUp) { - addToData(first, sixth) - addToData(fourth, seventh) - addToData(third, eighth) - addToData(third, eighth) - addToData(second, fifth) - addToData(first, sixth) + fun createQuad(vertexPosition1: Vec3, vertexPosition2: Vec3, vertexPosition3: Vec3, vertexPosition4: Vec3, texturePosition1: Vec2, texturePosition2: Vec2, texturePosition3: Vec2, texturePosition4: Vec2) { + addToData(vertexPosition1, texturePosition2) + addToData(vertexPosition4, texturePosition3) + addToData(vertexPosition3, texturePosition4) + addToData(vertexPosition3, texturePosition4) + addToData(vertexPosition2, texturePosition1) + addToData(vertexPosition1, texturePosition2) } when (direction) { - Directions.DOWN -> createQuad(positionDownLeftFront, positionDownLeftBack, positionDownRightBack, positionDownRightFront) - Directions.UP -> createQuad(positionUpLeftFront, positionUpLeftBack, positionUpRightBack, positionUpRightFront) // ToDo + Directions.DOWN -> createQuad(positionDownLeftFront, positionDownLeftBack, positionDownRightBack, positionDownRightFront, face.texturLeftDown, face.texturLeftUp, face.texturRightUp, face.texturRightDown) + Directions.UP -> createQuad(positionUpLeftFront, positionUpLeftBack, positionUpRightBack, positionUpRightFront, face.texturLeftDown, face.texturLeftUp, face.texturRightUp, face.texturRightDown) Directions.NORTH -> createQuad(positionDownLeftFront, positionUpLeftFront, positionUpRightFront, positionDownRightFront, face.texturRightDown, face.texturRightUp, face.texturLeftUp, face.texturLeftDown) Directions.SOUTH -> createQuad(positionDownLeftBack, positionUpLeftBack, positionUpRightBack, positionDownRightBack, face.texturLeftDown, face.texturLeftUp, face.texturRightUp, face.texturRightDown) Directions.WEST -> createQuad(positionUpLeftBack, positionDownLeftBack, positionDownLeftFront, positionUpLeftFront, face.texturRightUp, face.texturRightDown, face.texturLeftDown, face.texturLeftUp) Directions.EAST -> createQuad(positionUpRightBack, positionDownRightBack, positionDownRightFront, positionUpRightFront, face.texturLeftUp, face.texturLeftDown, face.texturRightDown, face.texturRightUp) } - - return data } fun isCullFace(direction: Directions): Boolean { diff --git a/src/main/resources/assets/rendering/shader/chunk_vertex.glsl b/src/main/resources/assets/rendering/shader/chunk_vertex.glsl index e141170fc..b7074cba1 100644 --- a/src/main/resources/assets/rendering/shader/chunk_vertex.glsl +++ b/src/main/resources/assets/rendering/shader/chunk_vertex.glsl @@ -9,9 +9,9 @@ out vec3 passTextureCoordinates; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; -uniform vec3 chunkPosition; +uniform vec3 worldPosition; void main() { - gl_Position = projectionMatrix * viewMatrix * vec4(inPosition + vec3(chunkPosition.x * 16u, chunkPosition.y * 16u, chunkPosition.z * 16u), 1.0f); + gl_Position = projectionMatrix * viewMatrix * vec4(inPosition + vec3(worldPosition.x * 16u, worldPosition.y * 16u, worldPosition.z * 16u), 1.0f); passTextureCoordinates = vec3(textureIndex, textureLayer); }