# HG changeset patch
# User Sam <sam@basx.dev>
# Date 1695314078 -25200
# Node ID 72913d68c07b284c54bf6bc02faaf2ca17228a3f
# Parent  670f598d59e79b86750bb99279159457b03d03b7
fix: indexing wrong

diff -r 670f598d59e7 -r 72913d68c07b src/semicongine/mesh.nim
--- 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]())
diff -r 670f598d59e7 -r 72913d68c07b src/semicongine/resources/mesh.nim
--- 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])