KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientWorld.kt

217 lines
6.2 KiB
Kotlin

package ru.dbotthepony.kstarbound.client
import ru.dbotthepony.kstarbound.client.render.LayeredRenderer
import ru.dbotthepony.kstarbound.math.encasingChunkPosAABB
import ru.dbotthepony.kstarbound.world.*
import ru.dbotthepony.kstarbound.world.entities.Entity
import ru.dbotthepony.kvector.util2d.AABB
import ru.dbotthepony.kvector.vector.Vector2i
class ClientWorld(
val client: StarboundClient,
seed: Long,
size: Vector2i? = null,
loopX: Boolean = false,
loopY: Boolean = false
) : World<ClientWorld, ClientChunk>(seed, size, loopX, loopY) {
init {
physics.debugDraw = client.gl.box2dRenderer
}
override fun chunkFactory(pos: ChunkPos): ClientChunk {
return ClientChunk(
world = this,
pos = pos,
)
}
fun addLayers(
size: AABB,
layers: LayeredRenderer
) {
for (chunk in collectPositionAware(size.encasingChunkPosAABB())) {
val renderer = chunk.second.Renderer(chunk.first)
renderer.addLayers(layers)
chunk.second.bake()
//client.lightRenderer.addShadowGeometry(renderer)
}
/*
for ((lightPosition, color) in listOf(
(client.screenToWorld(client.mouseCoordinatesF)) to Color.RED,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(0.1f)) to Color.GREEN,
(client.screenToWorld(client.mouseCoordinatesF) + Vector2f(-0.1f)) to Color.BLUE,
)) {
if (isScreenspaceRender) {
val (x, y) = client.worldToScreen(lightPosition.x - 64f, lightPosition.y - 64f)
val (x2, y2) = client.worldToScreen(lightPosition.x + 64f, lightPosition.y + 64f)
client.pushScissorRect(x, client.viewportHeight - y, x2 - x, y - y2)
}
client.lightRenderer.renderSoftLight(lightPosition, color, radius = 64f, innerRadius = 2f)
if (isScreenspaceRender) {
client.popScissorRect()
}
}
val old = client.gl.blendFunc
client.gl.blendFunc = BlendFunc.MULTIPLY_BY_SRC
client.gl.activeTexture = 0
client.gl.texture2D = client.lightRenderer.outputTexture
client.gl.programs.viewTextureQuad.run()
client.gl.blendFunc = old
*/
val pos = client.screenToWorld(client.mouseCoordinatesF)
/*val lightsize = 16
val lightmap = floodLight(
Vector2i(pos.x.roundToInt(), pos.y.roundToInt()), lightsize
)
client.gl.quadWireframe {
for (column in 0 until lightmap.columns) {
for (row in 0 until lightmap.rows) {
if (lightmap[column, row] > 0) {
it.quad(pos.x.roundToInt() + column.toFloat() - lightsize, pos.y.roundToInt() + row.toFloat() - lightsize, pos.x.roundToInt() + column + 1f - lightsize, pos.y.roundToInt() + row + 1f - lightsize)
}
}
}
}*/
/*
val rayFan = ArrayList<Vector2d>()
for (i in 0 .. 359) {
rayFan.add(Vector2d(cos(i / 180.0 * PI), sin(i / 180.0 * PI)))
}
for (ray in rayFan) {
val trace = castRayNaive(pos, ray, 16.0)
client.gl.quadWireframe {
for ((tpos, tile) in trace.traversedTiles) {
if (tile.material != null)
it.quad(
tpos.x.toFloat(),
tpos.y.toFloat(),
tpos.x + 1f,
tpos.y + 1f
)
}
}
}
*/
//rayLightCircleNaive(pos, 48.0, falloffByTravel = 1.0, falloffByTile = 3.0)
/*
val result = rayLightCircleNaive(pos, 48.0, falloffByTravel = 1.0, falloffByTile = 3.0)
val result2 = rayLightCircleNaive(pos + Vector2d(-8.0), 24.0, falloffByTravel = 1.0, falloffByTile = 3.0)
val frame = GLFrameBuffer(client.gl)
frame.attachTexture(client.viewportWidth, client.viewportHeight)
frame.bind()
client.gl.clearColor = Color.BLACK
glClear(GL_COLOR_BUFFER_BIT)
client.gl.blendFunc = BlendFunc.ADDITIVE*/
/*client.gl.quadColor {
for (row in 0 until result.rows) {
for (column in 0 until result.columns) {
if (result[column, row] > 0.05) {
val color = result[column, row].toFloat() * 1.5f
it.quad(
pos.x.roundToInt() - result.rows.toFloat() / 2f + row.toFloat(),
pos.y.roundToInt() - result.columns.toFloat() / 2f + column.toFloat(),
pos.x.roundToInt() - result.rows.toFloat() / 2f + row + 1f,
pos.y.roundToInt() - result.columns.toFloat() / 2f + column + 1f
) { a, b -> a.pushVec4f(color, color, color, 1f) }
}
}
}
}
client.gl.quadColor {
for (row in 0 until result2.rows) {
for (column in 0 until result2.columns) {
if (result2[column, row] > 0.05) {
val color = result2[column, row].toFloat() * 1.5f
it.quad(
pos.x.roundToInt() - 8f - result2.rows.toFloat() / 2f + row.toFloat(),
pos.y.roundToInt() - result2.columns.toFloat() / 2f + column.toFloat(),
pos.x.roundToInt() - 8f - result2.rows.toFloat() / 2f + row + 1f,
pos.y.roundToInt() - result2.columns.toFloat() / 2f + column + 1f
) { a, b -> a.pushVec4f(color, 0f, 0f, 1f) }
}
}
}
}*/
/*val lightTextureWidth = (client.viewportWidth / PIXELS_IN_STARBOUND_UNIT).roundToInt()
val lightTextureHeight = (client.viewportHeight / PIXELS_IN_STARBOUND_UNIT).roundToInt()
val textureBuffer = ByteBuffer.allocateDirect(lightTextureWidth * lightTextureHeight * 3)
textureBuffer.order(ByteOrder.LITTLE_ENDIAN)
for (x in 0 until result.columns.coerceAtMost(lightTextureWidth)) {
for (y in 0 until result.rows.coerceAtMost(lightTextureHeight)) {
textureBuffer.position(x * 3 + y * lightTextureWidth * 3)
if (result[x, y] > 0.05) {
val color = result[x, y].toFloat() * 1.5f
textureBuffer.put((color * 255).toInt().coerceAtMost(255).toByte())
textureBuffer.put((color * 255).toInt().coerceAtMost(255).toByte())
textureBuffer.put((color * 255).toInt().coerceAtMost(255).toByte())
}
}
}*/
//frame.unbind()
// val texture = GLTexture2D(client.gl)
// textureBuffer.position(0)
// texture.upload(GL_RGB, lightTextureWidth, lightTextureHeight, GL_RGB, GL_UNSIGNED_BYTE, textureBuffer)
// texture.textureMinFilter = GL_LINEAR
// texture.textureMagFilter = GL_LINEAR
// client.gl.blendFunc = BlendFunc.MULTIPLY_BY_SRC
// client.gl.activeTexture = 0
// client.gl.texture2D = texture
// client.gl.programs.viewTextureQuad.run()
// client.gl.blendFunc = old
//frame.close()
//texture.close()
/*for (renderer in determineRenderers) {
renderer.renderDebug()
}*/
}
override fun thinkInner(delta: Double) {
val copy = arrayOfNulls<Entity>(entities.size)
var i = 0
for (ent in entities) {
copy[i++] = ent
}
for (ent in copy) {
ent!!.think(delta)
}
}
}