diff examples/hello_triangle.nim @ 482:1670f8e70964

add: clean examples, update build configs
author Sam <sam@basx.dev>
date Tue, 10 Jan 2023 00:24:37 +0700
parents 14e5151f68d1
children b4a972bd37d5
line wrap: on
line diff
--- a/examples/hello_triangle.nim	Tue Jan 10 00:23:40 2023 +0700
+++ b/examples/hello_triangle.nim	Tue Jan 10 00:24:37 2023 +0700
@@ -6,58 +6,45 @@
 import zamikongine/shader
 
 type
+  # define type of vertex
   VertexDataA = object
-    position11: VertexAttribute[Vec2[float32]]
-    color22: VertexAttribute[Vec3[float32]]
+    position: VertexAttribute[Vec2[float32]]
+    color: VertexAttribute[Vec3[float32]]
+
+# vertex data (types must match the above VertexAttributes)
+const
+  triangle_pos = @[
+    Vec2([-0.5'f32, -0.5'f32]),
+    Vec2([ 0.5'f32,  0.5'f32]),
+    Vec2([-0.5'f32,  0.5'f32]),
+  ]
+  triangle_color = @[
+    Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
+  ]
 
 when isMainModule:
   var myengine = igniteEngine()
-  var mymesh1 = new Mesh[VertexDataA]
-  mymesh1.vertexData = VertexDataA(
-    position11: VertexAttribute[Vec2[float32]](
-      data: @[
-        Vec2([-0.5'f32, -0.5'f32]),
-        Vec2([ 0.5'f32,  0.5'f32]),
-        Vec2([-0.5'f32,  0.5'f32]),
-      ]
-    ),
-    color22: VertexAttribute[Vec3[float32]](
-      data: @[
-        Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
-      ]
-    )
+
+  # build a mesh
+  var trianglemesh = new Mesh[VertexDataA]
+  trianglemesh.vertexData = VertexDataA(
+    position: VertexAttribute[Vec2[float32]](data: triangle_pos),
+    color: VertexAttribute[Vec3[float32]](data: triangle_color),
   )
-  var mymesh2 = new IndexedMesh[VertexDataA, uint16]
-  mymesh2.vertexData = VertexDataA(
-    position11: VertexAttribute[Vec2[float32]](
-      data: @[
-        Vec2([ 0.0'f32, -0.7'f32]),
-        Vec2([ 0.6'f32,  0.1'f32]),
-        Vec2([ 0.3'f32,  0.4'f32]),
-      ]
-    ),
-    color22: VertexAttribute[Vec3[float32]](
-      data: @[
-        Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
-      ]
-    )
-  )
-  mymesh2.indices = @[[0'u16, 1'u16, 2'u16]]
-  var athing = new Thing
-  athing.parts.add mymesh1
-  var childthing = new Thing
-  childthing.parts.add mymesh2
-  athing.children.add childthing
+  # build a single-object scene graph
+  var triangle = new Thing
+  # add the triangle mesh to the object
+  triangle.parts.add trianglemesh
 
+  # upload data, prepare shaders, etc
   setupPipeline[VertexDataA, uint16](
     myengine,
-    athing,
-    generateVertexShaderCode[VertexDataA]("main", "position11", "color22"),
+    triangle,
+    generateVertexShaderCode[VertexDataA]("main", "position", "color"),
     generateFragmentShaderCode[VertexDataA]("main"),
   )
+  # show something
   myengine.fullThrottle()
   myengine.trash()