changeset 1027:d6c27f0ed3e4

fix: examples not compiling
author sam <sam@basx.dev>
date Wed, 22 May 2024 03:45:16 +0700
parents f7802c5069ce
children 2ba3f18e7cad
files examples/E01_hello_triangle.nim examples/E02_squares.nim examples/E03_hello_cube.nim examples/E04_input.nim semicongine/scene.nim
diffstat 5 files changed, 82 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E01_hello_triangle.nim	Mon May 20 19:34:47 2024 +0700
+++ b/examples/E01_hello_triangle.nim	Wed May 22 03:45:16 2024 +0700
@@ -5,6 +5,7 @@
 # shader setup
 const
   shaderConfiguration = createShaderConfiguration(
+    name = "default shader",
     inputs = [
       attr[Vec3f]("position"),
       attr[Vec4f]("color"),
@@ -29,7 +30,7 @@
 myengine.initRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration}, inFlightFrames = 2)
 myengine.loadScene(scene)
 
-while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
+while myengine.UpdateInputs() and not KeyWasPressed(Escape):
   transform[Vec3f](scene.meshes[0][], "position", scale(1.001, 1.001))
   myengine.renderScene(scene)
 
--- a/examples/E02_squares.nim	Mon May 20 19:34:47 2024 +0700
+++ b/examples/E02_squares.nim	Wed May 22 03:45:16 2024 +0700
@@ -2,7 +2,7 @@
 import std/tables
 import std/random
 
-import ../src/semicongine
+import ../semicongine
 
 
 when isMainModule:
@@ -44,15 +44,16 @@
 
   const
     shaderConfiguration = createShaderConfiguration(
-      inputs=[
+      name = "default shader",
+      inputs = [
         attr[Vec3f]("position"),
-        attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
+        attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
         attr[uint32]("index"),
       ],
-      intermediates=[attr[Vec4f]("outcolor")],
-      uniforms=[attr[float32]("time")],
-      outputs=[attr[Vec4f]("color")],
-      vertexCode="""
+      intermediates = [attr[Vec4f]("outcolor")],
+      uniforms = [attr[float32]("time")],
+      outputs = [attr[Vec4f]("color")],
+      vertexCode = """
 float pos_weight = index / 100.0; // add some gamma correction?
 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
 float v = min(1, max(0, pow(pos_weight - t, 2)));
@@ -60,23 +61,28 @@
 outcolor = vec4(color.r, color.g, v * 0.5, 1);
 gl_Position = vec4(position, 1.0);
 """,
-      fragmentCode="color = outcolor;",
+      fragmentCode = "color = outcolor;",
     )
+  let matDef = MaterialType(name: "default", vertexAttributes: {
+    "position": Vec3F32,
+    "color": Vec4F32,
+    "index": UInt32,
+  }.toTable)
   var squaremesh = newMesh(
-    positions=vertices,
-    indices=indices,
-    colors=colors,
-    material=Material(name: "default")
+    positions = vertices,
+    indices = indices,
+    colors = colors,
   )
   squaremesh[].initVertexAttribute("index", iValues.toSeq)
+  squaremesh.material = matDef.initMaterialData(name = "default")
 
   var myengine = initEngine("Squares")
-  myengine.initRenderer({"default": shaderConfiguration}.toTable)
+  myengine.initRenderer({matDef: shaderConfiguration})
 
   var scene = Scene(name: "scene", meshes: @[squaremesh])
   scene.addShaderGlobal("time", 0.0'f32)
-  myengine.addScene(scene)
-  while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
+  myengine.loadScene(scene)
+  while myengine.UpdateInputs() and not KeyWasPressed(Escape):
     scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
     myengine.renderScene(scene)
 
--- a/examples/E03_hello_cube.nim	Mon May 20 19:34:47 2024 +0700
+++ b/examples/E03_hello_cube.nim	Wed May 22 03:45:16 2024 +0700
@@ -1,17 +1,7 @@
-#
-#   TODO: Needs Depth-Buffer first!
-#
-#
-#
-#
-#
-#
-
-
 import std/tables
 import std/times
 
-import ../src/semicongine
+import ../semicongine
 
 const
   TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
@@ -24,11 +14,11 @@
   BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32)
 const
   cube_pos = @[
-    TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
-    TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
-    TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
-    TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
-    TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
+    TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront,     # front
+    TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack,         # back
+    TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack,         # left
+    TopRightBack, TopRightFront, BottomRightFront, BottomRightBack,     # right
+    TopLeftBack, TopRightBack, TopRightFront, TopLeftFront,             # top
     BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
   ]
   R = newVec4f(1, 0, 0, 1)
@@ -54,34 +44,36 @@
 
   const
     shaderConfiguration = createShaderConfiguration(
-      inputs=[
+      name = "default shader",
+      inputs = [
         attr[Vec3f]("position"),
-        attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
+        attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
       ],
-      intermediates=[attr[Vec4f]("outcolor")],
-      uniforms=[
+      intermediates = [attr[Vec4f]("outcolor")],
+      uniforms = [
         attr[Mat4]("projection"),
         attr[Mat4]("view"),
         attr[Mat4]("model"),
       ],
-      outputs=[attr[Vec4f]("color")],
-      vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
-      fragmentCode="color = outcolor;",
+      outputs = [attr[Vec4f]("color")],
+      vertexCode = """outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
+      fragmentCode = "color = outcolor;",
     )
-  myengine.initRenderer({"default": shaderConfiguration}.toTable)
-  var cube = Scene(name: "scene", meshes: @[newMesh(positions=cube_pos, indices=tris, colors=cube_color, material=Material(name: "default"))])
+  var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable)
+  var cube = Scene(name: "scene", meshes: @[newMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.initMaterialData(name = "default"))])
   cube.addShaderGlobal("projection", Unit4f32)
   cube.addShaderGlobal("view", Unit4f32)
   cube.addShaderGlobal("model", Unit4f32)
-  myengine.addScene(cube)
+  myengine.initRenderer({matDef: shaderConfiguration})
+  myengine.loadScene(cube)
 
   var t: float32 = cpuTime()
-  while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
+  while myengine.UpdateInputs() and not KeyWasPressed(Escape):
     setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32))
     setShaderGlobal(cube, "projection",
       perspective(
         float32(PI / 4),
-        float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
+        float32(myengine.GetWindow().size[0]) / float32(myengine.GetWindow().size[1]),
         0.1'f32,
         100'f32
       )
--- a/examples/E04_input.nim	Mon May 20 19:34:47 2024 +0700
+++ b/examples/E04_input.nim	Wed May 22 03:45:16 2024 +0700
@@ -3,7 +3,7 @@
 import std/typetraits
 import std/math
 
-import ../src/semicongine
+import ../semicongine
 
 const
   arrow = @[
@@ -108,33 +108,36 @@
 
   # define mesh objects
   var
-    material = Material(name: "default")
+    matDef = MaterialType(name: "default", vertexAttributes: {
+      "position": Vec3F32,
+      "color": Vec4F32,
+    }.toTable)
     cursormesh = newMesh(
-      positions=positions,
-      colors=arrow_colors,
-      material=material,
+      positions = positions,
+      colors = arrow_colors,
+      material = matDef.initMaterialData(),
     )
     keyboardmesh = newMesh(
-      positions=keyvertexpos,
-      colors=keyvertexcolor,
-      indices=keymeshindices,
-      material=material,
+      positions = keyvertexpos,
+      colors = keyvertexcolor,
+      indices = keymeshindices,
+      material = matDef.initMaterialData(),
     )
     backgroundmesh = newMesh(
-      positions= @[
+      positions = @[
         newVec3f(0'f32, 0'f32),
         newVec3f(1'f32, 0'f32),
         newVec3f(1'f32, 1'f32),
         newVec3f(0'f32, 1'f32),
       ],
-      colors= @[
+      colors = @[
         backgroundColor,
         backgroundColor,
         backgroundColor,
         backgroundColor,
       ],
-      indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
-      material=material,
+      indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
+      material = matDef.initMaterialData(),
     )
 
   # define mesh objects
@@ -148,51 +151,52 @@
   # shaders
   const
     shaderConfiguration = createShaderConfiguration(
-      inputs=[
+      name = "default shader",
+      inputs = [
         attr[Vec3f]("position"),
-        attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
-        attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
+        attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
+        attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
       ],
-      intermediates=[attr[Vec4f]("outcolor")],
-      uniforms=[attr[Mat4]("projection")],
-      outputs=[attr[Vec4f]("color")],
-      vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
-      fragmentCode="color = outcolor;",
+      intermediates = [attr[Vec4f]("outcolor")],
+      uniforms = [attr[Mat4]("projection")],
+      outputs = [attr[Vec4f]("color")],
+      vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
+      fragmentCode = "color = outcolor;",
     )
 
   # set up rendering
-  myengine.initRenderer({"default": shaderConfiguration}.toTable)
+  myengine.initRenderer({matDef: shaderConfiguration})
   scene.addShaderGlobal("projection", Unit4f32)
-  myengine.addScene(scene)
-  myengine.hideSystemCursor()
+  myengine.loadScene(scene)
+  myengine.HideSystemCursor()
 
   # mainloop
-  while myengine.updateInputs() == Running:
-    if myengine.windowWasResized():
+  while myengine.UpdateInputs():
+    if WindowWasResized():
       scene.setShaderGlobal("projection",
         ortho(
-          0, float32(myengine.getWindow().size[0]),
-          0, float32(myengine.getWindow().size[1]),
+          0, float32(myengine.GetWindow().size[0]),
+          0, float32(myengine.GetWindow().size[1]),
           0, 1,
         )
       )
       let
-        winsize = myengine.getWindow().size
+        winsize = myengine.GetWindow().size
         center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
       keyboardmesh.transform = keyboard_center * center
       backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32)
 
-    let mousePos = translate(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
+    let mousePos = translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32)
     cursormesh.transform = mousePos
 
     for (index, key) in enumerate(keyIndices):
-      if myengine.keyWasPressed(key):
+      if KeyWasPressed(key):
         let baseIndex = index * 4
         keyboardmesh["color", baseIndex + 0] = activeColor
         keyboardmesh["color", baseIndex + 1] = activeColor
         keyboardmesh["color", baseIndex + 2] = activeColor
         keyboardmesh["color", baseIndex + 3] = activeColor
-      if myengine.keyWasReleased(key):
+      if KeyWasReleased(key):
         let baseIndex = index * 4
         keyboardmesh["color", baseIndex + 0] = baseColor
         keyboardmesh["color", baseIndex + 1] = baseColor
--- a/semicongine/scene.nim	Mon May 20 19:34:47 2024 +0700
+++ b/semicongine/scene.nim	Wed May 22 03:45:16 2024 +0700
@@ -51,11 +51,11 @@
 proc addShaderGlobal*[T](scene: var Scene, name: string, data: T) =
   scene.addShaderGlobalArray(name, [data])
 
-func getShaderGlobalArray*[T](scene: Scene, name: string): ref seq[T] =
+proc getShaderGlobalArray*[T](scene: Scene, name: string): ref seq[T] =
   scene.shaderGlobals[name][T]
 
-func getShaderGlobal*[T](scene: Scene, name: string): T =
-  scene.getShaderGlobalArray(name)[][0]
+proc getShaderGlobal*[T](scene: Scene, name: string): T =
+  getShaderGlobalArray[T](scene, name)[][0]
 
 proc setShaderGlobalArray*[T](scene: var Scene, name: string, value: openArray[T]) =
   if scene.shaderGlobals[name, T][] == @value: