annotate examples/squares.nim @ 37:6859bcfabc62

did: update notes
author Sam <sam@basx.dev>
date Mon, 16 Jan 2023 00:51:03 +0700
parents 94c38e4b5782
children c3c963e7c1a6
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
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
7 import zamikongine/engine
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8 import zamikongine/math/vector
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9 import zamikongine/math/matrix
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 import zamikongine/vertex
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
11 import zamikongine/descriptor
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 import zamikongine/mesh
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 import zamikongine/thing
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 import zamikongine/shader
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 import zamikongine/buffer
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 type
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 VertexDataA = object
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19 position11: PositionAttribute[Vec2[float32]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
20 color22: ColorAttribute[Vec3[float32]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 index: GenericAttribute[uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22 Uniforms = object
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 t: Descriptor[float32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25 var
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
26 pipeline: RenderPipeline[VertexDataA, Uniforms]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
28
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 proc globalUpdate(engine: var Engine, dt: float32) =
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 uniformdata.t.value += dt
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
31 for buffer in pipeline.uniformBuffers:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
32 buffer.updateData(uniformdata)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
34 when isMainModule:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 randomize()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
36 var myengine = igniteEngine("A lot of triangles")
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 const
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 COLUMNS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39 ROWS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 WIDTH = 2'f32 / COLUMNS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 HEIGHT = 2'f32 / ROWS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 var
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
43 vertices: array[COLUMNS * ROWS * 4, Vec2[float32]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
44 colors: array[COLUMNS * ROWS * 4, Vec3[float32]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
45 iValues: array[COLUMNS * ROWS * 4, uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
46 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
47
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
48 for row in 0 ..< ROWS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
49 for col in 0 ..< COLUMNS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
50 let
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
51 y: float32 = (row * 2 / COLUMNS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
52 x: float32 = (col * 2 / ROWS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
53 color = Vec3[float32]([(x + 1) / 2, (y + 1) / 2, 0'f32])
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
54 squareIndex = row * COLUMNS + col
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
55 vertIndex = squareIndex * 4
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
56 vertices[vertIndex + 0] = Vec2[float32]([x, y])
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
57 vertices[vertIndex + 1] = Vec2[float32]([x + WIDTH, y])
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
58 vertices[vertIndex + 2] = Vec2[float32]([x + WIDTH, y + HEIGHT])
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
59 vertices[vertIndex + 3] = Vec2[float32]([x, y + HEIGHT])
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
60 colors[vertIndex + 0] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
61 colors[vertIndex + 1] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
62 colors[vertIndex + 2] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
63 colors[vertIndex + 3] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
64 iValues[vertIndex + 0] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
65 iValues[vertIndex + 1] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
66 iValues[vertIndex + 2] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
67 iValues[vertIndex + 3] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
68 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
69 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
70
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
71 var scene = new Thing
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
72
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
73 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
74 var squaremesh = PIndexedMesh(
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
75 vertexData: VertexDataA(
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
76 position11: PositionAttribute[Vec2[float32]](data: @vertices),
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
77 color22: ColorAttribute[Vec3[float32]](data: @colors),
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
78 index: GenericAttribute[uint32](data: @iValues),
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 indices: @indices
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
81 )
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
82 var childthing = new Thing
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
83 childthing.parts.add squaremesh
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
84 scene.children.add childthing
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 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
87 """
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
88 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
89 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
90 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
91 v = pow(1 - v, 3000);
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
92 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
93 """
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
94 )
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
95 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
96 static:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
97 echo "--------------"
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
98 for (i, line) in enumerate(vertexShader.splitLines()):
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
99 echo $(i + 1) & " " & line
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
100 echo "--------------"
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
101 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
102 myengine,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
103 scene,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
104 vertexShader,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
105 fragmentShader
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
106 )
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
107 myengine.run(pipeline, globalUpdate)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
108 pipeline.trash()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
109 myengine.trash()