Skip to content

Commit

Permalink
fix default renderer bugs and begin frames renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed Oct 27, 2023
1 parent a1bc059 commit aa811e1
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 77 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/solve/constants/ResourcesPaths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ const val IconsGridSelected = "/icons/sidepanel/GridSelected.png"
// Engine shaders.
const val ShadersDefaultVertexPath = "engine/shaders/default/default.vert"
const val ShadersDefaultFragmentPath = "engine/shaders/default/default.frag"
const val ShadersFrameVertexPath = "engine/shaders/frame/frame.vert"
const val ShadersFrameGeometryPath = "engine/shaders/frame/frame.geom"
const val ShadersFrameFragmentPath = "engine/shaders/frame/frame.frag"
54 changes: 39 additions & 15 deletions src/main/kotlin/solve/rendering/canvas/OpenGLCanvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import com.huskerdev.openglfx.events.GLInitializeEvent
import com.huskerdev.openglfx.events.GLRenderEvent
import com.huskerdev.openglfx.events.GLReshapeEvent
import com.huskerdev.openglfx.lwjgl.LWJGLExecutor
import javafx.application.Platform
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import org.joml.Matrix4f
import org.joml.Vector2f
import org.lwjgl.BufferUtils
Expand All @@ -15,6 +19,7 @@ import org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT
import org.lwjgl.opengl.GL11.GL_DEPTH_TEST
import org.lwjgl.opengl.GL11.GL_FLOAT
import org.lwjgl.opengl.GL11.GL_LEQUAL
import org.lwjgl.opengl.GL11.GL_LINE_SMOOTH
import org.lwjgl.opengl.GL11.GL_PROJECTION
import org.lwjgl.opengl.GL11.GL_TRIANGLES
import org.lwjgl.opengl.GL11.GL_UNSIGNED_INT
Expand Down Expand Up @@ -52,6 +57,7 @@ import solve.rendering.engine.scene.Transform
import solve.rendering.engine.shader.ShaderProgram
import solve.rendering.engine.shader.ShaderType
import solve.rendering.engine.structures.Color
import kotlin.math.sin
import com.huskerdev.openglfx.OpenGLCanvas as OpenGLFXCanvas

class OpenGLCanvas {
Expand All @@ -67,6 +73,10 @@ class OpenGLCanvas {

private lateinit var window: Window
private lateinit var renderer: DefaultRenderer
private lateinit var gameObject: GameObject
private var gs = mutableListOf<GameObject>()

var time = 0f

/*private lateinit var shaderProgram: ShaderProgram
private lateinit var camera: Camera
Expand All @@ -77,8 +87,11 @@ class OpenGLCanvas {
private var vboID = 0
private var eboID = 0*/

fun draw() {
fun draw(deltaTime: Float) {
println(1/deltaTime)
renderer.render()
time += deltaTime
gameObject.transform.position.x = sin(time) * 100

/*shaderProgram.use()
shaderProgram.uploadTexture("uTex", 0)
Expand Down Expand Up @@ -157,33 +170,44 @@ class OpenGLCanvas {
texture = Texture("icons/img.png")*/

window = Window(600, 600, Camera(Vector2f(), 1f))
window = Window(1920, 600, Camera(Vector2f(), 1f))
val scene = Scene(Camera())
val texture1 = Texture("icons/img.png")
val gameObject1 = GameObject(
gameObject = GameObject(
"gameObject1",
Transform(Vector2f(500f, 500f), 0f, Vector2f(10f, 10f)),
Transform(Vector2f(), 0f, Vector2f(1000f, 1000f)),
listOf(SpriteRenderer(texture1))
)
val gameObject2 = GameObject(

/*val gameObject2 = GameObject(
"gameObject2",
Transform(Vector2f(0f, 0f), 0f, Vector2f(100f , 100f)),
Transform(Vector2f(1f, 0f)),
listOf(SpriteRenderer(texture1).also { it.setColor(Color.black) })
)
val gameObject3 = GameObject(
"gameObject3",
Transform(Vector2f(-500f, -500f), 0f, Vector2f(50f, 50f)),
Transform(Vector2f(300f, 300f)),
listOf(SpriteRenderer(texture1))
)
scene.addGameObject(gameObject1)
scene.addGameObject(gameObject2)
scene.addGameObject(gameObject3)
)*/
scene.addGameObject(gameObject)
//scene.addGameObject(gameObject2)
//scene.addGameObject(gameObject3)
window.changeScene(scene)

renderer = DefaultRenderer(window)
renderer.addGameObject(gameObject1)
renderer.addGameObject(gameObject2)
renderer.addGameObject(gameObject3)
renderer.addGameObject(gameObject)
//renderer.addGameObject(gameObject2)
//renderer.addGameObject(gameObject3)


for (i in -20 until 20)
for (j in -20 until 20) {
renderer.addGameObject( GameObject(
"gameObject1",
Transform(Vector2f(i * 10f, j * 10f), 0f, Vector2f(100f, 100f)),
listOf(SpriteRenderer(texture1))
))
}
}

@Suppress("UNUSED_PARAMETER")
Expand All @@ -193,7 +217,7 @@ class OpenGLCanvas {

glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

draw()
draw(event.delta.toFloat())
}

private fun reshape(event: GLReshapeEvent) {
Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/solve/rendering/engine/camera/Camera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package solve.rendering.engine.camera

import org.joml.Matrix4f
import org.joml.Vector2f
import org.joml.Vector3f
import solve.utils.times

class Camera(var position: Vector2f = Vector2f(), zoom: Float = 1f) {
Expand All @@ -22,13 +21,13 @@ class Camera(var position: Vector2f = Vector2f(), zoom: Float = 1f) {
return orthoMatrix * zoomMatrix
}

private fun calculateZoomMatrix(): Matrix4f = Matrix4f().scale(zoom)
private fun calculateZoomMatrix(): Matrix4f = Matrix4f().scale(zoom * DefaultCameraScaleCoefficient)

private fun calculateOrthoMatrix(projectionSize: Vector2f): Matrix4f {
val projectionLeft = position.x - projectionSize.x / 2f
val projectionRight = position.x + projectionSize.x / 2f
val projectionTop = position.y - projectionSize.y / 2f
val projectionBottom = position.y + projectionSize.y / 2f
val projectionTop = position.y + projectionSize.y / 2f
val projectionBottom = position.y - projectionSize.y / 2f

return Matrix4f().ortho(
projectionLeft,
Expand All @@ -41,10 +40,8 @@ class Camera(var position: Vector2f = Vector2f(), zoom: Float = 1f) {
}

companion object {
private const val ProjectionOrthoZNear = 0.01f
private const val DefaultCameraScaleCoefficient = 1f
private const val ProjectionOrthoZNear = -100f
private const val ProjectionOrthoZFar = 100f

private val cameraFrontVector = Vector3f(0f, 0f, -1f)
private val cameraUpVector = Vector3f(0f, 1f, 0f)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ open class RenderBatch(
private val attributesNumber = attributes.sumOf { it.number }
private val attributesTotalSize = attributes.sumOf { it.size }

private val verticesDataBuffer = FloatArray(maxBatchSize * primitiveType.verticesNumber * attributesNumber)
private val verticesDataBufferSize = maxBatchSize * primitiveType.verticesNumber * attributesNumber
private val verticesDataBuffer = FloatArray(verticesDataBufferSize)
private var verticesDataBufferIndexPointer = 0

init {
Expand All @@ -61,7 +62,7 @@ open class RenderBatch(
fun bind() {
glBindVertexArray(vaoID)
attributes.forEachIndexed { index, _ -> glEnableVertexAttribArray(index) }
textures.forEachIndexed { index, texture -> texture.bindToSlot(index) }
textures.forEachIndexed { index, texture -> texture.bindToSlot(index + 1) }
}

fun unbind() {
Expand Down Expand Up @@ -112,6 +113,11 @@ open class RenderBatch(
primitiveType.drawingOrderElementsNumber
}

private fun checkIfFull() {
if (verticesDataBufferIndexPointer == verticesDataBufferSize)
isFull = true
}

private fun initializeBuffers() {
vaoID = glGenVertexArrays()
glBindVertexArray(vaoID)
Expand Down Expand Up @@ -156,7 +162,13 @@ open class RenderBatch(
}

fun pushFloat(value: Float) {
if (isFull) {
println("Cannot push a float value! The vertices data buffer is full!")
return
}

verticesDataBuffer[verticesDataBufferIndexPointer++] = value
checkIfFull()
}

fun pushVector2f(vector: Vector2f) {
Expand All @@ -172,11 +184,7 @@ open class RenderBatch(
}

fun pushInt(value: Int) {
val byteArray = ByteBuffer.allocate(Int.SIZE_BYTES).putInt(value).array()
val buffer = ByteBuffer.wrap(byteArray)
val floatValue = buffer.float

pushFloat(floatValue)
pushFloat(value.toFloat())
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package solve.rendering.engine.rendering.renderers

import org.joml.Matrix4f
import org.joml.Vector2f
import org.joml.Vector3f
import org.lwjgl.opengl.GL13.GL_TEXTURE0
import org.lwjgl.opengl.GL13.glActiveTexture
import solve.constants.ShadersDefaultFragmentPath
import solve.constants.ShadersDefaultVertexPath
import solve.rendering.engine.Window
Expand All @@ -22,7 +19,7 @@ class DefaultRenderer(
) : Renderer(window) {
override val maxBatchSize = 1000

private var modelsCommonMatrix: Matrix4f = Matrix4f().identity()
private var modelsCommonMatrix = Matrix4f().identity()
private val spriteRenderers = mutableListOf<SpriteRenderer>()

fun changeModelsCommonMatrix(newMatrix: Matrix4f) {
Expand Down Expand Up @@ -55,10 +52,8 @@ class DefaultRenderer(
}

override fun uploadUniforms(shaderProgram: ShaderProgram) {
shaderProgram.uploadMatrix4f(ProjectionUniformName, Matrix4f().identity().scale(0.001f))
shaderProgram.uploadTexture("uTex", 0)
glActiveTexture(GL_TEXTURE0)
//shaderProgram.uploadMatrix4f(modelUniformName, modelsCommonMatrix)
shaderProgram.uploadMatrix4f(ProjectionUniformName, window.calculateProjectionMatrix())
shaderProgram.uploadMatrix4f(ModelUniformName, modelsCommonMatrix)
}

override fun updateBatchesData() {
Expand Down Expand Up @@ -97,6 +92,7 @@ class DefaultRenderer(

override fun removeGameObject(gameObject: GameObject): Boolean {
val spriteRenderer = gameObject.getComponentOfType<SpriteRenderer>() ?: return false

return spriteRenderers.remove(spriteRenderer)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package solve.rendering.engine.rendering.renderers

import org.joml.Matrix4f
import org.joml.Vector2f
import org.joml.Vector2i
import org.lwjgl.opengl.GL11.GL_RGBA
import org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE
import org.lwjgl.opengl.GL11.glBindTexture
import org.lwjgl.opengl.GL11.glGenTextures
import org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY
import org.lwjgl.opengl.GL45.glTextureStorage3D
import org.lwjgl.opengl.GL45.glTextureSubImage3D
import solve.constants.ShadersFrameFragmentPath
import solve.constants.ShadersFrameGeometryPath
import solve.constants.ShadersFrameVertexPath
import solve.project.model.ProjectFrame
import solve.rendering.engine.Window
import solve.rendering.engine.rendering.batch.PrimitiveType
import solve.rendering.engine.rendering.batch.RenderBatch
import solve.rendering.engine.rendering.texture.Texture
import solve.rendering.engine.rendering.texture.TextureChannelsType
import solve.rendering.engine.shader.ShaderAttributeType
import solve.rendering.engine.shader.ShaderProgram
import solve.rendering.engine.shader.ShaderType

class FramesRenderer(
window: Window
) : Renderer(window) {
override val maxBatchSize = 1000
private var modelsCommonMatrix = Matrix4f().identity()
private var gridWidth = DefaultGridWidth
private var buffersSize = defaultBuffersSize

private var framesNumber = 0
private var framesWidth = 0
private var framesHeight = 0
private var framesChannelsType = TextureChannelsType.RGBA

fun changeModelsCommonMatrix(newMatrix: Matrix4f) {
modelsCommonMatrix = newMatrix
}

private fun initializeBuffersTextures(frames: List<ProjectFrame>) {
val firstTextureData = Texture.loadData(frames.first().imagePath.toString())
if (firstTextureData == null) {
println("The read texture is null!")
return
}

framesWidth = firstTextureData.width
framesHeight = firstTextureData.height
framesChannelsType = firstTextureData.channelsType

val arrayTextureID = glGenTextures()
glBindTexture(GL_TEXTURE_2D_ARRAY, arrayTextureID)
glTextureStorage3D(
arrayTextureID,
0,
framesChannelsType.openGLType,
framesWidth,
framesHeight,
128
)

frames.forEachIndexed { index, frame ->
val textureData = Texture.loadData(frame.imagePath.toString())
if (textureData == null) {
println("The read texture is null!")
return@forEachIndexed
}

glTextureSubImage3D(
arrayTextureID,
0,
0,
0,
0,
framesWidth,
framesHeight,
1,
GL_RGBA,
GL_UNSIGNED_BYTE,
textureData.data
)
}
}

override fun createShaderProgram(): ShaderProgram {
val shaderProgram = ShaderProgram()
shaderProgram.addShader(ShadersFrameVertexPath, ShaderType.VERTEX)
shaderProgram.addShader(ShadersFrameGeometryPath, ShaderType.GEOMETRY)
shaderProgram.addShader(ShadersFrameFragmentPath, ShaderType.FRAGMENT)
shaderProgram.link()

return shaderProgram
}

override fun uploadUniforms(shaderProgram: ShaderProgram) {
shaderProgram.uploadMatrix4f(ProjectionUniformName, window.calculateProjectionMatrix())
shaderProgram.uploadMatrix4f(ModelUniformName, modelsCommonMatrix)
shaderProgram.uploadInt(GridWidthUniformName, gridWidth)
shaderProgram.uploadVector2i(BuffersSizeUniformName, buffersSize)
}

override fun createNewBatch(zIndex: Int) =
RenderBatch(maxBatchSize, zIndex, PrimitiveType.Point, listOf(ShaderAttributeType.FLOAT2))


override fun updateBatchesData() {
// TODO
}

companion object {
private const val ProjectionUniformName = "uProjection"
private const val ModelUniformName = "uModel"
private const val GridWidthUniformName = "uGridWidth"
private const val BuffersSizeUniformName = "uBuffersSize"
private const val TexturesUniformName = "uTextures"

private const val DefaultGridWidth = 5

private val defaultBuffersSize = Vector2i(10, 10)
}
}
Loading

0 comments on commit aa811e1

Please sign in to comment.