annotate examples/E02_squares.nim @ 95:8011e4d6372d

add: consistent way to define platform-specific code
author Sam <sam@basx.dev>
date Wed, 01 Mar 2023 00:01:06 +0700
parents 779607656b12
children fc5db4f1f94e
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
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
6 import semicongine
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
7
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
8 type
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
9 VertexDataA = object
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
10 position11: PositionAttribute[Vec2]
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
11 color22: ColorAttribute[Vec3]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
12 index: GenericAttribute[uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
13 Uniforms = object
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
14 t: Descriptor[float32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
15
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
16 var
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
17 pipeline: RenderPipeline[VertexDataA, Uniforms]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
18 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
19
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents: 68
diff changeset
20 proc globalUpdate(engine: var Engine, t, dt: float32) =
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
21 uniformdata.t.value += dt
61
0f04ba283558 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 60
diff changeset
22 engine.vulkan.device.updateUniformData(pipeline, uniformdata)
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
23
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
24 when isMainModule:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
25 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
26 var myengine = igniteEngine("Squares")
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
27 const
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
28 COLUMNS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
29 ROWS = 10
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
30 WIDTH = 2'f32 / COLUMNS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
31 HEIGHT = 2'f32 / ROWS
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
32 var
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
33 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
34 colors: array[COLUMNS * ROWS * 4, Vec3]
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
35 iValues: array[COLUMNS * ROWS * 4, uint32]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
36 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
37
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
38 for row in 0 ..< ROWS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
39 for col in 0 ..< COLUMNS:
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
40 let
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
41 y: float32 = (row * 2 / COLUMNS) - 1
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
42 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
43 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
44 squareIndex = row * COLUMNS + col
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
45 vertIndex = squareIndex * 4
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
46 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
47 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
48 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
49 vertices[vertIndex + 3] = Vec2([x, y + HEIGHT])
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
50 colors[vertIndex + 0] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
51 colors[vertIndex + 1] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
52 colors[vertIndex + 2] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
53 colors[vertIndex + 3] = color
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
54 iValues[vertIndex + 0] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
55 iValues[vertIndex + 1] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
56 iValues[vertIndex + 2] = uint32(squareIndex)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
57 iValues[vertIndex + 3] = uint32(squareIndex)
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
58 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex +
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
59 1), uint16(vertIndex + 2)]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
60 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex +
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
61 3), uint16(vertIndex + 0)]
33
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
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
64 type PIndexedMesh = Mesh[VertexDataA,
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
65 uint16] # required so we can use ctor with ref/on heap
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
66 var squaremesh = PIndexedMesh(
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
67 vertexData: VertexDataA(
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
68 position11: PositionAttribute[Vec2](data: @vertices),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
69 color22: ColorAttribute[Vec3](data: @colors),
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
70 index: GenericAttribute[uint32](data: @iValues),
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
71 ),
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
72 indexed: true,
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
73 indices: @indices
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
74 )
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
75 var scene = newThing("scene", newThing("squares", squaremesh))
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
76
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
77 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
78 """
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
79 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
80 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
81 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
82 v = pow(1 - v, 3000);
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
83 out_color = vec4(in_color.r, in_color.g, v * 0.5, 1.0);
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
84 """
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 fragmentShader = generateFragmentShaderCode[VertexDataA]()
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
87 static:
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
88 echo vertexShader
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
89 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
90 myengine,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
91 scene,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
92 vertexShader,
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
93 fragmentShader
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 myengine.run(pipeline, globalUpdate)
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
96 pipeline.trash()
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff changeset
97 myengine.trash()