changeset 59:d7d9420ba675

did: use new vector and matrix names for simpler code
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 16:53:37 +0700
parents 8287a91e5d56
children c57285d292b6
files examples/alotof_triangles.nim examples/hello_cube.nim examples/hello_triangle.nim examples/input.nim examples/squares.nim src/semicongine/math/matrix.nim src/semicongine/math/vector.nim
diffstat 7 files changed, 88 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/examples/alotof_triangles.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/examples/alotof_triangles.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -8,15 +8,15 @@
 
 type
   VertexDataA = object
-    position11: PositionAttribute[TVec2[float32]]
-    color22: ColorAttribute[TVec3[float32]]
+    position11: PositionAttribute[Vec2]
+    color22: ColorAttribute[Vec3]
   Uniforms = object
     dt: Descriptor[float32]
 
 proc globalUpdate(engine: var Engine, dt: float32) =
   discard
 
-proc randomtransform(): TMat33[float32] =
+proc randomtransform(): Mat33 =
   let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5))
   let randomrotate = rotate2d(float32(rand(2 * PI)))
   let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8))
@@ -26,42 +26,42 @@
   randomize()
   var myengine = igniteEngine("A lot of triangles")
   const baseTriangle = [
-    TVec3([-0.1'f32, -0.1'f32, 1'f32]),
-    TVec3([ 0.1'f32,  0.1'f32, 1'f32]),
-    TVec3([-0.1'f32,  0.1'f32, 1'f32]),
+    Vec3([-0.1'f32, -0.1'f32, 1'f32]),
+    Vec3([ 0.1'f32,  0.1'f32, 1'f32]),
+    Vec3([-0.1'f32,  0.1'f32, 1'f32]),
   ]
 
   var scene = new Thing
 
   for i in 1 .. 300:
     var randommesh = new Mesh[VertexDataA]
-    let randomcolor1 = TVec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
+    let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
     let transform1 = randomtransform()
     randommesh.vertexData = VertexDataA(
-      position11: PositionAttribute[TVec2[float32]](
+      position11: PositionAttribute[Vec2](
         data: @[
-          TVec2[float32](transform1 * baseTriangle[0]),
-          TVec2[float32](transform1 * baseTriangle[1]),
-          TVec2[float32](transform1 * baseTriangle[2]),
+          Vec2(transform1 * baseTriangle[0]),
+          Vec2(transform1 * baseTriangle[1]),
+          Vec2(transform1 * baseTriangle[2]),
         ]
       ),
-      color22: ColorAttribute[TVec3[float32]](
+      color22: ColorAttribute[Vec3](
         data: @[randomcolor1, randomcolor1, randomcolor1]
       )
     )
 
-    let randomcolor2 = TVec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
+    let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
     let transform2 = randomtransform()
     var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
     randomindexedmesh.vertexData = VertexDataA(
-      position11: PositionAttribute[TVec2[float32]](
+      position11: PositionAttribute[Vec2](
         data: @[
-          TVec2[float32](transform2 * baseTriangle[0]),
-          TVec2[float32](transform2 * baseTriangle[1]),
-          TVec2[float32](transform2 * baseTriangle[2]),
+          Vec2(transform2 * baseTriangle[0]),
+          Vec2(transform2 * baseTriangle[1]),
+          Vec2(transform2 * baseTriangle[2]),
         ]
       ),
-      color22: ColorAttribute[TVec3[float32]](
+      color22: ColorAttribute[Vec3](
         data: @[randomcolor2, randomcolor2, randomcolor2]
       )
     )
--- a/examples/hello_cube.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/examples/hello_cube.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -17,12 +17,12 @@
 type
   # define type of vertex
   VertexDataA = object
-    position: PositionAttribute[TVec3[float32]]
-    color: ColorAttribute[TVec3[float32]]
+    position: PositionAttribute[Vec3]
+    color: ColorAttribute[Vec3]
   Uniforms = object
-    model: Descriptor[TMat44[float32]]
-    view: Descriptor[TMat44[float32]]
-    projection: Descriptor[TMat44[float32]]
+    model: Descriptor[Mat44]
+    view: Descriptor[Mat44]
+    projection: Descriptor[Mat44]
 
 var
   pipeline: RenderPipeline[VertexDataA, Uniforms]
@@ -36,7 +36,7 @@
   uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32) #  * rotate3d(float32(PI), Yf32)
 
   uniforms.view.value = Unit44f32
-  uniforms.projection.value = TMat44[float32](data:[
+  uniforms.projection.value = Mat44(data:[
     ratio, 0'f32, 0'f32, 0'f32,
     0'f32, 1'f32, 0'f32, 0'f32,
     0'f32, 0'f32, 1'f32, 0'f32,
@@ -47,14 +47,14 @@
     buffer.updateData(uniforms)
 
 const
-  TopLeftFront =     TVec3([ -0.5'f32, -0.5'f32, -0.5'f32])
-  TopRightFront =    TVec3([  0.5'f32, -0.5'f32, -0.5'f32])
-  BottomRightFront = TVec3([  0.5'f32,  0.5'f32, -0.5'f32])
-  BottomLeftFront =  TVec3([ -0.5'f32,  0.5'f32, -0.5'f32])
-  TopLeftBack =      TVec3([  0.5'f32, -0.5'f32,  0.5'f32])
-  TopRightBack =     TVec3([ -0.5'f32, -0.5'f32,  0.5'f32])
-  BottomRightBack =  TVec3([ -0.5'f32,  0.5'f32,  0.5'f32])
-  BottomLeftBack =   TVec3([  0.5'f32,  0.5'f32,  0.5'f32])
+  TopLeftFront =     Vec3([ -0.5'f32, -0.5'f32, -0.5'f32])
+  TopRightFront =    Vec3([  0.5'f32, -0.5'f32, -0.5'f32])
+  BottomRightFront = Vec3([  0.5'f32,  0.5'f32, -0.5'f32])
+  BottomLeftFront =  Vec3([ -0.5'f32,  0.5'f32, -0.5'f32])
+  TopLeftBack =      Vec3([  0.5'f32, -0.5'f32,  0.5'f32])
+  TopRightBack =     Vec3([ -0.5'f32, -0.5'f32,  0.5'f32])
+  BottomRightBack =  Vec3([ -0.5'f32,  0.5'f32,  0.5'f32])
+  BottomLeftBack =   Vec3([  0.5'f32,  0.5'f32,  0.5'f32])
 const
   cube_pos = @[
     TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
@@ -97,8 +97,8 @@
   # build a mesh
   var trianglemesh = new IndexedMesh[VertexDataA, uint16]
   trianglemesh.vertexData = VertexDataA(
-    position: PositionAttribute[TVec3[float32]](data: cube_pos),
-    color: ColorAttribute[TVec3[float32]](data: cube_color),
+    position: PositionAttribute[Vec3](data: cube_pos),
+    color: ColorAttribute[Vec3](data: cube_color),
   )
   trianglemesh.indices = tris
   # build a single-object scene graph
--- a/examples/hello_triangle.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/examples/hello_triangle.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -7,9 +7,9 @@
 type
   # define type of vertex
   VertexDataA = object
-    position: PositionAttribute[TVec2[float32]]
-    color: ColorAttribute[TVec3[float32]]
-    id: InstanceAttribute[TVec3[float32]]
+    position: PositionAttribute[Vec2]
+    color: ColorAttribute[Vec3]
+    id: InstanceAttribute[Vec3]
 
 var pipeline: RenderPipeline[VertexDataA, void]
 
@@ -19,14 +19,14 @@
 # vertex data (types must match the above VertexAttributes)
 const
   triangle_pos = @[
-    TVec2([ 0.0'f32, -0.5'f32]),
-    TVec2([ 0.5'f32,  0.5'f32]),
-    TVec2([-0.5'f32,  0.5'f32]),
+    Vec2([ 0.0'f32, -0.5'f32]),
+    Vec2([ 0.5'f32,  0.5'f32]),
+    Vec2([-0.5'f32,  0.5'f32]),
   ]
   triangle_color = @[
-    TVec3([1.0'f32, 0.0'f32, 0.0'f32]),
-    TVec3([0.0'f32, 1.0'f32, 0.0'f32]),
-    TVec3([0.0'f32, 0.0'f32, 1.0'f32]),
+    Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 0.0'f32, 1.0'f32]),
   ]
 
 when isMainModule:
@@ -35,9 +35,9 @@
   # build a mesh
   var trianglemesh = new Mesh[VertexDataA]
   trianglemesh.vertexData = VertexDataA(
-    position: PositionAttribute[TVec2[float32]](data: triangle_pos),
-    color: ColorAttribute[TVec3[float32]](data: triangle_color),
-    id: InstanceAttribute[TVec3[float32]](data: @[TVec3[float32]([0.5'f32, 0.5'f32, 0.5'f32])]),
+    position: PositionAttribute[Vec2](data: triangle_pos),
+    color: ColorAttribute[Vec3](data: triangle_color),
+    id: InstanceAttribute[Vec3](data: @[Vec3([0.5'f32, 0.5'f32, 0.5'f32])]),
   )
   # build a single-object scene graph
   var triangle = new Thing
--- a/examples/input.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/examples/input.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -7,12 +7,12 @@
 type
   # define type of vertex
   VertexDataA = object
-    position: PositionAttribute[TVec2[float32]]
-    color: ColorAttribute[TVec3[float32]]
+    position: PositionAttribute[Vec2]
+    color: ColorAttribute[Vec3]
     iscursor: GenericAttribute[int32]
   Uniforms = object
-    projection: Descriptor[TMat44[float32]]
-    cursor: Descriptor[TVec2[float32]]
+    projection: Descriptor[Mat44]
+    cursor: Descriptor[Vec2]
 
 var
   pipeline: RenderPipeline[VertexDataA, Uniforms]
@@ -35,20 +35,20 @@
 # vertex data (types must match the above VertexAttributes)
 const
   shape = @[
-    TVec2([-  1'f32, -  1'f32]),
-    TVec2([   1'f32, -  1'f32]),
-    TVec2([-0.3'f32, -0.3'f32]),
-    TVec2([-0.3'f32, -0.3'f32]),
-    TVec2([-  1'f32,    1'f32]),
-    TVec2([-  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]),
   ]
   colors = @[
-    TVec3([1'f32, 0'f32, 0'f32]),
-    TVec3([1'f32, 0'f32, 0'f32]),
-    TVec3([1'f32, 0'f32, 0'f32]),
-    TVec3([0.8'f32, 0'f32, 0'f32]),
-    TVec3([0.8'f32, 0'f32, 0'f32]),
-    TVec3([0.8'f32, 0'f32, 0'f32]),
+    Vec3([1'f32, 0'f32, 0'f32]),
+    Vec3([1'f32, 0'f32, 0'f32]),
+    Vec3([1'f32, 0'f32, 0'f32]),
+    Vec3([0.8'f32, 0'f32, 0'f32]),
+    Vec3([0.8'f32, 0'f32, 0'f32]),
+    Vec3([0.8'f32, 0'f32, 0'f32]),
   ]
 
 when isMainModule:
@@ -58,8 +58,8 @@
   var cursor = new Thing
   var cursormesh = new Mesh[VertexDataA]
   cursormesh.vertexData = VertexDataA(
-    position: PositionAttribute[TVec2[float32]](data: shape),
-    color: ColorAttribute[TVec3[float32]](data: colors),
+    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 the cursor a bit to make it look nice
@@ -71,15 +71,15 @@
       scale2d(0.5'f32, 1'f32) *
       rotate2d(float32(PI) / 4'f32)
     )
-    let pos = TVec3[float32]([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[TVec2[float32]](data: shape),
-    color: ColorAttribute[TVec3[float32]](data: colors),
+    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]),
   )
 
--- a/examples/squares.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/examples/squares.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -8,8 +8,8 @@
 
 type
   VertexDataA = object
-    position11: PositionAttribute[TVec2[float32]]
-    color22: ColorAttribute[TVec3[float32]]
+    position11: PositionAttribute[Vec2]
+    color22: ColorAttribute[Vec3]
     index: GenericAttribute[uint32]
   Uniforms = object
     t: Descriptor[float32]
@@ -32,8 +32,8 @@
     WIDTH = 2'f32 / COLUMNS
     HEIGHT = 2'f32 / ROWS
   var
-    vertices: array[COLUMNS * ROWS * 4, TVec2[float32]]
-    colors: array[COLUMNS * ROWS * 4, TVec3[float32]]
+    vertices: array[COLUMNS * ROWS * 4, Vec2]
+    colors: array[COLUMNS * ROWS * 4, Vec3]
     iValues: array[COLUMNS * ROWS * 4, uint32]
     indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
 
@@ -42,13 +42,13 @@
       let
         y: float32 = (row * 2 / COLUMNS) - 1
         x: float32 = (col * 2 / ROWS) - 1
-        color = TVec3[float32]([(x + 1) / 2, (y + 1) / 2, 0'f32])
+        color = Vec3([(x + 1) / 2, (y + 1) / 2, 0'f32])
         squareIndex = row * COLUMNS + col
         vertIndex = squareIndex * 4
-      vertices[vertIndex + 0] = TVec2[float32]([x, y])
-      vertices[vertIndex + 1] = TVec2[float32]([x + WIDTH, y])
-      vertices[vertIndex + 2] = TVec2[float32]([x + WIDTH, y + HEIGHT])
-      vertices[vertIndex + 3] = TVec2[float32]([x, y + HEIGHT])
+      vertices[vertIndex + 0] = Vec2([x, y])
+      vertices[vertIndex + 1] = Vec2([x + WIDTH, y])
+      vertices[vertIndex + 2] = Vec2([x + WIDTH, y + HEIGHT])
+      vertices[vertIndex + 3] = Vec2([x, y + HEIGHT])
       colors[vertIndex + 0] = color
       colors[vertIndex + 1] = color
       colors[vertIndex + 2] = color
@@ -64,8 +64,8 @@
   type PIndexedMesh = ref IndexedMesh[VertexDataA, uint16] # required so we can use ctor with ref/on heap
   var squaremesh = PIndexedMesh(
     vertexData: VertexDataA(
-      position11: PositionAttribute[TVec2[float32]](data: @vertices),
-      color22: ColorAttribute[TVec3[float32]](data: @colors),
+      position11: PositionAttribute[Vec2](data: @vertices),
+      color22: ColorAttribute[Vec3](data: @colors),
       index: GenericAttribute[uint32](data: @iValues),
     ),
     indices: @indices
--- a/src/semicongine/math/matrix.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/src/semicongine/math/matrix.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -31,13 +31,13 @@
   TMat44*[T: SomeNumber] = object
     data*: array[16, T]
   TMat* = TMat22|TMat33|TMat44|TMat23|TMat32|TMat34|TMat43
-  Mat22 = TMat22[float32]
-  Mat23 = TMat22[float32]
-  Mat32 = TMat22[float32]
-  Mat33 = TMat22[float32]
-  Mat34 = TMat22[float32]
-  Mat43 = TMat22[float32]
-  Mat44 = TMat22[float32]
+  Mat22* = TMat22[float32]
+  Mat23* = TMat23[float32]
+  Mat32* = TMat32[float32]
+  Mat33* = TMat33[float32]
+  Mat34* = TMat34[float32]
+  Mat43* = TMat43[float32]
+  Mat44* = TMat44[float32]
 
 func unit22[T: SomeNumber](): auto {.compiletime.} = TMat22[T](data:[
   T(1), T(0),
--- a/src/semicongine/math/vector.nim	Fri Jan 20 16:36:52 2023 +0700
+++ b/src/semicongine/math/vector.nim	Fri Jan 20 16:53:37 2023 +0700
@@ -13,9 +13,9 @@
   TVec3*[T: SomeNumber] = array[3, T]
   TVec4*[T: SomeNumber] = array[4, T]
   TVec* = TVec2|TVec3|TVec4
-  Vec2 = TVec2[float32]
-  Vec3 = TVec3[float32]
-  Vec4 = TVec4[float32]
+  Vec2* = TVec2[float32]
+  Vec3* = TVec3[float32]
+  Vec4* = TVec4[float32]
 
 converter toVec2*[T: SomeNumber](orig: TVec3[T]|TVec4[T]): TVec2[T] =
   TVec2[T]([orig[0], orig[1]])