mipmaps: fix some bugs

This commit is contained in:
Bixilon 2021-11-01 16:46:00 +01:00
parent fb540c5a53
commit c4d242228a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 96 additions and 23 deletions

View File

@ -33,8 +33,10 @@ import de.bixilon.minosoft.gui.rendering.block.renderable.BlockLikeRenderContext
import de.bixilon.minosoft.gui.rendering.input.camera.Frustum
import de.bixilon.minosoft.gui.rendering.modding.events.FrustumChangeEvent
import de.bixilon.minosoft.gui.rendering.modding.events.RenderingStateChangeEvent
import de.bixilon.minosoft.gui.rendering.system.base.RenderModes
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureManager
import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLShader
import de.bixilon.minosoft.gui.rendering.util.VecUtil.chunkPosition
import de.bixilon.minosoft.gui.rendering.util.VecUtil.getWorldOffset
import de.bixilon.minosoft.gui.rendering.util.VecUtil.of
@ -62,6 +64,7 @@ class WorldRenderer(
private val waterBlock = connection.registries.blockRegistry[ResourceLocation("minecraft:water")].nullCast<FluidBlock>()
private val chunkShader: Shader = renderWindow.renderSystem.createShader(ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "world"))
private val transparentShader: Shader = renderWindow.renderSystem.createShader(ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "world"))
private val lightMap = LightMap(connection)
val allChunkSections: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkSectionMeshCollection>> = synchronizedMapOf()
@ -118,8 +121,12 @@ class WorldRenderer(
}
}
if (meshCollection.transparentSectionArrayMesh!!.data.isEmpty) {
meshCollection.transparentSectionArrayMesh = null
if (meshCollection.translucentMesh!!.data.isEmpty) {
meshCollection.translucentMesh = null
}
if (meshCollection.transparentMesh!!.data.isEmpty) {
meshCollection.transparentMesh = null
}
return meshCollection
}
@ -180,11 +187,15 @@ class WorldRenderer(
override fun postInit() {
chunkShader.load()
(transparentShader as OpenGLShader).defines["TRANSPARENT"] = "" // ToDo
transparentShader.load()
lightMap.init()
renderWindow.textureManager.staticTextures.use(chunkShader)
renderWindow.textureManager.staticTextures.use(transparentShader)
renderWindow.textureManager.staticTextures.animator.use(chunkShader)
lightMap.use(chunkShader)
lightMap.use(transparentShader)
for (blockState in allBlocks!!) {
for (model in blockState.renderers) {
@ -205,18 +216,28 @@ class WorldRenderer(
for (map in lastVisibleChunks.values) {
for (mesh in map.values) {
mesh.opaqueSectionArrayMesh.draw()
mesh.opaqueMesh.draw()
}
}
}
override fun postDraw() {
renderWindow.renderSystem.reset(depthMask = false)
chunkShader.use()
renderWindow.renderSystem.reset()
transparentShader.use()
for (map in lastVisibleChunks.values) {
for (mesh in map.values) {
mesh.transparentSectionArrayMesh?.draw()
mesh.transparentMesh?.draw()
}
}
chunkShader.use()
renderWindow.renderSystem.renderMode(RenderModes.TRANSLUCENT)
for (map in lastVisibleChunks.values) {
for (mesh in map.values) {
mesh.translucentMesh?.draw()
}
}
}
@ -329,23 +350,33 @@ class WorldRenderer(
val sectionMap = allChunkSections.getOrPut(chunkPosition) { synchronizedMapOf() }
sectionMap[index]?.let {
it.opaqueSectionArrayMesh.unload()
it.opaqueMesh.unload()
meshes--
triangles -= it.opaqueSectionArrayMesh.vertices
triangles -= it.opaqueMesh.vertices
it.transparentSectionArrayMesh?.let {
it.translucentMesh?.let {
it.unload()
meshes--
triangles -= it.vertices
}
it.transparentMesh?.let {
it.unload()
meshes--
triangles -= it.vertices
}
}
meshCollection.opaqueSectionArrayMesh.let {
meshCollection.opaqueMesh.let {
it.load()
meshes++
triangles += it.vertices
}
meshCollection.transparentSectionArrayMesh?.let {
meshCollection.translucentMesh?.let {
it.load()
meshes++
triangles += it.vertices
}
meshCollection.transparentMesh?.let {
it.load()
meshes++
triangles += it.vertices
@ -410,12 +441,17 @@ class WorldRenderer(
private fun unloadMeshes(meshes: Collection<ChunkSectionMeshCollection>) {
renderWindow.assertOnRenderThread()
for (meshCollection in meshes) {
meshCollection.opaqueSectionArrayMesh.let {
meshCollection.opaqueMesh.let {
it.unload()
this.meshes--
triangles -= it.vertices
}
meshCollection.transparentSectionArrayMesh?.let {
meshCollection.translucentMesh?.let {
it.unload()
this.meshes--
triangles -= it.vertices
}
meshCollection.transparentMesh?.let {
it.unload()
this.meshes--
triangles -= it.vertices

View File

@ -17,8 +17,9 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
class ChunkSectionMeshCollection(
renderWindow: RenderWindow,
val opaqueSectionArrayMesh: ChunkSectionArrayMesh = ChunkSectionArrayMesh(renderWindow),
var transparentSectionArrayMesh: ChunkSectionArrayMesh? = ChunkSectionArrayMesh(renderWindow),
val opaqueMesh: ChunkSectionArrayMesh = ChunkSectionArrayMesh(renderWindow),
var translucentMesh: ChunkSectionArrayMesh? = ChunkSectionArrayMesh(renderWindow),
var transparentMesh: ChunkSectionArrayMesh? = ChunkSectionArrayMesh(renderWindow),
) {
var lowestBlockHeight = 0
var highestBlockHeight = 0

View File

@ -168,11 +168,11 @@ class ElementRenderer(
val DRAW_OFFSET = Vec3(+0.5f, +0.5f, +0.5f)
fun getMesh(meshCollection: ChunkSectionMeshCollection, textureTransparencies: TextureTransparencies): ChunkSectionArrayMesh {
return if (textureTransparencies == TextureTransparencies.TRANSLUCENT) {
meshCollection.transparentSectionArrayMesh!!
} else {
meshCollection.opaqueSectionArrayMesh
fun getMesh(meshCollection: ChunkSectionMeshCollection, transparency: TextureTransparencies): ChunkSectionArrayMesh {
return when (transparency) {
TextureTransparencies.OPAQUE -> meshCollection.opaqueMesh
TextureTransparencies.TRANSPARENT -> meshCollection.transparentMesh!!
TextureTransparencies.TRANSLUCENT -> meshCollection.translucentMesh!!
}
}
}

View File

@ -0,0 +1,20 @@
/*
* Minosoft
* Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.base
enum class RenderModes {
DEFAULT,
TRANSLUCENT,
;
}

View File

@ -89,4 +89,14 @@ interface RenderSystem {
fun createTextureManager(): TextureManager
fun clear(vararg buffers: IntegratedBufferTypes)
fun renderMode(mode: RenderModes) {
when (mode) {
RenderModes.TRANSLUCENT -> {
depthMask = false
setBlendFunc(BlendingFunctions.SOURCE_ALPHA, BlendingFunctions.ONE_MINUS_SOURCE_ALPHA, BlendingFunctions.ONE, BlendingFunctions.ONE_MINUS_SOURCE_ALPHA)
}
}
}
}

View File

@ -23,5 +23,5 @@ interface SpriteAnimator {
fun draw()
fun use(shader: Shader, bufferName: String = "uAnimationBuffer")
fun use(shader: Shader, bufferName: String = "uSpriteBuffer")
}

View File

@ -189,7 +189,7 @@ class OpenGLTextureArray(
companion object {
val TEXTURE_RESOLUTION_ID_MAP = arrayOf(16, 32, 64, 128, 256, 512, 1024) // A 12x12 texture will be saved in texture id 0 (in 0 are only 16x16 textures). Animated textures get split
val TEXTURE_RESOLUTION_ID_MAP = intArrayOf(16, 32, 64, 128, 256, 512, 1024) // A 12x12 texture will be saved in texture id 0 (in 0 are only 16x16 textures). Animated textures get split
const val TEXTURE_MAX_RESOLUTION = 1024
const val MAX_MIPMAP_LEVELS = 5
}

View File

@ -45,6 +45,12 @@ void work() {
}
outColor = mix(firstTexelColor, secondTexelColor, finInterpolation) * finTintColor;
#ifndef TRANSPARENT
if (outColor.a < 0.5){
discard;
}
#endif
}
#include "minosoft:postprocessing/fragment"
#include "minosoft:postprocessing/fragment"