129 lines
3.9 KiB
Kotlin
129 lines
3.9 KiB
Kotlin
package ru.dbotthepony.kstarbound.client.render
|
|
|
|
import org.lwjgl.opengl.GL46.*
|
|
import ru.dbotthepony.kbox2d.api.IDebugDraw
|
|
import ru.dbotthepony.kbox2d.api.Transform
|
|
import ru.dbotthepony.kstarbound.PIXELS_IN_STARBOUND_UNIT
|
|
import ru.dbotthepony.kstarbound.client.gl.GLStateTracker
|
|
import ru.dbotthepony.kstarbound.math.Vector2d
|
|
import ru.dbotthepony.kstarbound.util.Color
|
|
import kotlin.math.cos
|
|
import kotlin.math.sin
|
|
|
|
class Box2DRenderer(val state: GLStateTracker) : IDebugDraw {
|
|
override var drawShapes: Boolean = false
|
|
override var drawJoints: Boolean = false
|
|
override var drawAABB: Boolean = false
|
|
override var drawPairs: Boolean = false
|
|
override var drawCenterOfMess: Boolean = false
|
|
|
|
override fun drawPolygon(vertices: List<Vector2d>, color: Color) {
|
|
require(vertices.size > 1) { "Vertex list had only ${vertices.size} namings in it" }
|
|
|
|
val stateful = state.flat2DLines.statefulSmall
|
|
val builder = stateful.builder
|
|
|
|
builder.begin()
|
|
|
|
for (i in vertices.indices) {
|
|
val current = vertices[i]
|
|
val next = vertices[(i + 1) % vertices.size]
|
|
builder.Vertex().pushVec2f(current.x.toFloat(), current.y.toFloat())
|
|
builder.Vertex().pushVec2f(next.x.toFloat(), next.y.toFloat())
|
|
}
|
|
|
|
stateful.upload()
|
|
|
|
state.flatProgram.use()
|
|
state.flatProgram.color.set(color)
|
|
state.flatProgram.transform.set(state.matrixStack.last)
|
|
|
|
stateful.draw(GL_LINES)
|
|
}
|
|
|
|
private fun drawSolid(vertices: List<Vector2d>, color: Color) {
|
|
require(vertices.size >= 3) { "Vertex list had only ${vertices.size} namings in it" }
|
|
|
|
val stateful = state.flat2DTriangles.statefulSmall
|
|
val builder = stateful.builder
|
|
|
|
builder.begin()
|
|
|
|
val zero = vertices[0]
|
|
|
|
for (i in 1 until vertices.size) {
|
|
val current = vertices[i]
|
|
val next = vertices[(i + 1) % vertices.size]
|
|
builder.Vertex().pushVec2f(zero.x.toFloat(), zero.y.toFloat())
|
|
builder.Vertex().pushVec2f(current.x.toFloat(), current.y.toFloat())
|
|
builder.Vertex().pushVec2f(next.x.toFloat(), next.y.toFloat())
|
|
}
|
|
|
|
stateful.upload()
|
|
|
|
state.flatProgram.use()
|
|
state.flatProgram.color.set(color)
|
|
state.flatProgram.transform.set(state.matrixStack.last)
|
|
|
|
stateful.draw(GL_TRIANGLES)
|
|
}
|
|
|
|
override fun drawSolidPolygon(vertices: List<Vector2d>, color: Color) {
|
|
drawSolid(vertices, color.copy(alpha = 0.5f))
|
|
drawPolygon(vertices, color)
|
|
}
|
|
|
|
override fun drawCircle(center: Vector2d, radius: Double, color: Color) {
|
|
val vertexList = ArrayList<Vector2d>()
|
|
|
|
for (i in 0 until 360 step 15) {
|
|
val rad = Math.toRadians(i.toDouble())
|
|
val c = cos(rad)
|
|
val s = sin(rad)
|
|
|
|
vertexList.add(Vector2d(
|
|
center.x + c * radius,
|
|
center.y + s * radius
|
|
))
|
|
}
|
|
|
|
drawPolygon(vertexList, color)
|
|
}
|
|
|
|
override fun drawSolidCircle(center: Vector2d, radius: Double, axis: Vector2d, color: Color) {
|
|
val vertexList = ArrayList<Vector2d>()
|
|
|
|
for (i in 0 until 360 step 15) {
|
|
val rad = Math.toRadians(i.toDouble())
|
|
val c = cos(rad)
|
|
val s = sin(rad)
|
|
|
|
vertexList.add(Vector2d(
|
|
center.x + c * radius,
|
|
center.y + s * radius
|
|
))
|
|
}
|
|
|
|
drawSolidPolygon(vertexList, color.copy(alpha = 0.5f))
|
|
drawPolygon(vertexList, color)
|
|
drawPolygon(listOf(center, center + axis * radius), color)
|
|
}
|
|
|
|
override fun drawSegment(p1: Vector2d, p2: Vector2d, color: Color) {
|
|
drawPolygon(listOf(p1, p2), color)
|
|
}
|
|
|
|
override fun drawTransform(xf: Transform) {
|
|
TODO("Not yet implemented")
|
|
}
|
|
|
|
override fun drawPoint(p: Vector2d, size: Double, color: Color) {
|
|
drawSolid(listOf(
|
|
Vector2d(x = p.x - size / (PIXELS_IN_STARBOUND_UNIT * 2.0), y = p.y - size / (PIXELS_IN_STARBOUND_UNIT * 2.0)),
|
|
Vector2d(x = p.x + size / (PIXELS_IN_STARBOUND_UNIT * 2.0), y = p.y - size / (PIXELS_IN_STARBOUND_UNIT * 2.0)),
|
|
Vector2d(x = p.x + size / (PIXELS_IN_STARBOUND_UNIT * 2.0), y = p.y + size / (PIXELS_IN_STARBOUND_UNIT * 2.0)),
|
|
Vector2d(x = p.x - size / (PIXELS_IN_STARBOUND_UNIT * 2.0), y = p.y + size / (PIXELS_IN_STARBOUND_UNIT * 2.0)),
|
|
), color)
|
|
}
|
|
}
|