changeset 481:c472abfcee57

add: generate ubermeshes
author Sam <sam@basx.dev>
date Tue, 10 Jan 2023 00:23:40 +0700
parents 14e5151f68d1
children 1670f8e70964
files src/zamikongine/engine.nim src/zamikongine/math/vector.nim src/zamikongine/mesh.nim
diffstat 3 files changed, 36 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/zamikongine/engine.nim	Mon Jan 09 11:04:19 2023 +0700
+++ b/src/zamikongine/engine.nim	Tue Jan 10 00:23:40 2023 +0700
@@ -502,10 +502,17 @@
     vertexShader,
     fragmentShader,
   )
+  var allmeshes: seq[Mesh[T]]
   for mesh in partsOfType[ref Mesh[T]](engine.currentscenedata):
-    engine.vulkan.vertexBuffers.add createVertexBuffers(mesh[], engine.vulkan.device.device, engine.vulkan.device.physicalDevice.device, engine.vulkan.commandPool, engine.vulkan.device.graphicsQueue)
+    allmeshes.add(mesh[])
+  var ubermesh = createUberMesh(allmeshes)
+  engine.vulkan.vertexBuffers.add createVertexBuffers(ubermesh, engine.vulkan.device.device, engine.vulkan.device.physicalDevice.device, engine.vulkan.commandPool, engine.vulkan.device.graphicsQueue)
+
+  var allindexedmeshes: seq[IndexedMesh[T, U]]
   for mesh in partsOfType[ref IndexedMesh[T, U]](engine.currentscenedata):
-    engine.vulkan.indexedVertexBuffers.add createIndexedVertexBuffers(mesh[], engine.vulkan.device.device, engine.vulkan.device.physicalDevice.device, engine.vulkan.commandPool, engine.vulkan.device.graphicsQueue)
+    allindexedmeshes.add(mesh[])
+  var indexedubermesh = createUberMesh(allindexedmeshes)
+  engine.vulkan.indexedVertexBuffers.add createIndexedVertexBuffers(indexedubermesh, engine.vulkan.device.device, engine.vulkan.device.physicalDevice.device, engine.vulkan.commandPool, engine.vulkan.device.graphicsQueue)
 
 proc recordCommandBuffer(renderPass: VkRenderPass, pipeline: VkPipeline, commandBuffer: VkCommandBuffer, framebuffer: VkFramebuffer, frameDimension: VkExtent2D, engine: var Engine) =
   var
--- a/src/zamikongine/math/vector.nim	Mon Jan 09 11:04:19 2023 +0700
+++ b/src/zamikongine/math/vector.nim	Tue Jan 10 00:23:40 2023 +0700
@@ -12,6 +12,11 @@
   Vec4*[T: SomeNumber] = array[4, T]
   Vec* = Vec2|Vec3|Vec4
 
+converter toVec2*[T: SomeNumber](orig: Vec3[T]|Vec4[T]): Vec2[T] =
+  Vec2[T]([orig[0], orig[1]])
+converter toVec3*[T: SomeNumber](orig: Vec4[T]): Vec3[T] =
+  Vec2[T]([orig[0], orig[1], orig[2]])
+
 # define some often used constants
 func ConstOne2[T: SomeNumber](): auto {.compiletime.} = Vec2[T]([T(1), T(1)])
 func ConstOne3[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(1), T(1)])
--- a/src/zamikongine/mesh.nim	Mon Jan 09 11:04:19 2023 +0700
+++ b/src/zamikongine/mesh.nim	Tue Jan 10 00:23:40 2023 +0700
@@ -12,6 +12,28 @@
     vertexData*: T
     indices*: seq[array[3, U]]
 
+func createUberMesh*[T](meshes: openArray[Mesh[T]]): Mesh[T] =
+  for mesh in meshes:
+    for srcname, srcvalue in mesh.vertexData.fieldPairs:
+      when typeof(srcvalue) is VertexAttribute:
+        for dstname, dstvalue in result.vertexData.fieldPairs:
+          when srcname == dstname:
+            dstvalue.data.add srcvalue.data
+
+func createUberMesh*[T: object, U: uint16|uint32](meshes: openArray[IndexedMesh[T, U]]): IndexedMesh[T, U] =
+  var indexoffset = U(0)
+  for mesh in meshes:
+    for srcname, srcvalue in mesh.vertexData.fieldPairs:
+      when typeof(srcvalue) is VertexAttribute:
+        for dstname, dstvalue in result.vertexData.fieldPairs:
+          when srcname == dstname:
+            dstvalue.data.add srcvalue.data
+      var indexdata: seq[array[3, U]]
+      for i in mesh.indices:
+        indexdata.add [i[0] + indexoffset, i[1] + indexoffset, i[2] + indexoffset]
+      result.indices.add indexdata
+    indexoffset += U(mesh.vertexData.VertexCount)
+
 func getVkIndexType[T: object, U: uint16|uint32](m: IndexedMesh[T, U]): VkIndexType =
   when U is uint16: VK_INDEX_TYPE_UINT16
   elif U is uint32: VK_INDEX_TYPE_UINT32