annotate examples/squares.nim @ 500:8025ab67d931

fix: hello cube, add: run_all command
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 13:49:12 +0700
parents 3f1111f3b9f8
children 2771db8d4276
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
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
7 import zamikongine/engine
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8 import zamikongine/math/vector
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9 import zamikongine/math/matrix
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
10 import zamikongine/vertex
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
11 import zamikongine/descriptor
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 import zamikongine/mesh
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 import zamikongine/thing
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 import zamikongine/shader
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15 import zamikongine/buffer
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 type
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 VertexDataA = object
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19 position11: PositionAttribute[Vec2[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
20 color22: ColorAttribute[Vec3[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 index: GenericAttribute[uint32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
22 Uniforms = object
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23 t: Descriptor[float32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25 var
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
26 pipeline: RenderPipeline[VertexDataA, Uniforms]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
28
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 proc globalUpdate(engine: var Engine, dt: float32) =
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 uniformdata.t.value += dt
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
31 for buffer in pipeline.uniformBuffers:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
32 buffer.updateData(uniformdata)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
33
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
34 when isMainModule:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 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
36 var myengine = igniteEngine("Squares")
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37 const
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 COLUMNS = 10
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39 ROWS = 10
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 WIDTH = 2'f32 / COLUMNS
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 HEIGHT = 2'f32 / ROWS
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 var
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
43 vertices: array[COLUMNS * ROWS * 4, Vec2[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
44 colors: array[COLUMNS * ROWS * 4, Vec3[float32]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
45 iValues: array[COLUMNS * ROWS * 4, uint32]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
46 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
47
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
48 for row in 0 ..< ROWS:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
49 for col in 0 ..< COLUMNS:
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
50 let
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
51 y: float32 = (row * 2 / COLUMNS) - 1
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
52 x: float32 = (col * 2 / ROWS) - 1
0c18638c7217 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])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
54 squareIndex = row * COLUMNS + col
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
55 vertIndex = squareIndex * 4
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
56 vertices[vertIndex + 0] = Vec2[float32]([x, y])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
57 vertices[vertIndex + 1] = Vec2[float32]([x + WIDTH, y])
0c18638c7217 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])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
59 vertices[vertIndex + 3] = Vec2[float32]([x, y + HEIGHT])
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
60 colors[vertIndex + 0] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
61 colors[vertIndex + 1] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
62 colors[vertIndex + 2] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
63 colors[vertIndex + 3] = color
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
64 iValues[vertIndex + 0] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
65 iValues[vertIndex + 1] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
66 iValues[vertIndex + 2] = uint32(squareIndex)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
67 iValues[vertIndex + 3] = uint32(squareIndex)
0c18638c7217 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)]
0c18638c7217 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)]
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
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
72 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
73 var squaremesh = PIndexedMesh(
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
74 vertexData: VertexDataA(
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
75 position11: PositionAttribute[Vec2[float32]](data: @vertices),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
76 color22: ColorAttribute[Vec3[float32]](data: @colors),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
77 index: GenericAttribute[uint32](data: @iValues),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
78 ),
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
79 indices: @indices
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
80 )
500
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
81 var scene = new Thing
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
82 var childthing = new Thing
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
83 childthing.parts.add squaremesh
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
84 scene.children.add childthing
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 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
87 """
0c18638c7217 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?
0c18638c7217 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;
0c18638c7217 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)));
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
91 v = pow(1 - v, 3000);
0c18638c7217 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);
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 )
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
95 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
96 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
97 myengine,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
98 scene,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
99 vertexShader,
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
100 fragmentShader
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
101 )
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
102 myengine.run(pipeline, globalUpdate)
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
103 pipeline.trash()
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
104 myengine.trash()