Skip to content

Commit

Permalink
Refactor: Moved some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Jan 13, 2024
1 parent 45c694d commit d8aa723
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
3 changes: 1 addition & 2 deletions game/src/main/scala/hexacraft/game/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class GameScene(
)
)

private val worldRenderer: WorldRenderer =
new WorldRenderer(world, world.requestRenderUpdate, blockSpecRegistry, initialWindowSize.physicalSize)
private val worldRenderer: WorldRenderer = new WorldRenderer(world, blockSpecRegistry, initialWindowSize.physicalSize)
world.trackEvents(worldRenderer.onWorldEvent _)

// worldRenderer.addPlayer(otherPlayer)
Expand Down
4 changes: 3 additions & 1 deletion game/src/main/scala/hexacraft/world/World.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class World(worldProvider: WorldProvider, worldInfo: WorldInfo) extends BlockRep
savedChunkModCounts(ch.coords) = ch.modCount
col.updateHeightmapAfterChunkUpdate(ch)

ch.initLightingIfNeeded(lightPropagator)

requestRenderUpdate(ch.coords)
requestRenderUpdateForNeighborChunks(ch.coords)

Expand Down Expand Up @@ -246,7 +248,7 @@ class World(worldProvider: WorldProvider, worldInfo: WorldInfo) extends BlockRep
ch <- col.allChunks
do requestRenderUpdate(ch.coords)

def requestRenderUpdate(chunkCoords: ChunkRelWorld): Unit =
private def requestRenderUpdate(chunkCoords: ChunkRelWorld): Unit =
dispatcher.notify(World.Event.ChunkNeedsRenderUpdate(chunkCoords))

def unload(): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package hexacraft.world.render

import hexacraft.util.{TickableTimer, UniquePQ}
import hexacraft.util.UniquePQ
import hexacraft.world.{Camera, CylinderSize, PosAndDir}
import hexacraft.world.coord.{BlockCoords, BlockRelWorld, ChunkRelWorld, CylCoords}

Expand All @@ -9,14 +9,11 @@ class ChunkRenderUpdateQueue(using CylinderSize):

private val queue: UniquePQ[ChunkRelWorld] = new UniquePQ(makeChunkToUpdatePriority, Ordering.by(-_))

private val reorderingTimer: TickableTimer = TickableTimer(5)

def reorderAndFilter(camera: Camera, renderDistance: Double): Unit =
origin.setPosAndDirFrom(camera.view)

if reorderingTimer.tick() then
val rDistSq = (renderDistance * 16) * (renderDistance * 16)
queue.reprioritizeAndFilter(_._1 <= rDistSq)
val rDistSq = (renderDistance * 16) * (renderDistance * 16)
queue.reprioritizeAndFilter(_._1 <= rDistSq)

def length: Int = queue.size

Expand Down
19 changes: 6 additions & 13 deletions game/src/main/scala/hexacraft/world/render/WorldRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package hexacraft.world.render

import hexacraft.infra.gpu.OpenGL
import hexacraft.renderer.{GpuState, InstancedRenderer, Renderer, VAO}
import hexacraft.world.{BlocksInWorld, Camera, CylinderSize, LightPropagator, World}
import hexacraft.util.TickableTimer
import hexacraft.world.{BlocksInWorld, Camera, CylinderSize, World}
import hexacraft.world.block.{BlockSpecRegistry, BlockState}
import hexacraft.world.chunk.Chunk
import hexacraft.world.coord.{BlockRelWorld, ChunkRelWorld}
Expand All @@ -13,12 +14,7 @@ import org.joml.{Vector2ic, Vector3f}
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

class WorldRenderer(
world: BlocksInWorld,
requestRenderUpdate: ChunkRelWorld => Unit,
blockSpecs: BlockSpecRegistry,
initialFramebufferSize: Vector2ic
)(using
class WorldRenderer(world: BlocksInWorld, blockSpecs: BlockSpecRegistry, initialFrameBufferSize: Vector2ic)(using
CylinderSize
):
private val skyShader = new SkyShader()
Expand All @@ -29,8 +25,6 @@ class WorldRenderer(

private val chunkHandler: ChunkRenderHandler = new ChunkRenderHandler

private val lightPropagator = new LightPropagator(world, requestRenderUpdate)

private val skyVao: VAO = SkyVao.create
private val skyRenderer =
new Renderer(OpenGL.PrimitiveMode.TriangleStrip, GpuState.of(OpenGL.State.DepthTest -> false))
Expand All @@ -42,14 +36,15 @@ class WorldRenderer(
private val selectedBlockVao = SelectedBlockVao.create
private val selectedBlockRenderer = new InstancedRenderer(OpenGL.PrimitiveMode.LineStrip)

private var mainFrameBuffer = MainFrameBuffer.fromSize(initialFramebufferSize.x, initialFramebufferSize.y)
private var mainFrameBuffer = MainFrameBuffer.fromSize(initialFrameBufferSize.x, initialFrameBufferSize.y)

private var currentlySelectedBlockAndSide: Option[(BlockState, BlockRelWorld, Option[Int])] = None

private val chunksToRender: mutable.Set[ChunkRelWorld] = mutable.HashSet.empty
private val entityRenderers = for s <- 0 until 8 yield BlockRenderer(EntityPartVao.forSide(s), GpuState())

private val chunkRenderUpdateQueue: ChunkRenderUpdateQueue = new ChunkRenderUpdateQueue
private val chunkRenderUpdateQueueReorderingTimer: TickableTimer = TickableTimer(5) // only reorder every 5 ticks

private val players = ArrayBuffer.empty[Entity]
def addPlayer(player: Entity): Unit = players += player
Expand All @@ -59,8 +54,6 @@ class WorldRenderer(
def transmissiveChunkBufferFragmentation: IndexedSeq[Float] = chunkHandler.transmissiveChunkBufferFragmentation

private def updateChunkData(ch: Chunk): Unit =
ch.initLightingIfNeeded(lightPropagator)

val (opaqueBlocks, transmissiveBlocks) =
if ch.blocks.isEmpty
then (ChunkRenderDataFactory.empty, ChunkRenderDataFactory.empty)
Expand All @@ -73,7 +66,7 @@ class WorldRenderer(
chunkHandler.setChunkRenderData(ch.coords, opaqueBlocks, transmissiveBlocks)

def tick(camera: Camera, renderDistance: Double): Unit =
chunkRenderUpdateQueue.reorderAndFilter(camera, renderDistance)
if chunkRenderUpdateQueueReorderingTimer.tick() then chunkRenderUpdateQueue.reorderAndFilter(camera, renderDistance)

val numUpdatesToPerform = if chunkRenderUpdateQueue.length > 10 then 4 else 1

Expand Down

0 comments on commit d8aa723

Please sign in to comment.