annotate examples/squares.nim @ 59:d7d9420ba675

did: use new vector and matrix names for simpler code
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 16:53:37 +0700
parents 547f3a374271
children c57285d292b6
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
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/math
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/enumerate
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 type
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 VertexDataA = object
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
11 position11: PositionAttribute[Vec2]
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
12 color22: ColorAttribute[Vec3]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 index: GenericAttribute[uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 Uniforms = object
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 t: Descriptor[float32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
16
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
17 var
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 pipeline: RenderPipeline[VertexDataA, Uniforms]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
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 proc globalUpdate(engine: var Engine, dt: float32) =
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22 uniformdata.t.value += dt
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 for buffer in pipeline.uniformBuffers:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24 buffer.updateData(uniformdata)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
26 when isMainModule:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 randomize()
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 33
diff changeset
28 var myengine = igniteEngine("Squares")
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 const
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 COLUMNS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
31 ROWS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
32 WIDTH = 2'f32 / COLUMNS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
33 HEIGHT = 2'f32 / ROWS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
34 var
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
35 vertices: array[COLUMNS * ROWS * 4, Vec2]
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
36 colors: array[COLUMNS * ROWS * 4, Vec3]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 iValues: array[COLUMNS * ROWS * 4, uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 for row in 0 ..< ROWS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 for col in 0 ..< COLUMNS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 let
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
43 y: float32 = (row * 2 / COLUMNS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
44 x: float32 = (col * 2 / ROWS) - 1
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
45 color = Vec3([(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
46 squareIndex = row * COLUMNS + col
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
47 vertIndex = squareIndex * 4
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
48 vertices[vertIndex + 0] = Vec2([x, y])
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
49 vertices[vertIndex + 1] = Vec2([x + WIDTH, y])
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
50 vertices[vertIndex + 2] = Vec2([x + WIDTH, y + HEIGHT])
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
51 vertices[vertIndex + 3] = Vec2([x, y + HEIGHT])
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
52 colors[vertIndex + 0] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
53 colors[vertIndex + 1] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
54 colors[vertIndex + 2] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
55 colors[vertIndex + 3] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
56 iValues[vertIndex + 0] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
57 iValues[vertIndex + 1] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
58 iValues[vertIndex + 2] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
59 iValues[vertIndex + 3] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
60 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
61 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
62
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
63
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
64 type PIndexedMesh = ref IndexedMesh[VertexDataA, uint16] # required so we can use ctor with ref/on heap
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
65 var squaremesh = PIndexedMesh(
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
66 vertexData: VertexDataA(
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
67 position11: PositionAttribute[Vec2](data: @vertices),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
68 color22: ColorAttribute[Vec3](data: @colors),
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
69 index: GenericAttribute[uint32](data: @iValues),
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
70 ),
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
71 indices: @indices
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
72 )
39
0cb294c5d2fd fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 38
diff changeset
73 var scene = new Thing
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
74 var childthing = new Thing
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
75 childthing.parts.add squaremesh
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
76 scene.children.add childthing
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
77
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
78 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
79 """
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
80 float pos_weight = index / 100.0; // add some gamma correction?
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
81 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5;
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
82 float v = min(1, max(0, pow(pos_weight - t, 2)));
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
83 v = pow(1 - v, 3000);
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
84 out_color = vec3(in_color.r, in_color.g, v * 0.5);
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
85 """
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
86 )
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
87 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
88 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
89 myengine,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
90 scene,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
91 vertexShader,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
92 fragmentShader
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
93 )
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
94 myengine.run(pipeline, globalUpdate)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
95 pipeline.trash()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
96 myengine.trash()