Skip to content

Commit

Permalink
fix bug with certain indices & vertices being overwritten when ensuri…
Browse files Browse the repository at this point in the history
…ng buffer size inside Mesh & IndexedMesh
  • Loading branch information
LeHaine committed Dec 7, 2024
1 parent 43f9010 commit a6e3dce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open class IndexedMeshGeometry(layout: VertexBufferLayout, size: Int = INITIAL_S

/** The number of indices added to the [indices] buffer. */
var numIndices: Int = 0
private set

/** If the geometry has changed since last update. */
override val dirty: Boolean
Expand Down Expand Up @@ -145,10 +146,13 @@ open class IndexedMeshGeometry(layout: VertexBufferLayout, size: Int = INITIAL_S
}

private fun increaseIndices(newSize: Int) {
logger.debug { "Increasing indices buffer size to $newSize" }
val oldPosition = indices.position
indices.position = 0
val newData = ShortBuffer(newSize)
newData.put(indices)
indices = newData
indices.position = 0
indices.position = oldPosition
}

/** The type of shape indices buffer are used for. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.littlekt.graphics.util
import com.littlekt.file.FloatBuffer
import com.littlekt.graphics.VertexBufferLayout
import com.littlekt.log.Logger
import kotlin.jvm.JvmStatic
import kotlin.math.max
import kotlin.math.min
import kotlin.math.round
Expand Down Expand Up @@ -68,7 +69,7 @@ open class MeshGeometry(val layout: VertexBufferLayout, size: Int = INITIAL_SIZE
newVertices: FloatArray,
srcOffset: Int = 0,
dstOffset: Int = 0,
count: Int = newVertices.size - srcOffset
count: Int = newVertices.size - srcOffset,
) {
ensureVertices(count / vertexSize)
vertices.put(data = newVertices, dstOffset = dstOffset, srcOffset = srcOffset, len = count)
Expand All @@ -85,6 +86,7 @@ open class MeshGeometry(val layout: VertexBufferLayout, size: Int = INITIAL_SIZE
* account the [vertexSize].
*/
fun skip(totalVertices: Int) {
ensureVertices(totalVertices)
vertices.position += totalVertices * vertexSize
numVertices += totalVertices
}
Expand All @@ -106,7 +108,7 @@ open class MeshGeometry(val layout: VertexBufferLayout, size: Int = INITIAL_SIZE
increaseVertices(
max(
round(vertices.capacity * GROW_FACTOR).toInt(),
(numVertices + required) * vertexSize
(numVertices + required) * vertexSize,
)
)
return true
Expand All @@ -115,16 +117,18 @@ open class MeshGeometry(val layout: VertexBufferLayout, size: Int = INITIAL_SIZE
}

private fun increaseVertices(newSize: Int) {
logger.debug { "Increasing vertices buffer size to $newSize" }
val oldPos = vertices.position
val newData = FloatBuffer(newSize)
vertices.position = 0
newData.put(vertices)
vertices = newData
vertices.position = 0
logger.debug { "Increasing vertices buffer size to $newSize" }
vertices.position = oldPos
}

companion object {
private const val INITIAL_SIZE = 1000
private const val GROW_FACTOR = 2f
private val logger = Logger<MeshGeometry>()
@JvmStatic protected val logger = Logger<MeshGeometry>()
}
}

0 comments on commit a6e3dce

Please sign in to comment.