annotate examples/E02_squares.nim @ 203:84fd522fdf3f

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 25d97fa0ad5c
children 6e2017cb8b8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/times
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
2 import std/sequtils
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/strutils
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/math
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/random
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
6
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
7 import semicongine
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 when isMainModule:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
11 randomize()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 const
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 COLUMNS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 ROWS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 WIDTH = 2'f32 / COLUMNS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
16 HEIGHT = 2'f32 / ROWS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
17 var
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
18 vertices: array[COLUMNS * ROWS * 4, Vec3f]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
19 colors: array[COLUMNS * ROWS * 4, Vec3f]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
20 iValues: array[COLUMNS * ROWS * 4, uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 for row in 0 ..< ROWS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24 for col in 0 ..< COLUMNS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25 let
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
26 y: float32 = (row * 2 / COLUMNS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 x: float32 = (col * 2 / ROWS) - 1
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
28 color = Vec3f([(x + 1) / 2, (y + 1) / 2, 0'f32])
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 squareIndex = row * COLUMNS + col
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 vertIndex = squareIndex * 4
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
31 vertices[vertIndex + 0] = newVec3f(x, y)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
32 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
33 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
34 vertices[vertIndex + 3] = newVec3f(x, y + HEIGHT)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 colors[vertIndex + 0] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
36 colors[vertIndex + 1] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 colors[vertIndex + 2] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 colors[vertIndex + 3] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39 iValues[vertIndex + 0] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 iValues[vertIndex + 1] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 iValues[vertIndex + 2] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 iValues[vertIndex + 3] = uint32(squareIndex)
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
43 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
44 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
45
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
46
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
47 const
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
48 vertexInput = @[
163
25d97fa0ad5c fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 148
diff changeset
49 attr[Vec3f]("position"),
25d97fa0ad5c fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 148
diff changeset
50 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
25d97fa0ad5c fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 148
diff changeset
51 attr[uint32]("index"),
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
52 ]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
53 vertexOutput = @[attr[Vec3f]("outcolor")]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
54 uniforms = @[attr[float32]("time")]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
55 fragOutput = @[attr[Vec4f]("color")]
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
56 vertexCode = compileGlslShader(
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
57 stage=VK_SHADER_STAGE_VERTEX_BIT,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
58 inputs=vertexInput,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
59 uniforms=uniforms,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
60 outputs=vertexOutput,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
61 main="""
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
62 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
63 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
64 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
65 v = pow(1 - v, 3000);
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
66 outcolor = vec3(color.r, color.g, v * 0.5);
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
67 gl_Position = vec4(position, 1.0);
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
68 """
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
69 )
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
70 fragmentCode = compileGlslShader(
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
71 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
72 inputs=vertexOutput,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
73 uniforms=uniforms,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
74 outputs=fragOutput,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
75 main="color = vec4(outcolor, 1);"
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
76 )
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
77 var squaremesh = newMesh(
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
78 positions=vertices,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
79 indices=indices,
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
80 colors=colors,
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
81 )
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
82 setMeshData[uint32](squaremesh, "index", iValues.toSeq)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
83
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
84 var myengine = initEngine("Squares")
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
85 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
86
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
87 var scene = newScene("scene", newEntity("scene", newEntity("squares", squaremesh)))
136
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
88 myengine.addScene(scene, vertexInput)
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
89 scene.addShaderGlobal("time", 0.0'f32)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 139
diff changeset
90 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
91 setShaderGlobal(scene, "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
92 myengine.renderScene(scene)
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
93
fc5db4f1f94e did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 74
diff changeset
94 myengine.destroy()