annotate examples/E02_squares.nim @ 452:efc537516478

did: correct usage of vSync/triple buffering
author Sam <sam@basx.dev>
date Sun, 10 Mar 2024 22:06:52 +0700
parents 887ddc8d45fd
children c66503386e8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
1 import std/sequtils
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
2 import std/tables
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/random
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
4
271
95281f2db400 did: fix API changes broke examples
Sam <sam@basx.dev>
parents: 253
diff changeset
5 import ../src/semicongine
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
6
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
7
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8 when isMainModule:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9 randomize()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 const
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
11 COLUMNS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 ROWS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 WIDTH = 2'f32 / COLUMNS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 HEIGHT = 2'f32 / ROWS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 var
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
16 vertices: array[COLUMNS * ROWS * 4, Vec3f]
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 204
diff changeset
17 colors: array[COLUMNS * ROWS * 4, Vec4f]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 iValues: array[COLUMNS * ROWS * 4, uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
20
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 for row in 0 ..< ROWS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22 for col in 0 ..< COLUMNS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 let
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24 y: float32 = (row * 2 / COLUMNS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25 x: float32 = (col * 2 / ROWS) - 1
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 204
diff changeset
26 color = newVec4f((x + 1) / 2, (y + 1) / 2, 0, 1)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 squareIndex = row * COLUMNS + col
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
28 vertIndex = squareIndex * 4
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
29 vertices[vertIndex + 0] = newVec3f(x, y)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
30 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
31 vertices[vertIndex + 2] = newVec3f(x + WIDTH, y + HEIGHT)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
32 vertices[vertIndex + 3] = newVec3f(x, y + HEIGHT)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
33 colors[vertIndex + 0] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
34 colors[vertIndex + 1] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 colors[vertIndex + 2] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
36 colors[vertIndex + 3] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 iValues[vertIndex + 0] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 iValues[vertIndex + 1] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39 iValues[vertIndex + 2] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 iValues[vertIndex + 3] = uint32(squareIndex)
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
41 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
42 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
43
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
44
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
45 const
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
46 shaderConfiguration = createShaderConfiguration(
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
47 inputs=[
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
48 attr[Vec3f]("position"),
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
49 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
50 attr[uint32]("index"),
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
51 ],
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
52 intermediates=[attr[Vec4f]("outcolor")],
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
53 uniforms=[attr[float32]("time")],
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
54 outputs=[attr[Vec4f]("color")],
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
55 vertexCode="""
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
56 float pos_weight = index / 100.0; // add some gamma correction?
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
57 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
58 float v = min(1, max(0, pow(pos_weight - t, 2)));
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
59 v = pow(1 - v, 3000);
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 204
diff changeset
60 outcolor = vec4(color.r, color.g, v * 0.5, 1);
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
61 gl_Position = vec4(position, 1.0);
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
62 """,
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
63 fragmentCode="color = outcolor;",
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
64 )
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
65 var squaremesh = newMesh(
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
66 positions=vertices,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
67 indices=indices,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
68 colors=colors,
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
69 material=Material(name: "default")
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
70 )
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
71 squaremesh[].initVertexAttribute("index", iValues.toSeq)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
72
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
73 var myengine = initEngine("Squares")
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
74 myengine.initRenderer({"default": shaderConfiguration}.toTable)
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
75
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
76 var scene = Scene(name: "scene", meshes: @[squaremesh])
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
77 scene.addShaderGlobal("time", 0.0'f32)
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
78 myengine.addScene(scene)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 139
diff changeset
79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
336
887ddc8d45fd did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 316
diff changeset
80 scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
81 myengine.renderScene(scene)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
82
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
83 myengine.destroy()