Skip to content

Commit

Permalink
Refactor: made GpuState more convenient to work with
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Feb 12, 2024
1 parent 3037bf8 commit 254e63f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion game/src/main/scala/hexacraft/game/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GameScene(
private val crosshairShader = new CrosshairShader()
private val crosshairVAO: VAO = makeCrosshairVAO
private val crosshairRenderer: Renderer =
new Renderer(OpenGL.PrimitiveMode.Lines, GpuState.of(OpenGL.State.DepthTest -> false))
new Renderer(OpenGL.PrimitiveMode.Lines, GpuState.build(_.depthTest(false)))

private val worldInfo = net.worldProvider.getWorldInfo
private val world = World(net.worldProvider, worldInfo)
Expand Down
5 changes: 1 addition & 4 deletions game/src/main/scala/hexacraft/game/guiBlock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class GuiBlockRenderer(w: Int, h: Int, separation: Float = 0.2f)(blockTextureInd
for s <- 0 until 8 yield {
BlockRenderer(
GuiBlockVao.forSide(s),
GpuState.of(
OpenGL.State.DepthTest -> false,
OpenGL.State.Blend -> true
)
GpuState.build(_.blend(true).depthTest(false))
)
}

Expand Down
2 changes: 1 addition & 1 deletion game/src/main/scala/hexacraft/gui/comp/Component.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Component {
.finish(4)
private val rectRenderer = new Renderer(
OpenGL.PrimitiveMode.TriangleStrip,
GpuState.of(OpenGL.State.Blend -> true, OpenGL.State.DepthTest -> false)
GpuState.build(_.blend(true).depthTest(false))
)

val font: Font = Fonts.get("Verdana").get
Expand Down
17 changes: 12 additions & 5 deletions game/src/main/scala/hexacraft/renderer/GpuState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import hexacraft.infra.gpu.OpenGL
import scala.collection.mutable

object GpuState {
def of(states: (OpenGL.State, Boolean)*): GpuState = {
GpuState(
enabled = states.filter(_._2).map(_._1),
disabled = states.filter(!_._2).map(_._1)
)
def build(f: Builder => Builder): GpuState = f(new Builder(Map.empty)).build()

class Builder private[GpuState] (states: Map[OpenGL.State, Boolean]) {
def blend(enabled: Boolean): Builder = Builder(states + (OpenGL.State.Blend -> enabled))
def depthTest(enabled: Boolean): Builder = Builder(states + (OpenGL.State.DepthTest -> enabled))
def cullFace(enabled: Boolean): Builder = Builder(states + (OpenGL.State.CullFace -> enabled))
def scissorTest(enabled: Boolean): Builder = Builder(states + (OpenGL.State.ScissorTest -> enabled))

private[GpuState] def build(): GpuState = {
val (enabled, disabled) = states.partition(_._2)
GpuState(enabled.keys.toSeq, disabled.keys.toSeq)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hexacraft.world.render

import hexacraft.infra.gpu.OpenGL
import hexacraft.renderer.{GpuState, TextureArray}
import hexacraft.world.{BlocksInWorld, Camera, CylinderSize}
import hexacraft.world.chunk.LocalBlockState
Expand Down Expand Up @@ -41,18 +40,12 @@ class ChunkRenderHandler {
private val regularBlockHexagonHandler = new HexagonRenderHandler(
blockShader,
blockSideShader,
GpuState.of(
OpenGL.State.CullFace -> true,
OpenGL.State.Blend -> false
)
GpuState.build(_.blend(false).cullFace(true))
)
private val transmissiveBlockHexagonHandler = new HexagonRenderHandler(
blockShader,
blockSideShader,
GpuState.of(
OpenGL.State.Blend -> true,
OpenGL.State.CullFace -> false
)
GpuState.build(_.blend(true).cullFace(false))
)

def regularChunkBufferFragmentation: IndexedSeq[Float] = regularBlockHexagonHandler.fragmentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class WorldRenderer(

private val skyVao: VAO = SkyVao.create
private val skyRenderer =
new Renderer(OpenGL.PrimitiveMode.TriangleStrip, GpuState.of(OpenGL.State.DepthTest -> false))
new Renderer(OpenGL.PrimitiveMode.TriangleStrip, GpuState.build(_.depthTest(false)))

private val worldCombinerVao: VAO = WorldCombinerVao.create
private val worldCombinerRenderer =
new Renderer(OpenGL.PrimitiveMode.TriangleStrip, GpuState.of(OpenGL.State.DepthTest -> false))
new Renderer(OpenGL.PrimitiveMode.TriangleStrip, GpuState.build(_.depthTest(false)))

private val selectedBlockVao = SelectedBlockVao.create
private val selectedBlockRenderer = new InstancedRenderer(OpenGL.PrimitiveMode.LineStrip)
Expand Down

0 comments on commit 254e63f

Please sign in to comment.