changeset 664:c33c8e156e3e

did: update examples to use new API for scene + scene globals
author Sam <sam@basx.dev>
date Mon, 08 May 2023 21:21:49 +0700
parents af9ab61aa87e
children e85f54b88afb
files examples/E01_hello_triangle.nim examples/E02_squares.nim examples/E03_hello_cube.nim examples/E04_input.nim examples/E10_pong.nim
diffstat 5 files changed, 35 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E01_hello_triangle.nim	Mon May 08 00:48:11 2023 +0700
+++ b/examples/E01_hello_triangle.nim	Mon May 08 21:21:49 2023 +0700
@@ -25,13 +25,13 @@
   )
 
 var
-  triangle = newEntity(
+  triangle = newScene("scene", newEntity(
     "triangle",
     newMesh(
       [newVec3f(-0.5, 0.5), newVec3f(0, -0.5), newVec3f(0.5, 0.5)],
       [X, Y, Z],
     )
-  )
+  ))
   myengine = initEngine("Hello triangle")
   renderPass = myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)
 
--- a/examples/E02_squares.nim	Mon May 08 00:48:11 2023 +0700
+++ b/examples/E02_squares.nim	Mon May 08 21:21:49 2023 +0700
@@ -84,12 +84,11 @@
   var myengine = initEngine("Squares")
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
 
-  var scene = newEntity("scene", newEntity("squares", squaremesh))
+  var scene = newScene("scene", newEntity("scene", newEntity("squares", squaremesh)))
   myengine.addScene(scene, vertexInput)
-  var time = initShaderGlobal("time", 0.0'f32)
-  scene.components.add time
+  scene.addShaderGlobal("time", 0.0'f32)
   while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
-    setValue[float32](time.value, get[float32](time.value) + 0.0005)
+    setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
     myengine.renderScene(scene)
 
   myengine.destroy()
--- a/examples/E03_hello_cube.nim	Mon May 08 00:48:11 2023 +0700
+++ b/examples/E03_hello_cube.nim	Mon May 08 21:21:49 2023 +0700
@@ -76,21 +76,16 @@
       main="color = vec4(outcolor, 1);"
     )
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
-  var
-    projection = initShaderGlobal("projection", Unit4f32)
-    view = initShaderGlobal("view", Unit4f32)
-    model = initShaderGlobal("model", Unit4f32)
-    cube = newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color))
-  cube.components.add projection
-  cube.components.add view
-  cube.components.add model
+  var cube = newScene("scene", newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color)))
+  cube.addShaderGlobal("projection", Unit4f32)
+  cube.addShaderGlobal("view", Unit4f32)
+  cube.addShaderGlobal("model", Unit4f32)
   myengine.addScene(cube, vertexInput)
 
   var t: float32 = cpuTime()
   while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
-    setValue[Mat4](model.value, translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32))
-    setValue[Mat4](
-      projection.value,
+    setShaderGlobal(cube, "model", translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32))
+    setShaderGlobal(cube, "projection",
       perspective(
         float32(PI / 4),
         float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
--- a/examples/E04_input.nim	Mon May 08 00:48:11 2023 +0700
+++ b/examples/E04_input.nim	Mon May 08 21:21:49 2023 +0700
@@ -1,7 +1,5 @@
 import std/enumerate
-import std/strutils
 import std/typetraits
-import std/times
 import std/math
 
 import semicongine
@@ -58,7 +56,7 @@
 
 # build keyboard and cursor meshes
 var
-  scene: Entity
+  scene: Scene
   keyvertexpos: seq[Vec3f]
   keyvertexcolor: seq[Vec3f]
   keymeshindices: seq[array[3, uint16]]
@@ -136,16 +134,16 @@
     )
 
   # define mesh objects
-  scene = newEntity("scene")
-  scene.add newEntity("background", backgroundmesh)
+  scene = newScene("scene", newEntity("scene"))
+  scene.root.add newEntity("background", backgroundmesh)
   let keyboard = newEntity("keyboard", keyboardmesh)
   keyboard.transform = translate3d(
     -float32(rowWidth) / 2'f32,
     -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
     0'f32
   )
-  scene.add newEntity("keyboard-center", keyboard)
-  scene.add newEntity("cursor", cursormesh)
+  scene.root.add newEntity("keyboard-center", keyboard)
+  scene.root.add newEntity("cursor", cursormesh)
 
   # shaders
   const
@@ -175,27 +173,28 @@
   # set up rendering
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
   myengine.addScene(scene, vertexInput, transformAttribute="transform")
-  var projection = initShaderGlobal("projection", Unit4f32)
-  scene.add projection
+  scene.addShaderGlobal("projection", Unit4f32)
 
   # mainloop
   while myengine.updateInputs() == Running:
     if myengine.windowWasResized():
-      setValue[Mat4](projection.value, ortho[float32](
-        0'f32, float32(myengine.getWindow().size[0]),
-        0'f32, float32(myengine.getWindow().size[1]),
-        0'f32, 1'f32,
-      ))
+      setShaderGlobal(scene, "projection",
+        ortho[float32](
+          0'f32, float32(myengine.getWindow().size[0]),
+          0'f32, float32(myengine.getWindow().size[1]),
+          0'f32, 1'f32,
+        )
+      )
       let
         winsize = myengine.getWindow().size
         center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
-      scene.firstWithName("keyboard-center").transform = center
-      scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
+      scene.root.firstWithName("keyboard-center").transform = center
+      scene.root.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
 
     let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
-    scene.firstWithName("cursor").transform = mousePos
+    scene.root.firstWithName("cursor").transform = mousePos
 
-    var mesh = Mesh(scene.firstWithName("keyboard").components[0])
+    var mesh = Mesh(scene.root.firstWithName("keyboard").components[0])
     for (index, key) in enumerate(keyIndices):
       if myengine.keyWasPressed(key):
         let baseIndex = uint32(index * 4)
--- a/examples/E10_pong.nim	Mon May 08 00:48:11 2023 +0700
+++ b/examples/E10_pong.nim	Mon May 08 21:21:49 2023 +0700
@@ -7,29 +7,28 @@
   barSize = 0.1'f
   barWidth = 0.01'f
   ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
-  levelRatio = 1
   ballSize = 0.01'f
   backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
   ballSpeed = 60'f
 
 var
-  level: Entity
+  level: Scene
   ballVelocity = newVec2f(1, 1).normalized * ballSpeed
 
 when isMainModule:
   var myengine = initEngine("Pong")
-  level = newEntity("Level")
+  level = newScene("scene", newEntity("Level"))
   var playerbarmesh = rect(color=barcolor)
   var playerbar = newEntity("playerbar", playerbarmesh)
   playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
   var player = newEntity("player", playerbar)
   player.transform = translate3d(0'f, 0.3'f, 0'f)
-  level.add player
+  level.root.add player
 
   var ballmesh = circle(color=ballcolor)
   var ball = newEntity("ball", ballmesh)
   ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
-  level.add ball
+  level.root.add ball
 
   const
     vertexInput = @[
@@ -58,8 +57,7 @@
   # set up rendering
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
   myengine.addScene(level, vertexInput, transformAttribute="transform")
-  var projection = initShaderGlobal("projection", Unit4f32)
-  level.add projection
+  level.addShaderGlobal("projection", Unit4f32)
 
   var
     winsize = myengine.getWindow().size
@@ -85,8 +83,8 @@
       winsize = myengine.getWindow().size
       height = float32(winsize[1]) / float32(winsize[0])
       width = 1'f
-      setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
-    var player = level.firstWithName("player")
+      setShaderGlobal(level, "projection", ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
+    var player = level.root.firstWithName("player")
     if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
       player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
     if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: