changeset 356:befa060d782d

fix: indexing wrong
author Sam <sam@basx.dev>
date Thu, 21 Sep 2023 23:34:38 +0700
parents 26b29ca448fc
children 2942ec187cbe
files src/semicongine/mesh.nim src/semicongine/resources/mesh.nim
diffstat 2 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/mesh.nim	Wed Sep 20 23:25:44 2023 +0700
+++ b/src/semicongine/mesh.nim	Thu Sep 21 23:34:38 2023 +0700
@@ -46,8 +46,20 @@
 func instanceCount*(mesh: MeshObject): int =
   mesh.instanceTransforms.len
 
+func indicesCount*(mesh: MeshObject): int =
+  (
+    case mesh.indexType
+    of None: 0
+    of Tiny: mesh.tinyIndices.len
+    of Small: mesh.smallIndices.len
+    of Big: mesh.bigIndices.len
+  ) * 3
+
 func `$`*(mesh: MeshObject): string =
-  &"Mesh(vertexCount: {mesh.vertexCount}, instanceCount: {mesh.instanceCount}, vertexData: {mesh.vertexData.keys().toSeq()}, instanceData: {mesh.instanceData.keys().toSeq()}, indexType: {mesh.indexType})"
+  if mesh.indexType == None:
+    &"Mesh(vertexCount: {mesh.vertexCount}, instanceCount: {mesh.instanceCount}, vertexData: {mesh.vertexData.keys().toSeq()}, instanceData: {mesh.instanceData.keys().toSeq()}, indexType: {mesh.indexType})"
+  else:
+    &"Mesh(vertexCount: {mesh.vertexCount}, indexCount: {mesh.indicesCount}, instanceCount: {mesh.instanceCount}, vertexData: {mesh.vertexData.keys().toSeq()}, instanceData: {mesh.instanceData.keys().toSeq()}, indexType: {mesh.indexType})"
 func `$`*(mesh: Mesh): string =
   $mesh[]
 
@@ -82,15 +94,6 @@
     of Small: VK_INDEX_TYPE_UINT16
     of Big: VK_INDEX_TYPE_UINT32
 
-func indicesCount*(mesh: MeshObject): int =
-  (
-    case mesh.indexType
-    of None: 0
-    of Tiny: mesh.tinyIndices.len
-    of Small: mesh.smallIndices.len
-    of Big: mesh.bigIndices.len
-  ) * 3
-
 proc initVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) =
   assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute)
   mesh.vertexData[attribute] = newDataList(thetype=getDataType[T]())
--- a/src/semicongine/resources/mesh.nim	Wed Sep 20 23:25:44 2023 +0700
+++ b/src/semicongine/resources/mesh.nim	Thu Sep 21 23:34:38 2023 +0700
@@ -236,7 +236,7 @@
   if primitiveNode.hasKey("indices"):
     assert mesh[].indexType != None
     let data = root.getAccessorData(root["accessors"][primitiveNode["indices"].getInt()], mainBuffer)
-    let baseIndex = mesh[].indicesCount
+    let baseIndex = mesh[].vertexCount - vertexCount
     var tri: seq[int]
     case data.thetype
       of UInt16:
@@ -248,7 +248,7 @@
             tri.setLen(0)
       of UInt32:
         for entry in getValues[uint32](data)[]:
-          tri.add int(entry)
+          tri.add int(entry) + baseIndex
           if tri.len == 3:
             # FYI gltf uses counter-clockwise indexing
             mesh[].appendIndicesData(tri[0], tri[1], tri[2])