annotate examples/squares.nim @ 517:836790efab48

did: cleanup main namespace, add: better coordinate handling in input example
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 00:41:55 +0700
parents e89fceb5a3a2
children 547f3a374271
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/times
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/math
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/enumerate
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
6
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
7 import semicongine
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9 type
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 VertexDataA = object
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
11 position11: PositionAttribute[Vec2[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 color22: ColorAttribute[Vec3[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 index: GenericAttribute[uint32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 Uniforms = object
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 t: Descriptor[float32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
16
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
17 var
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 pipeline: RenderPipeline[VertexDataA, Uniforms]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
20
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 proc globalUpdate(engine: var Engine, dt: float32) =
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22 uniformdata.t.value += dt
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 for buffer in pipeline.uniformBuffers:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24 buffer.updateData(uniformdata)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
26 when isMainModule:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 randomize()
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents: 494
diff changeset
28 var myengine = igniteEngine("Squares")
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 const
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 COLUMNS = 10
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
31 ROWS = 10
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
32 WIDTH = 2'f32 / COLUMNS
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
33 HEIGHT = 2'f32 / ROWS
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
34 var
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 vertices: array[COLUMNS * ROWS * 4, Vec2[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
36 colors: array[COLUMNS * ROWS * 4, Vec3[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 iValues: array[COLUMNS * ROWS * 4, uint32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 for row in 0 ..< ROWS:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 for col in 0 ..< COLUMNS:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 let
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
43 y: float32 = (row * 2 / COLUMNS) - 1
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
44 x: float32 = (col * 2 / ROWS) - 1
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
45 color = Vec3[float32]([(x + 1) / 2, (y + 1) / 2, 0'f32])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
46 squareIndex = row * COLUMNS + col
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
47 vertIndex = squareIndex * 4
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
48 vertices[vertIndex + 0] = Vec2[float32]([x, y])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
49 vertices[vertIndex + 1] = Vec2[float32]([x + WIDTH, y])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
50 vertices[vertIndex + 2] = Vec2[float32]([x + WIDTH, y + HEIGHT])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
51 vertices[vertIndex + 3] = Vec2[float32]([x, y + HEIGHT])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
52 colors[vertIndex + 0] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
53 colors[vertIndex + 1] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
54 colors[vertIndex + 2] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
55 colors[vertIndex + 3] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
56 iValues[vertIndex + 0] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
57 iValues[vertIndex + 1] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
58 iValues[vertIndex + 2] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
59 iValues[vertIndex + 3] = uint32(squareIndex)
0c18638c7217 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)]
0c18638c7217 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)]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
62
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
63
0c18638c7217 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
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
65 var squaremesh = PIndexedMesh(
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
66 vertexData: VertexDataA(
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
67 position11: PositionAttribute[Vec2[float32]](data: @vertices),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
68 color22: ColorAttribute[Vec3[float32]](data: @colors),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
69 index: GenericAttribute[uint32](data: @iValues),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
70 ),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
71 indices: @indices
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
72 )
500
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
73 var scene = new Thing
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
74 var childthing = new Thing
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
75 childthing.parts.add squaremesh
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
76 scene.children.add childthing
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
77
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
78 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
79 """
0c18638c7217 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?
0c18638c7217 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;
0c18638c7217 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)));
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
83 v = pow(1 - v, 3000);
0c18638c7217 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);
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
85 """
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
86 )
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
87 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
88 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
89 myengine,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
90 scene,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
91 vertexShader,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
92 fragmentShader
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
93 )
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
94 myengine.run(pipeline, globalUpdate)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
95 pipeline.trash()
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
96 myengine.trash()