diff examples/input.nim @ 60:c57285d292b6

did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
author Sam <sam@basx.dev>
date Sun, 22 Jan 2023 22:46:53 +0700
parents d7d9420ba675
children
line wrap: on
line diff
--- a/examples/input.nim	Fri Jan 20 16:53:37 2023 +0700
+++ b/examples/input.nim	Sun Jan 22 22:46:53 2023 +0700
@@ -1,6 +1,5 @@
+import std/strutils
 import std/times
-import std/strutils
-import std/enumerate
 
 import semicongine
 
@@ -9,7 +8,12 @@
   VertexDataA = object
     position: PositionAttribute[Vec2]
     color: ColorAttribute[Vec3]
-    iscursor: GenericAttribute[int32]
+    # transform: ModelTransformAttribute
+    # TODO: make this somehow a single vertex attribute
+    m1: GenericInstanceAttribute[Vec4]
+    m2: GenericInstanceAttribute[Vec4]
+    m3: GenericInstanceAttribute[Vec4]
+    m4: GenericInstanceAttribute[Vec4]
   Uniforms = object
     projection: Descriptor[Mat44]
     cursor: Descriptor[Vec2]
@@ -17,30 +21,46 @@
 var
   pipeline: RenderPipeline[VertexDataA, Uniforms]
   uniforms: Uniforms
+  scene: Thing
+  time: float
 
 
 proc globalUpdate(engine: var Engine, dt: float32) =
-  uniforms.cursor.value[0] = float32(engine.input.mouseX)
-  uniforms.cursor.value[1] = float32(engine.input.mouseY)
+  time += dt
+  uniforms.cursor.value = engine.input.mousePos
   uniforms.projection.value = ortho[float32](
-    0'f32, float32(engine.vulkan.frameDimension.width),
-    0'f32, float32(engine.vulkan.frameDimension.height),
+    0'f32, float32(engine.vulkan.frameSize.x),
+    0'f32, float32(engine.vulkan.frameSize.y),
     0'f32, 1'f32,
   )
-  echo uniforms.projection.value
-  # echo uniforms.projection
-  for buffer in pipeline.uniformBuffers:
-    buffer.updateData(uniforms)
+  engine.vulkan.device.updateUniformData(pipeline, uniforms)
 
-# vertex data (types must match the above VertexAttributes)
+  let cursor = firstPartWithName[Mesh[VertexDataA]](scene, "cursor")
+  if cursor != nil:
+    for c in cursor.vertexData.color.data.mitems:
+      c[1] = (sin(time * 8) * 0.5 + 0.5) * 0.2
+      c[2] = (sin(time * 8) * 0.5 + 0.5) * 0.2
+    engine.vulkan.device.updateVertexData(cursor.vertexData.color)
+    var trans = Unit44 * translate3d(engine.input.mousePos.x,
+        engine.input.mousePos.y, 0'f32)
+    cursor.vertexData.m1.data = @[trans.col(0)]
+    cursor.vertexData.m2.data = @[trans.col(1)]
+    cursor.vertexData.m3.data = @[trans.col(2)]
+    cursor.vertexData.m4.data = @[trans.col(3)]
+    engine.vulkan.device.updateVertexData(cursor.vertexData.m1)
+    engine.vulkan.device.updateVertexData(cursor.vertexData.m2)
+    engine.vulkan.device.updateVertexData(cursor.vertexData.m3)
+    engine.vulkan.device.updateVertexData(cursor.vertexData.m4)
+
+
 const
   shape = @[
-    Vec2([-  1'f32, -  1'f32]),
-    Vec2([   1'f32, -  1'f32]),
+    Vec2([ - 1'f32, - 1'f32]),
+    Vec2([1'f32, - 1'f32]),
     Vec2([-0.3'f32, -0.3'f32]),
     Vec2([-0.3'f32, -0.3'f32]),
-    Vec2([-  1'f32,    1'f32]),
-    Vec2([-  1'f32, -  1'f32]),
+    Vec2([ - 1'f32, 1'f32]),
+    Vec2([ - 1'f32, - 1'f32]),
   ]
   colors = @[
     Vec3([1'f32, 0'f32, 0'f32]),
@@ -54,13 +74,15 @@
 when isMainModule:
   var myengine = igniteEngine("Input")
 
-  # build a single-object scene graph
-  var cursor = new Thing
   var cursormesh = new Mesh[VertexDataA]
   cursormesh.vertexData = VertexDataA(
-    position: PositionAttribute[Vec2](data: shape),
+    position: PositionAttribute[Vec2](data: shape, useOnDeviceMemory: true),
     color: ColorAttribute[Vec3](data: colors),
-    iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
+    # transform: ModelTransformAttribute(data: @[Unit44]),
+    m1: GenericInstanceAttribute[Vec4](data: @[Unit44.row(0)]),
+    m2: GenericInstanceAttribute[Vec4](data: @[Unit44.row(1)]),
+    m3: GenericInstanceAttribute[Vec4](data: @[Unit44.row(2)]),
+    m4: GenericInstanceAttribute[Vec4](data: @[Unit44.row(3)]),
   )
   # transform the cursor a bit to make it look nice
   for i in 0 ..< cursormesh.vertexData.position.data.len:
@@ -71,28 +93,40 @@
       scale2d(0.5'f32, 1'f32) *
       rotate2d(float32(PI) / 4'f32)
     )
-    let pos = Vec3([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32])
+    let pos = Vec3([cursormesh.vertexData.position.data[i][0],
+        cursormesh.vertexData.position.data[i][1], 1'f32])
     cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy
-  cursor.parts.add cursormesh
 
-  var box = new Thing
   var boxmesh = new Mesh[VertexDataA]
   boxmesh.vertexData = VertexDataA(
     position: PositionAttribute[Vec2](data: shape),
     color: ColorAttribute[Vec3](data: colors),
-    iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
+    # transform: ModelTransformAttribute(data: @[Unit44]),
+    m1: GenericInstanceAttribute[Vec4](data: @[Unit44.row(0)]),
+    m2: GenericInstanceAttribute[Vec4](data: @[Unit44.row(1)]),
+    m3: GenericInstanceAttribute[Vec4](data: @[Unit44.row(2)]),
+    m4: GenericInstanceAttribute[Vec4](data: @[Unit44.row(3)]),
   )
+  for i in 0 ..< boxmesh.vertexData.position.data.len:
+    let boxscale = translate2d(100'f32, 100'f32) * scale2d(100'f32, 100'f32)
+    let pos = Vec3([boxmesh.vertexData.position.data[i][0],
+        boxmesh.vertexData.position.data[i][1], 1'f32])
+    boxmesh.vertexData.position.data[i] = (boxscale * pos).xy
+  echo boxmesh.vertexData.position.data
 
-  var scene = new Thing
-  scene.children.add cursor
+  scene = newThing("scene")
+  scene.add newThing("cursor", cursormesh)
+  scene.add newThing("a box", boxmesh, newTransform(Unit44), newTransform(
+      translate3d(1'f32, 0'f32, 0'f32)))
+  scene.add newTransform(scale3d(1.5'f32, 1.5'f32, 1.5'f32))
 
   # upload data, prepare shaders, etc
   const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
-    out_position = uniforms.projection * vec4(in_position + (uniforms.cursor * iscursor), 0, 1);
+    mat4 mat = mat4(m1, m2, m3, m4);
+    out_position = uniforms.projection * mat * vec4(position, 0, 1);
   """)
+  echo vertexShader
   const fragmentShader = generateFragmentShaderCode[VertexDataA]()
-  echo vertexShader
-  echo fragmentShader
   pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
     myengine,
     scene,