annotate examples/alotof_triangles.nim @ 60:c57285d292b6

did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
author Sam <sam@basx.dev>
date Sun, 22 Jan 2023 22:46:53 +0700
parents d7d9420ba675
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
1 import std/times
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
2 import std/strutils
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
3 import std/math
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
316923e9247c add: clean examples, update build configs
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
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
7
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
8 type
316923e9247c add: clean examples, update build configs
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: 58
diff changeset
10 position11: PositionAttribute[Vec2]
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
11 color22: ColorAttribute[Vec3]
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
12 Uniforms = object
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
13 dt: Descriptor[float32]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
14
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
15 proc globalUpdate(engine: var Engine, dt: float32) =
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
16 discard
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
17
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
18 proc randomtransform(): Mat33 =
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
19 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5))
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
20 let randomrotate = rotate2d(float32(rand(2 * PI)))
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
21 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
22 1.6) - 0.8))
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
23 result = randomtranslate * randomrotate * randomscale
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
24
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
25 when isMainModule:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
26 randomize()
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
27 var myengine = igniteEngine("A lot of triangles")
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
28 const baseTriangle = [
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
29 Vec3([-0.1'f32, -0.1'f32, 1'f32]),
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
30 Vec3([0.1'f32, 0.1'f32, 1'f32]),
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
31 Vec3([-0.1'f32, 0.1'f32, 1'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
32 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
33
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
34 var scene = newThing("scene")
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
35
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
36 for i in 1 .. 300:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
37 var randommesh = new Mesh[VertexDataA]
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
38 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
39 let transform1 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
40 randommesh.vertexData = VertexDataA(
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
41 position11: PositionAttribute[Vec2](
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
42 data: @[
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
43 Vec2(transform1 * baseTriangle[0]),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
44 Vec2(transform1 * baseTriangle[1]),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
45 Vec2(transform1 * baseTriangle[2]),
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
46 ]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
47 ),
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
48 color22: ColorAttribute[Vec3](
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
49 data: @[randomcolor1, randomcolor1, randomcolor1]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
50 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
51 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
52
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
53 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
54 let transform2 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
55 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
56 randomindexedmesh.vertexData = VertexDataA(
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
57 position11: PositionAttribute[Vec2](
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
58 data: @[
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
59 Vec2(transform2 * baseTriangle[0]),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
60 Vec2(transform2 * baseTriangle[1]),
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
61 Vec2(transform2 * baseTriangle[2]),
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
62 ]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
63 ),
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
64 color22: ColorAttribute[Vec3](
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
65 data: @[randomcolor2, randomcolor2, randomcolor2]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
66 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
67 )
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
68 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
69 scene.add newThing("randommesh", randommesh, randomindexedmesh)
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
70
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
71 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]()
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
72 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
73 var pipeline = setupPipeline[VertexDataA, float32, uint16](
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
74 myengine,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
75 scene,
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
76 vertexShader,
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
77 fragmentShader
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
78 )
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
79 myengine.run(pipeline, globalUpdate)
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
80 pipeline.trash()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
81 myengine.trash()