Skip to content

Commit

Permalink
fix planes drawing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed Apr 8, 2024
1 parent 7038bb2 commit 2a5f3ed
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 43 deletions.
23 changes: 17 additions & 6 deletions src/main/kotlin/solve/rendering/engine/core/batch/RenderBatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.lwjgl.opengl.GL20.glVertexAttribPointer
import org.lwjgl.opengl.GL30.glBindVertexArray
import org.lwjgl.opengl.GL30.glDeleteVertexArrays
import org.lwjgl.opengl.GL30.glGenVertexArrays
import solve.rendering.engine.core.texture.Texture
import solve.rendering.engine.core.texture.Texture2D
import solve.rendering.engine.shader.ShaderAttributeType
import solve.rendering.engine.utils.toList
Expand All @@ -30,7 +31,7 @@ open class RenderBatch(
val zIndex: Int,
val primitiveType: PrimitiveType,
private val attributes: List<ShaderAttributeType>
) {
) : Comparable<RenderBatch> {
var isFull = false
private set
var isTexturesFull = false
Expand All @@ -40,7 +41,7 @@ open class RenderBatch(
private var vaoID = 0
private var eboID = 0

private val textures = mutableListOf<Texture2D>()
private val textures = mutableListOf<Texture>()

private val attributesNumber = attributes.sumOf { it.number }
private val attributesTotalSize = attributes.sumOf { it.size }
Expand All @@ -61,6 +62,15 @@ open class RenderBatch(
textures.clear()
}

override fun compareTo(other: RenderBatch): Int {
return if (zIndex < other.zIndex)
-1
else if (zIndex > other.zIndex)
1
else
0
}

fun bind() {
glBindVertexArray(vaoID)
attributes.forEachIndexed { index, _ -> glEnableVertexAttribArray(index) }
Expand All @@ -69,6 +79,7 @@ open class RenderBatch(

fun unbind() {
attributes.forEachIndexed { index, _ -> glDisableVertexAttribArray(index) }
textures.forEach { texture -> texture.unbind() }
glBindVertexArray(0)
}

Expand All @@ -83,7 +94,7 @@ open class RenderBatch(
glDeleteVertexArrays(vaoID)
}

fun addTexture(texture: Texture2D): Int {
fun addTexture(texture: Texture): Int {
val textureID: Int
if (textures.contains(texture)) {
textureID = textures.indexOf(texture) + 1
Expand All @@ -99,11 +110,11 @@ open class RenderBatch(
return textureID
}

fun getTextureLocalID(texture: Texture2D) = textures.indexOf(texture) + 1
fun getTextureLocalID(texture: Texture) = textures.indexOf(texture) + 1

fun removeTexture(texture: Texture2D): Boolean = textures.remove(texture)
fun removeTexture(texture: Texture): Boolean = textures.remove(texture)

fun containsTexture(texture: Texture2D) = textures.contains(texture)
fun containsTexture(texture: Texture) = textures.contains(texture)

fun getVerticesNumber(): Int {
if (verticesDataBufferIndexPointer % attributesNumber != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import solve.rendering.engine.core.batch.PrimitiveType
import solve.rendering.engine.core.batch.RenderBatch
import solve.rendering.engine.core.texture.Texture
import solve.rendering.engine.core.texture.Texture2D
import solve.rendering.engine.core.texture.TextureFilterType
import solve.rendering.engine.shader.ShaderAttributeType
import solve.rendering.engine.shader.ShaderProgram
import solve.rendering.engine.shader.ShaderType
Expand Down Expand Up @@ -58,31 +59,30 @@ class PlanesLayerRenderer(

override fun uploadUniforms(shaderProgram: ShaderProgram) {
shaderProgram.uploadMatrix4f(ProjectionUniformName, window.calculateProjectionMatrix())
shaderProgram.uploadIntArray(TexturesUniformName, texturesIndices)
}

override fun beforeRender() {

}

override fun updateBatchesData() {
if (needToInitializePlaneTextures) {
planeLayersTextures = planeLayers.map { Texture2D(it.filePath.toString()) }
planeLayersTextures = planeLayers.map { Texture2D(it.filePath.toString(), TextureFilterType.PixelPerfect) }
needToInitializePlaneTextures = false
}

val firstPlaneLayer = planeLayers.firstOrNull() ?: return
renderPriority = getScene()?.indexOf(firstPlaneLayer.settings) ?: return
}

override fun updateBatchesData() {
planeLayers.forEachIndexed { planeLayerIndex, planeLayer ->
if (!planeLayer.settings.enabled)
return@forEachIndexed

val planeLayerTexture = planeLayersTextures[planeLayerIndex]
//val layerIndex = getScene()?.indexOf(planeLayer.settings) ?: return@forEachIndexed
val batch = getAvailableBatch(planeLayerTexture, 0)
val textureID = batch.getTextureLocalID(planeLayerTexture)
val topLeftFrameShaderPosition = getFrameTopLeftShaderPosition(planeLayerIndex)
textureLocalVerticesPositions.forEachIndexed { localVertexIndex, localVertexPosition ->
val q = Vector2f(localVertexPosition)
if (planeLayer.name.contains("alg1"))
q.mul(0.5f)
else
q.mul(0.8f)
val scaledLocalVertexPosition = Vector2f(q.x * framesRatio, q.y)
val scaledLocalVertexPosition = Vector2f(localVertexPosition.x * framesRatio, localVertexPosition.y)
val vertexShaderPosition = topLeftFrameShaderPosition + scaledLocalVertexPosition
batch.pushVector2f(vertexShaderPosition)
batch.pushVector2f(Texture.defaultUVCoordinates[localVertexIndex])
Expand All @@ -92,6 +92,9 @@ class PlanesLayerRenderer(
}
companion object {
private const val ProjectionUniformName = "uProjection"
private const val TexturesUniformName = "uTextures"

private val texturesIndices = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7)

private val textureLocalVerticesPositions = listOf(
Vector2f(0.0f, 1.0f),
Expand Down
19 changes: 16 additions & 3 deletions src/main/kotlin/solve/rendering/engine/core/renderers/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import org.lwjgl.opengl.GL11.GL_UNSIGNED_INT
import org.lwjgl.opengl.GL11.glDrawElements
import solve.rendering.engine.Window
import solve.rendering.engine.core.batch.RenderBatch
import solve.rendering.engine.core.texture.Texture
import solve.rendering.engine.core.texture.Texture2D
import solve.rendering.engine.shader.ShaderProgram
import solve.scene.model.VisualizationFrame

abstract class Renderer(protected val window: Window) {
abstract class Renderer(protected val window: Window) : Comparable<Renderer> {
protected abstract val maxBatchSize: Int

protected var needToRebuffer = true
protected lateinit var shaderProgram: ShaderProgram
protected val batches = mutableListOf<RenderBatch>()

protected var renderPriority = 0

init {
initialize()
}
Expand All @@ -34,7 +37,8 @@ abstract class Renderer(protected val window: Window) {
rebufferBatches()
}

batches.forEach { batch ->
batches.sort()
batches.sorted().forEach { batch ->
batch.bind()
glDrawElements(batch.primitiveType.openGLPrimitive, batch.getVerticesNumber(), GL_UNSIGNED_INT, 0)
batch.unbind()
Expand All @@ -44,12 +48,21 @@ abstract class Renderer(protected val window: Window) {
shaderProgram.detach()
}

override fun compareTo(other: Renderer): Int {
return if (renderPriority < other.renderPriority)
-1
else if (renderPriority > other.renderPriority)
1
else
0
}

fun delete() {
batches.forEach { it.deleteBuffers() }
shaderProgram.delete()
}

protected fun getAvailableBatch(texture: Texture2D?, requiredZIndex: Int): RenderBatch {
protected fun getAvailableBatch(texture: Texture?, requiredZIndex: Int): RenderBatch {
batches.forEach { batch ->
if (batch.isFull || batch.zIndex != requiredZIndex) {
return@forEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class ArrayTexture(
width: Int,
height: Int,
channelsType: TextureChannelsType,
val size: Int
) : Texture() {
val size: Int,
filterType: TextureFilterType = TextureFilterType.Smoothed
) : Texture(filterType) {
override val textureOpenGLType: Int = GL_TEXTURE_2D_ARRAY

init {
Expand All @@ -30,6 +31,12 @@ class ArrayTexture(
initialize()
}

override fun initializeTextureParams() {
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glGenerateMipmap(GL_TEXTURE_2D_ARRAY)
}

fun uploadTexture(textureData: Texture2DData, layerIndex: Int) {
if (textureData.width != width || textureData.height != height || textureData.channelsType != channelsType) {
println("Uploading texture has incorrect configuration!")
Expand Down Expand Up @@ -65,12 +72,4 @@ class ArrayTexture(
null as ByteBuffer?
)
}

override fun initializeTextureParams() {
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glGenerateMipmap(GL_TEXTURE_2D_ARRAY)
}
}
22 changes: 21 additions & 1 deletion src/main/kotlin/solve/rendering/engine/core/texture/Texture.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package solve.rendering.engine.core.texture

import com.huskerdev.openglfx.core.GL_NEAREST
import org.joml.Vector2f
import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL11.GL_LINEAR
import org.lwjgl.opengl.GL11.GL_TEXTURE_2D
import org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER
import org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER
import org.lwjgl.opengl.GL11.glBindTexture
import org.lwjgl.opengl.GL11.glDeleteTextures
import org.lwjgl.opengl.GL11.glGenTextures
import org.lwjgl.opengl.GL11.glTexParameteri
import org.lwjgl.opengl.GL13
import org.lwjgl.opengl.GL13.GL_TEXTURE0
import org.lwjgl.opengl.GL13.glActiveTexture

abstract class Texture {
enum class TextureFilterType(val openGLParamValue: Int)
{
PixelPerfect(GL_NEAREST),
Smoothed(GL_LINEAR)
}

abstract class Texture(private val filterType: TextureFilterType = TextureFilterType.Smoothed) {
protected abstract val textureOpenGLType: Int

val textureID: Int = glGenTextures()
Expand All @@ -23,6 +36,12 @@ abstract class Texture {

protected abstract fun initializeTexture()

private fun initializeTextureFilterParams()
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterType.openGLParamValue)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterType.openGLParamValue)
}

fun bind() {
glBindTexture(textureOpenGLType, textureID)
}
Expand All @@ -47,6 +66,7 @@ abstract class Texture {

protected fun initialize() {
bind()
initializeTextureFilterParams()
initializeTextureParams()
initializeTexture()
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/kotlin/solve/rendering/engine/core/texture/Texture2D.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package solve.rendering.engine.core.texture

import org.lwjgl.opengl.GL11.GL_LINEAR
import org.lwjgl.opengl.GL11.GL_REPEAT
import org.lwjgl.opengl.GL11.GL_TEXTURE_2D
import org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER
import org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER
import org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S
import org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T
import org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE
Expand All @@ -13,9 +10,11 @@ import org.lwjgl.opengl.GL11.glTexParameteri
import org.lwjgl.stb.STBImage.stbi_image_free
import org.lwjgl.stb.STBImage.stbi_load
import org.lwjgl.stb.STBImage.stbi_set_flip_vertically_on_load
import solve.utils.getResourceAbsolutePath

class Texture2D(private val filePath: String) : Texture() {
class Texture2D(
private val filePath: String,
filterType: TextureFilterType = TextureFilterType.Smoothed
) : Texture(filterType) {
override val textureOpenGLType: Int = GL_TEXTURE_2D

init {
Expand All @@ -25,9 +24,6 @@ class Texture2D(private val filePath: String) : Texture() {
override fun initializeTextureParams() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
}

override fun initializeTexture() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/solve/rendering/engine/scene/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Scene(val framesRenderer: FramesRenderer) {

private fun render() {
framesRenderer.render()
landmarkRenderers.forEach { it.render() }
_landmarkRenderers.sort()
//_landmarkRenderers.forEach { it.render() }
}
}
4 changes: 2 additions & 2 deletions src/main/resources/engine/shaders/landmark/plane/plane.frag
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void main()
}

if (color == vec4(0, 0, 0, 1)) {
color = vec4(0, 0, 0, 0);
discard;
}
} else {
color = vec4(0, 0, 0, 0);
discard;
}
}

0 comments on commit 2a5f3ed

Please sign in to comment.