rendering: use correct sky color

This commit is contained in:
Bixilon 2021-05-12 00:35:18 +02:00
parent e6f35564b5
commit bb9d03e58b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 50 additions and 27 deletions

View File

@ -23,6 +23,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent
import de.bixilon.minosoft.gui.rendering.modding.events.FrustumChangeEvent import de.bixilon.minosoft.gui.rendering.modding.events.FrustumChangeEvent
import de.bixilon.minosoft.gui.rendering.modding.events.ScreenResizeEvent import de.bixilon.minosoft.gui.rendering.modding.events.ScreenResizeEvent
import de.bixilon.minosoft.gui.rendering.sky.SkyRenderer
import de.bixilon.minosoft.gui.rendering.util.VecUtil import de.bixilon.minosoft.gui.rendering.util.VecUtil
import de.bixilon.minosoft.gui.rendering.util.VecUtil.blockPosition import de.bixilon.minosoft.gui.rendering.util.VecUtil.blockPosition
import de.bixilon.minosoft.gui.rendering.util.VecUtil.chunkPosition import de.bixilon.minosoft.gui.rendering.util.VecUtil.chunkPosition
@ -232,18 +233,19 @@ class Camera(
sectionHeight = blockPosition.sectionHeight sectionHeight = blockPosition.sectionHeight
inChunkSectionPosition = blockPosition.inChunkSectionPosition inChunkSectionPosition = blockPosition.inChunkSectionPosition
// recalculate sky color for current biome // recalculate sky color for current biome
renderWindow.setSkyColor(connection.world.getBiome(blockPosition)?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR) val skyRenderer = renderWindow[SkyRenderer] ?: return
skyRenderer.setSkyColor(connection.world.getBiome(blockPosition)?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR)
frustum.recalculate() frustum.recalculate()
connection.fireEvent(FrustumChangeEvent(renderWindow, frustum)) connection.fireEvent(FrustumChangeEvent(renderWindow, frustum))
connection.world.dimension?.hasSkyLight?.let { connection.world.dimension?.hasSkyLight?.let {
if (it) { if (it) {
renderWindow.setSkyColor(currentBiome?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR) skyRenderer.setSkyColor(currentBiome?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR)
} else { } else {
renderWindow.setSkyColor(RenderConstants.BLACK_COLOR) skyRenderer.setSkyColor(RenderConstants.BLACK_COLOR)
} }
} ?: renderWindow.setSkyColor(RenderConstants.DEFAULT_SKY_COLOR) } ?: skyRenderer.setSkyColor(RenderConstants.DEFAULT_SKY_COLOR)
} }
private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4 { private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4 {

View File

@ -16,8 +16,6 @@ package de.bixilon.minosoft.gui.rendering
import de.bixilon.minosoft.config.StaticConfiguration import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor
import de.bixilon.minosoft.gui.input.key.RenderWindowInputHandler import de.bixilon.minosoft.gui.input.key.RenderWindowInputHandler
import de.bixilon.minosoft.gui.rendering.chunk.WorldRenderer import de.bixilon.minosoft.gui.rendering.chunk.WorldRenderer
import de.bixilon.minosoft.gui.rendering.font.Font import de.bixilon.minosoft.gui.rendering.font.Font
@ -179,7 +177,7 @@ class RenderWindow(
// Make the window visible // Make the window visible
GL.createCapabilities() GL.createCapabilities()
setSkyColor("#fffe7a".asColor()) glClearColor(1.0f, 1.0f, 0.0f, 1.0f)
Log.log(LogMessageType.RENDERING_LOADING) { "Enabling all open gl features (${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING) { "Enabling all open gl features (${stopwatch.labTime()})..." }
glEnable(GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)
@ -382,14 +380,9 @@ class RenderWindow(
connection.fireEvent(RenderingStateChangeEvent(connection, previousState, renderingState)) connection.fireEvent(RenderingStateChangeEvent(connection, previousState, renderingState))
} }
fun registerRenderer(renderBuilder: RenderBuilder) { fun registerRenderer(rendererBuilder: RendererBuilder<*>) {
val renderer = renderBuilder.build(connection, this) val renderer = rendererBuilder.build(connection, this)
rendererMap[renderBuilder.RESOURCE_LOCATION] = renderer rendererMap[rendererBuilder.RESOURCE_LOCATION] = renderer
}
@Deprecated(message = "Will be replaced with SkyRenderer")
fun setSkyColor(color: RGBColor) {
glClearColor(color.floatRed, color.floatGreen, color.floatBlue, 1.0f)
} }
fun sendDebugMessage(message: String) { fun sendDebugMessage(message: String) {
@ -403,4 +396,8 @@ class RenderWindow(
fun assertOnRenderThread() { fun assertOnRenderThread() {
check(Thread.currentThread() == renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" } check(Thread.currentThread() == renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" }
} }
operator fun <T : Renderer> get(renderer: RendererBuilder<T>): T? {
return rendererMap[renderer.RESOURCE_LOCATION] as T
}
} }

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.gui.rendering
import de.bixilon.minosoft.data.mappings.CompanionResourceLocation import de.bixilon.minosoft.data.mappings.CompanionResourceLocation
import de.bixilon.minosoft.protocol.network.connection.PlayConnection import de.bixilon.minosoft.protocol.network.connection.PlayConnection
interface RenderBuilder : CompanionResourceLocation { interface RendererBuilder<T : Renderer> : CompanionResourceLocation {
fun build(connection: PlayConnection, renderWindow: RenderWindow): Renderer fun build(connection: PlayConnection, renderWindow: RenderWindow): T
} }

View File

@ -419,7 +419,7 @@ class WorldRenderer(
return this + upOrDown.directionVector.y return this + upOrDown.directionVector.y
} }
companion object : RenderBuilder { companion object : RendererBuilder<WorldRenderer> {
override val RESOURCE_LOCATION = ResourceLocation("minosoft:world_renderer") override val RESOURCE_LOCATION = ResourceLocation("minosoft:world_renderer")
override fun build(connection: PlayConnection, renderWindow: RenderWindow): WorldRenderer { override fun build(connection: PlayConnection, renderWindow: RenderWindow): WorldRenderer {

View File

@ -16,10 +16,10 @@ package de.bixilon.minosoft.gui.rendering.hud
import de.bixilon.minosoft.Minosoft import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames import de.bixilon.minosoft.config.config.game.controls.KeyBindingsNames
import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.gui.rendering.RenderBuilder
import de.bixilon.minosoft.gui.rendering.RenderConstants import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.Renderer import de.bixilon.minosoft.gui.rendering.Renderer
import de.bixilon.minosoft.gui.rendering.RendererBuilder
import de.bixilon.minosoft.gui.rendering.hud.atlas.HUDAtlasElement import de.bixilon.minosoft.gui.rendering.hud.atlas.HUDAtlasElement
import de.bixilon.minosoft.gui.rendering.hud.elements.other.CrosshairHUDElement import de.bixilon.minosoft.gui.rendering.hud.elements.other.CrosshairHUDElement
import de.bixilon.minosoft.gui.rendering.hud.nodes.HUDElement import de.bixilon.minosoft.gui.rendering.hud.nodes.HUDElement
@ -214,7 +214,7 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
return Vec2i(x, y) return Vec2i(x, y)
} }
companion object : RenderBuilder { companion object : RendererBuilder<HUDRenderer> {
override val RESOURCE_LOCATION = ResourceLocation("minosoft:hud_renderer") override val RESOURCE_LOCATION = ResourceLocation("minosoft:hud_renderer")
override fun build(connection: PlayConnection, renderWindow: RenderWindow): HUDRenderer { override fun build(connection: PlayConnection, renderWindow: RenderWindow): HUDRenderer {

View File

@ -17,6 +17,7 @@ import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.assets.AssetsManager import de.bixilon.minosoft.data.assets.AssetsManager
import de.bixilon.minosoft.data.commands.CommandStringReader import de.bixilon.minosoft.data.commands.CommandStringReader
import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.Rendering import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil
@ -125,6 +126,10 @@ class Shader(
} }
} }
fun setRGBColor(uniformName: String, color: RGBColor) {
setVec4(uniformName, Vec4(color.floatRed, color.floatGreen, color.floatBlue, color.alpha))
}
fun setUniform(uniformName: String, data: Any?) { fun setUniform(uniformName: String, data: Any?) {
if (data == null) { if (data == null) {
return return

View File

@ -14,10 +14,12 @@
package de.bixilon.minosoft.gui.rendering.sky package de.bixilon.minosoft.gui.rendering.sky
import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.data.mappings.ResourceLocation
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderBuilder import de.bixilon.minosoft.gui.rendering.RenderConstants
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.Renderer import de.bixilon.minosoft.gui.rendering.Renderer
import de.bixilon.minosoft.gui.rendering.RendererBuilder
import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent import de.bixilon.minosoft.gui.rendering.modding.events.CameraMatrixChangeEvent
import de.bixilon.minosoft.gui.rendering.shader.Shader import de.bixilon.minosoft.gui.rendering.shader.Shader
import de.bixilon.minosoft.gui.rendering.textures.Texture import de.bixilon.minosoft.gui.rendering.textures.Texture
@ -43,12 +45,11 @@ class SkyRenderer(
resourceLocation = ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "sky/sun"), resourceLocation = ResourceLocation(ProtocolDefinition.MINOSOFT_NAMESPACE, "sky/sun"),
) )
private val skyboxMesh = SkyboxMesh() private val skyboxMesh = SkyboxMesh()
private var skySunMesh = SkySunMesh() private var skySunMesh = SkySunMesh()
private var sunTexture = Texture(SUN_TEXTURE_RESOURCE_LOCATION) private var sunTexture = Texture(SUN_TEXTURE_RESOURCE_LOCATION)
private var recalculateSunNextFrame: Boolean = true private var recalculateSunNextFrame: Boolean = true
private var bottomColor = ChatColors.BLACK
private var topColor = RenderConstants.DEFAULT_SKY_COLOR
override fun init() { override fun init() {
@ -110,6 +111,21 @@ class SkyRenderer(
skySunMesh.draw() skySunMesh.draw()
} }
fun setSkyColor(color: RGBColor) {
topColor = color
bottomColor = RGBColor(color.red * 8 / 9, color.green * 8 / 9, color.blue * 8 / 9)
renderWindow.renderQueue.add {
updateSkyColor()
}
}
private fun updateSkyColor() {
skyboxShader.use()
skyboxShader.setRGBColor("bottomColor", bottomColor)
skyboxShader.setRGBColor("topColor", topColor)
}
private fun drawSkybox() { private fun drawSkybox() {
skyboxShader.use() skyboxShader.use()
skyboxMesh.draw() skyboxMesh.draw()
@ -124,7 +140,7 @@ class SkyRenderer(
glDepthFunc(GL_LESS) glDepthFunc(GL_LESS)
} }
companion object : RenderBuilder { companion object : RendererBuilder<SkyRenderer> {
override val RESOURCE_LOCATION = ResourceLocation("minosoft:sky") override val RESOURCE_LOCATION = ResourceLocation("minosoft:sky")
private val SUN_TEXTURE_RESOURCE_LOCATION = ResourceLocation("minecraft:textures/environment/sun.png") private val SUN_TEXTURE_RESOURCE_LOCATION = ResourceLocation("minecraft:textures/environment/sun.png")

View File

@ -18,12 +18,15 @@ out vec4 color;
uniform mat4 skyViewProjectionMatrix; uniform mat4 skyViewProjectionMatrix;
uniform vec4 bottomColor;
uniform vec4 topColor;
void main() { void main() {
gl_Position = (skyViewProjectionMatrix * vec4(inPosition, 1.0)).xyww; gl_Position = (skyViewProjectionMatrix * vec4(inPosition, 1.0)).xyww;
if (inPosition.y < 0.5f) { if (inPosition.y < 0.5f) {
color = vec4(0.0f, 1.0f, 1.0f, 1.0f); color = bottomColor;
} else { } else {
color = vec4(1.0f, 0.0f, 1.0f, 1.0f); color = topColor;
} }
} }