Skip to content

Commit

Permalink
fix rendering system bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed Oct 23, 2023
1 parent 669e389 commit a1bc059
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 92 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ dependencies {
runtimeOnly("org.lwjgl:lwjgl-glfw:$lwjglVersion:$lwjglNativesClassifier")
runtimeOnly("org.lwjgl:lwjgl-openal:$lwjglVersion:$lwjglNativesClassifier")
runtimeOnly("org.lwjgl:lwjgl-opengl:$lwjglVersion:$lwjglNativesClassifier")
runtimeOnly("org.lwjgl:lwjgl:$lwjglVersion:$lwjglNativesClassifier")
runtimeOnly("org.lwjgl:lwjgl-stb:$lwjglVersion:$lwjglNativesClassifier")
implementation("org.joml:joml:$jomlVersion")
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/solve/main/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import solve.importer.controller.ImporterController
import solve.importer.view.ImporterView
import solve.main.splitpane.SidePanelLocation
import solve.main.splitpane.SidePanelSplitPane
import solve.rendering.canvas.OpenGLCanvas
import solve.scene.view.SceneView
import solve.settings.grid.view.GridSettingsView
import solve.settings.visualization.VisualizationSettingsView
Expand Down Expand Up @@ -91,6 +92,8 @@ class MainView : View() {
)
)

private val canvas = OpenGLCanvas()

private val leftSidePanelViews =
createSidePanelsViews(leftSidePanelTabs, SidePanelLocation.Left, leftSidePanelTabs.first())
private val rightSidePanelViews =
Expand Down Expand Up @@ -158,6 +161,8 @@ class MainView : View() {
)
mainViewSplitPane.addStylesheet(MainSplitPaneStyle::class)
center = mainViewSplitPane
bottom = canvas.canvas
canvas.canvas.setMinSize(600.0, 600.0)
}

override val root = mainViewBorderPane
Expand Down
146 changes: 146 additions & 0 deletions src/main/kotlin/solve/rendering/canvas/OpenGLCanvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,53 @@ 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 org.joml.Matrix4f
import org.joml.Vector2f
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL.createCapabilities
import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT
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_PROJECTION
import org.lwjgl.opengl.GL11.GL_TRIANGLES
import org.lwjgl.opengl.GL11.GL_UNSIGNED_INT
import org.lwjgl.opengl.GL11.glClear
import org.lwjgl.opengl.GL11.glDepthFunc
import org.lwjgl.opengl.GL11.glDrawElements
import org.lwjgl.opengl.GL11.glEnable
import org.lwjgl.opengl.GL11.glFrustum
import org.lwjgl.opengl.GL11.glLoadIdentity
import org.lwjgl.opengl.GL11.glMatrixMode
import org.lwjgl.opengl.GL11.glTranslatef
import org.lwjgl.opengl.GL13.GL_TEXTURE0
import org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER
import org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER
import org.lwjgl.opengl.GL15.GL_STATIC_DRAW
import org.lwjgl.opengl.GL15.glActiveTexture
import org.lwjgl.opengl.GL15.glBindBuffer
import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL15.glGenBuffers
import org.lwjgl.opengl.GL20C
import org.lwjgl.opengl.GL30
import org.lwjgl.opengl.GL30.glBindVertexArray
import org.lwjgl.opengl.GL30.glDisableVertexAttribArray
import org.lwjgl.opengl.GL30.glEnableVertexAttribArray
import solve.constants.ShadersDefaultFragmentPath
import solve.constants.ShadersDefaultVertexPath
import solve.rendering.engine.Window
import solve.rendering.engine.camera.Camera
import solve.rendering.engine.components.SpriteRenderer
import solve.rendering.engine.rendering.renderers.DefaultRenderer
import solve.rendering.engine.rendering.texture.Texture
import solve.rendering.engine.scene.GameObject
import solve.rendering.engine.scene.Scene
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 com.huskerdev.openglfx.OpenGLCanvas as OpenGLFXCanvas

class OpenGLCanvas {
Expand All @@ -32,12 +65,125 @@ class OpenGLCanvas {
canvas.addOnInitEvent(this::canvasInit)
}

private lateinit var window: Window
private lateinit var renderer: DefaultRenderer

/*private lateinit var shaderProgram: ShaderProgram
private lateinit var camera: Camera
private lateinit var vertices: FloatArray
private lateinit var elements: IntArray
private lateinit var texture: Texture
private var vaoID = 0
private var vboID = 0
private var eboID = 0*/

fun draw() {
renderer.render()

/*shaderProgram.use()
shaderProgram.uploadTexture("uTex", 0)
glActiveTexture(GL_TEXTURE0)
texture.bind()
shaderProgram.uploadMatrix4f("uProjection", Matrix4f().identity().scale(0.001f))
glBindVertexArray(vaoID)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2)
glDrawElements(GL_TRIANGLES, elements.size, GL_UNSIGNED_INT, 0)
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
glEnableVertexAttribArray(2)
glBindVertexArray(0)
shaderProgram.detach()*/
}

private fun canvasInit(event: GLInitializeEvent) {
event.toString()
createCapabilities()

/*shaderProgram = ShaderProgram()
shaderProgram.addShader(ShadersDefaultVertexPath, ShaderType.VERTEX)
shaderProgram.addShader(ShadersDefaultFragmentPath, ShaderType.FRAGMENT)
shaderProgram.link()
camera = Camera()
vertices = floatArrayOf(
100f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 1f,
0f, 100f, 0f, 0f, 1f, 0f, 1f, 0f, 0f,
100f, 100f, 0f, 1f, 0f, 1f, 1f, 1f, 0f,
0f, 0f, 0f, 1f, 1f, 0f, 1f, 0f, 1f
)
elements = intArrayOf(2, 1, 0, 0, 1, 3)
vaoID = GL30.glGenVertexArrays()
GL30.glBindVertexArray(vaoID)
val verticesBuffer = BufferUtils.createFloatBuffer(vertices.size)
verticesBuffer.put(vertices).flip()
vboID = glGenBuffers()
glBindBuffer(GL_ARRAY_BUFFER, vboID)
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW)
val elementsBuffer = BufferUtils.createIntBuffer(elements.size)
elementsBuffer.put(elements).flip()
val eboID = glGenBuffers()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementsBuffer, GL_STATIC_DRAW)
val posSize = 3
val colorSize = 4
val uvSize = 2
val verticesSizeBytes = (posSize + colorSize + uvSize) * Float.SIZE_BYTES
GL20C.glVertexAttribPointer(0, posSize, GL_FLOAT, false, verticesSizeBytes, 0L)
GL20C.glEnableVertexAttribArray(0)
GL20C.glVertexAttribPointer(1, colorSize, GL_FLOAT, false, verticesSizeBytes, (posSize * Float.SIZE_BYTES).toLong())
GL20C.glEnableVertexAttribArray(1)
GL20C.glVertexAttribPointer(2, uvSize, GL_FLOAT, false, verticesSizeBytes, ((posSize + colorSize) * Float.SIZE_BYTES).toLong())
GL20C.glEnableVertexAttribArray(2)
texture = Texture("icons/img.png")*/

window = Window(600, 600, Camera(Vector2f(), 1f))
val scene = Scene(Camera())
val texture1 = Texture("icons/img.png")
val gameObject1 = GameObject(
"gameObject1",
Transform(Vector2f(500f, 500f), 0f, Vector2f(10f, 10f)),
listOf(SpriteRenderer(texture1))
)
val gameObject2 = GameObject(
"gameObject2",
Transform(Vector2f(0f, 0f), 0f, Vector2f(100f , 100f)),
listOf(SpriteRenderer(texture1).also { it.setColor(Color.black) })
)
val gameObject3 = GameObject(
"gameObject3",
Transform(Vector2f(-500f, -500f), 0f, Vector2f(50f, 50f)),
listOf(SpriteRenderer(texture1))
)
scene.addGameObject(gameObject1)
scene.addGameObject(gameObject2)
scene.addGameObject(gameObject3)
window.changeScene(scene)

renderer = DefaultRenderer(window)
renderer.addGameObject(gameObject1)
renderer.addGameObject(gameObject2)
renderer.addGameObject(gameObject3)
}

@Suppress("UNUSED_PARAMETER")
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/solve/rendering/engine/components/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ package solve.rendering.engine.components

import solve.rendering.engine.scene.GameObject

abstract class Component(val gameObject: GameObject) {
abstract class Component {
var gameObject: GameObject? = null
private set

fun addToGameObject(gameObject: GameObject) {
this.gameObject?.removeComponent(this)

this.gameObject = gameObject
gameObject.addComponent(this)
}

open fun start() { }

open fun update(deltaTime: Float) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import solve.rendering.engine.rendering.texture.Texture
import solve.rendering.engine.scene.GameObject
import solve.rendering.engine.structures.Color

class SpriteRenderer(gameObject: GameObject) : Component(gameObject) {
class SpriteRenderer() : Component() {
var color = Color.white
private set
var sprite: Sprite? = null
Expand All @@ -13,10 +13,22 @@ class SpriteRenderer(gameObject: GameObject) : Component(gameObject) {
val texture: Texture?
get() = sprite?.texture

constructor(texture: Texture): this() {
setTexture(texture)
}

constructor(sprite: Sprite): this() {
setSprite(sprite)
}

fun setColor(color: Color) {
this.color = color
}

fun setSprite(sprite: Sprite) {
this.sprite = sprite
}

fun setTexture(texture: Texture) {
if (sprite?.texture != texture) {
sprite = Sprite(texture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.lwjgl.opengl.GL15.glBufferData
import org.lwjgl.opengl.GL15.glBufferSubData
import org.lwjgl.opengl.GL15.glDeleteBuffers
import org.lwjgl.opengl.GL15.glGenBuffers
import org.lwjgl.opengl.GL20.glDisableVertexAttribArray
import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20.glVertexAttribPointer
import org.lwjgl.opengl.GL30.glBindVertexArray
Expand Down Expand Up @@ -50,12 +51,21 @@ open class RenderBatch(
initializeAttributes()
}

open fun cleanupData() {
verticesDataBufferIndexPointer = 0
isTexturesFull = false
isFull = false
textures.clear()
}

fun bind() {
glBindVertexArray(vaoID)
textures.forEachIndexed { index, texture -> texture.bindToSlot(index + 1) }
attributes.forEachIndexed { index, _ -> glEnableVertexAttribArray(index) }
textures.forEachIndexed { index, texture -> texture.bindToSlot(index) }
}

fun unbind() {
attributes.forEachIndexed { index, _ -> glDisableVertexAttribArray(index) }
textures.forEach { it.unbind() }
glBindVertexArray(0)
}
Expand Down Expand Up @@ -98,10 +108,8 @@ open class RenderBatch(
println("The vertices data buffer seems to not have correct amount of data!")
}

return (
verticesDataBufferIndexPointer * primitiveType.drawingOrderElementsNumber /
(attributesNumber * primitiveType.verticesNumber)
)
return verticesDataBufferIndexPointer / attributesNumber / primitiveType.verticesNumber *
primitiveType.drawingOrderElementsNumber
}

private fun initializeBuffers() {
Expand All @@ -120,26 +128,27 @@ open class RenderBatch(

private fun initializeAttributes() {
var attributesSizeOffset = 0L
for (i in 0 until attributes.count()) {
val attribute = attributes[i]
attributes.forEachIndexed { index, attribute ->
glVertexAttribPointer(
i,
index,
attribute.number,
attribute.openGLType,
false,
attributesTotalSize,
attributesSizeOffset
)
glEnableVertexAttribArray(i)
glEnableVertexAttribArray(index)
attributesSizeOffset += attribute.size
}
}

private fun generateElementsIndices(): IntArray {
val elementsBuffer = IntArray(maxBatchSize * primitiveType.verticesNumber)
val elementsBuffer = IntArray(maxBatchSize * primitiveType.drawingOrderElementsNumber)
for (i in 0 until maxBatchSize) {
primitiveType.verticesDrawingOrder.forEach { vertexIndex ->
elementsBuffer[i * primitiveType.verticesNumber] = i + vertexIndex
val elementFirstVertexIndex = i * primitiveType.verticesNumber
val bufferIndexPointer = i * primitiveType.drawingOrderElementsNumber
primitiveType.verticesDrawingOrder.forEachIndexed { index, vertexIndex ->
elementsBuffer[bufferIndexPointer + index] = elementFirstVertexIndex + vertexIndex
}
}

Expand Down
Loading

0 comments on commit a1bc059

Please sign in to comment.