changeset 56:94d7eed3f118

did: cleanup main namespace, add: better coordinate handling in input example
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 00:41:55 +0700
parents be3ab2619c33
children 547f3a374271
files examples/alotof_triangles.nim examples/hello_cube.nim examples/hello_triangle.nim examples/input.nim examples/squares.nim src/semicongine.nim src/semicongine/math/matrix.nim
diffstat 7 files changed, 43 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/examples/alotof_triangles.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/examples/alotof_triangles.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -4,14 +4,7 @@
 import std/random
 import std/enumerate
 
-import semicongine/engine
-import semicongine/math/vector
-import semicongine/math/matrix
-import semicongine/vertex
-import semicongine/descriptor
-import semicongine/mesh
-import semicongine/thing
-import semicongine/shader
+import semicongine
 
 type
   VertexDataA = object
--- a/examples/hello_cube.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/examples/hello_cube.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -12,15 +12,7 @@
 import std/strutils
 import std/enumerate
 
-import semicongine/engine
-import semicongine/math/vector
-import semicongine/math/matrix
-import semicongine/vertex
-import semicongine/descriptor
-import semicongine/mesh
-import semicongine/thing
-import semicongine/shader
-import semicongine/buffer
+import semicongine
 
 type
   # define type of vertex
--- a/examples/hello_triangle.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/examples/hello_triangle.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -2,15 +2,7 @@
 import std/strutils
 import std/enumerate
 
-import semicongine/engine
-import semicongine/math/vector
-import semicongine/math/matrix
-import semicongine/vertex
-import semicongine/descriptor
-import semicongine/mesh
-import semicongine/thing
-import semicongine/shader
-import semicongine/buffer
+import semicongine
 
 type
   # define type of vertex
--- a/examples/input.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/examples/input.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -2,35 +2,33 @@
 import std/strutils
 import std/enumerate
 
-import semicongine/engine
-import semicongine/math/vector
-import semicongine/math/matrix
-import semicongine/vertex
-import semicongine/descriptor
-import semicongine/mesh
-import semicongine/thing
-import semicongine/shader
-import semicongine/buffer
+import semicongine
 
 type
   # define type of vertex
   VertexDataA = object
     position: PositionAttribute[Vec2[float32]]
     color: ColorAttribute[Vec3[float32]]
+    translate: InstanceAttribute[Vec2[float32]]
   Uniforms = object
+    projection: Descriptor[Mat44[float32]]
     cursor: Descriptor[Vec2[float32]]
-    aspect: Descriptor[float32]
 
 var
   pipeline: RenderPipeline[VertexDataA, Uniforms]
   uniforms: Uniforms
-uniforms.aspect.value = 1
 
 
 proc globalUpdate(engine: var Engine, dt: float32) =
-  uniforms.aspect.value = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width)
-  uniforms.cursor.value[0] = ((float32(engine.input.mouseX) / float32(engine.vulkan.frameDimension.width)) * 2'f32 ) - 1'f32
-  uniforms.cursor.value[1] = ((float32(engine.input.mouseY) / float32(engine.vulkan.frameDimension.height)) * 2'f32 ) - 1'f32
+  uniforms.cursor.value[0] = float32(engine.input.mouseX)
+  uniforms.cursor.value[1] = float32(engine.input.mouseY)
+  uniforms.projection.value = ortho[float32](
+    0'f32, float32(engine.vulkan.frameDimension.width),
+    0'f32, float32(engine.vulkan.frameDimension.height),
+    0'f32, 1'f32,
+  )
+  echo uniforms.projection.value
+  # echo uniforms.projection
   for buffer in pipeline.uniformBuffers:
     buffer.updateData(uniforms)
 
@@ -62,11 +60,12 @@
   cursorpart.vertexData = VertexDataA(
     position: PositionAttribute[Vec2[float32]](data: shape),
     color: ColorAttribute[Vec3[float32]](data: colors),
+    translate: InstanceAttribute[Vec2[float32]](data: @[Vec2[float32]([100'f32, 100'f32])]),
   )
   # transform the cursor a bit to make it look nice
   for i in 0 ..< cursorpart.vertexData.position.data.len:
     let cursorscale = (
-      scale2d(0.07'f32, 0.07'f32) *
+      scale2d(20'f32, 20'f32) *
       translate2d(1'f32, 1'f32) *
       rotate2d(-float32(PI) / 4'f32) *
       scale2d(0.5'f32, 1'f32) *
@@ -81,8 +80,7 @@
 
   # upload data, prepare shaders, etc
   const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
-  out_position.x = in_position.x * uniforms.aspect + uniforms.cursor.x;
-  out_position.y = in_position.y + uniforms.cursor.y;
+    out_position = uniforms.projection * vec4(in_position + uniforms.cursor, 0, 1);
   """)
   const fragmentShader = generateFragmentShaderCode[VertexDataA]()
   echo vertexShader
--- a/examples/squares.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/examples/squares.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -4,15 +4,7 @@
 import std/random
 import std/enumerate
 
-import semicongine/engine
-import semicongine/math/vector
-import semicongine/math/matrix
-import semicongine/vertex
-import semicongine/descriptor
-import semicongine/mesh
-import semicongine/thing
-import semicongine/shader
-import semicongine/buffer
+import semicongine
 
 type
   VertexDataA = object
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/semicongine.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -0,0 +1,21 @@
+import semicongine/math/vector
+import semicongine/math/matrix
+import semicongine/engine
+import semicongine/vertex
+import semicongine/descriptor
+import semicongine/mesh
+import semicongine/thing
+import semicongine/shader
+import semicongine/buffer
+import semicongine/events
+
+export vector
+export matrix
+export engine
+export vertex
+export descriptor
+export mesh
+export thing
+export shader
+export buffer
+export events
--- a/src/semicongine/math/matrix.nim	Thu Jan 19 22:55:56 2023 +0700
+++ b/src/semicongine/math/matrix.nim	Fri Jan 20 00:41:55 2023 +0700
@@ -382,10 +382,10 @@
     T(0),                          T(0),               T(1),                     T(1),
   ])
 
-func ortho*[T: SomeFloat](left, right, bottom, top, zNear, zFar: T): Mat44[T] =
+func ortho*[T: SomeFloat](left, right, top, bottom, zNear, zFar: T): Mat44[T] =
   Mat44[T](data:[
     T(2) / (right - left), T(0),                  T(0),                  -(right + left) / (right - left),
-    T(0),                  T(2) / (top - bottom), T(0),                  -(top + bottom) / (top - bottom),
+    T(0),                  T(2) / (bottom - top), T(0),                  -(bottom + top) / (bottom - top),
     T(0),                  T(0),                  T(1) / (zFar - zNear), -zNear / (zFar - zNear),
-    T(0),                  T(0),                  T(1),                  T(1),
+    T(0),                  T(0),                  T(1),                   T(1),
   ])