render system: abstract polygon mode

This commit is contained in:
Bixilon 2021-06-20 14:03:04 +02:00
parent 2c900298aa
commit 0576f1920f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 94 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import de.bixilon.minosoft.gui.rendering.modding.events.*
import de.bixilon.minosoft.gui.rendering.particle.ParticleRenderer
import de.bixilon.minosoft.gui.rendering.shader.Shader
import de.bixilon.minosoft.gui.rendering.sky.SkyRenderer
import de.bixilon.minosoft.gui.rendering.system.base.PolygonModes
import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
import de.bixilon.minosoft.gui.rendering.system.opengl.OpenGLRenderSystem
import de.bixilon.minosoft.gui.rendering.system.window.BaseWindow
@ -206,12 +207,9 @@ class RenderWindow(
private fun registerGlobalKeyCombinations() {
inputHandler.registerKeyCallback(KeyBindingsNames.DEBUG_POLYGON) {
glPolygonMode(GL_FRONT_AND_BACK, if (it) {
GL_LINE
} else {
GL_FILL
})
sendDebugMessage("Toggled polygon mode!")
val nextMode = it.decide(PolygonModes.LINE, PolygonModes.FILL)
renderSystem.polygonMode = nextMode
sendDebugMessage("Set polygon to: $nextMode")
}
inputHandler.registerKeyCallback(KeyBindingsNames.QUIT_RENDERING) { window.close() }

View File

@ -0,0 +1,27 @@
/*
* 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 FaceTypes {
FRONT_LEFT,
FRONT_RIGHT,
BACK_LEFT,
BACK_RIGHT,
FRONT,
BACK,
LEFT,
RIGHT,
FRONT_AND_BACK,
;
}

View File

@ -0,0 +1,21 @@
/*
* 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 PolygonModes {
FILL,
LINE,
POINT,
;
}

View File

@ -49,6 +49,7 @@ interface RenderSystem {
var depth: DepthFunctions
var depthMask: Boolean
fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer
var polygonMode: PolygonModes
fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer
}

View File

@ -113,6 +113,15 @@ class OpenGLRenderSystem(
field = value
}
override var polygonMode: PolygonModes = PolygonModes.FILL
set(value) {
if (field == value) {
return
}
glPolygonMode(FaceTypes.FRONT_AND_BACK.gl, value.gl)
field = value
}
override fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer {
val buffer: ByteBuffer = BufferUtils.createByteBuffer((end.x - start.x) * (end.y - start.y) * type.bytes)
glReadPixels(start.x, start.y, end.x, end.y, type.gl, GL_UNSIGNED_BYTE, buffer)
@ -178,5 +187,31 @@ class OpenGLRenderSystem(
else -> throw IllegalArgumentException("OpenGL does not support pixel type: $this")
}
}
private val PolygonModes.gl: Int
get() {
return when (this) {
PolygonModes.FILL -> GL_FILL
PolygonModes.LINE -> GL_LINE
PolygonModes.POINT -> GL_POINT
else -> throw IllegalArgumentException("OpenGL does not support polygon mode: $this")
}
}
private val FaceTypes.gl: Int
get() {
return when (this) {
FaceTypes.FRONT_LEFT -> GL_FRONT_LEFT
FaceTypes.FRONT_RIGHT -> GL_FRONT_RIGHT
FaceTypes.BACK_LEFT -> GL_BACK_LEFT
FaceTypes.BACK_RIGHT -> GL_BACK_RIGHT
FaceTypes.FRONT -> GL_FRONT
FaceTypes.BACK -> GL_BACK
FaceTypes.LEFT -> GL_LEFT
FaceTypes.RIGHT -> GL_RIGHT
FaceTypes.FRONT_AND_BACK -> GL_FRONT_AND_BACK
else -> throw IllegalArgumentException("OpenGL does not face type: $this")
}
}
}
}

View File

@ -18,7 +18,6 @@ import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.PixelTypes
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.logging.Log
import glm_.vec2.Vec2i
import java.awt.image.BufferedImage
import java.io.File
@ -36,6 +35,10 @@ class ScreenshotTaker(
var i = 1
while (File(path).exists()) {
path = "${basePath}_${i++}.png"
if (i > 10) {
screenshotFail(StackOverflowError())
return
}
}
val width = renderWindow.window.size.x
@ -63,7 +66,6 @@ class ScreenshotTaker(
val message = "§aScreenshot saved to §f${file.name}"
renderWindow.sendDebugMessage(message)
Log.game(message)
} catch (exception: Exception) {
screenshotFail(exception)
}
@ -73,7 +75,7 @@ class ScreenshotTaker(
}
}
private fun screenshotFail(exception: Exception?) {
private fun screenshotFail(exception: Throwable?) {
exception?.printStackTrace()
renderWindow.sendDebugMessage("§cFailed to make a screenshot!")
}