annotate examples/alotof_triangles.nim @ 30:917d99d5eb89

add: missing file
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 14:08:45 +0700
parents b1b05d4efb52
children 9edca5dc4e93
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
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
2 import std/math
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/random
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
4
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
5 import zamikongine/engine
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
6 import zamikongine/math/vector
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
7 import zamikongine/math/matrix
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
8 import zamikongine/vertex
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
9 import zamikongine/mesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
10 import zamikongine/thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
11 import zamikongine/shader
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
12
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
13 type
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
14 VertexDataA = object
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
15 position11: VertexAttribute[Vec2[float32]]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
16 color22: VertexAttribute[Vec3[float32]]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
17
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
18 proc globalUpdate(engine: var Engine, dt: Duration) =
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
19 discard
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
20
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
21 proc randomtransform(): Mat33[float32] =
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
22 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
23 let randomrotate = rotate2d(float32(rand(2 * PI)))
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
24 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8))
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
25 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
26
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
27 when isMainModule:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
28 randomize()
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
29 var myengine = igniteEngine("A lot of triangles")
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
30 const baseTriangle = [
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
31 Vec3([-0.1'f32, -0.1'f32, 1'f32]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
32 Vec3([ 0.1'f32, 0.1'f32, 1'f32]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
33 Vec3([-0.1'f32, 0.1'f32, 1'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
34 ]
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 var scene = new Thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
37
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
38 for i in 1 .. 300:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
39 var randommesh = new Mesh[VertexDataA]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
40 # TODO: create randomized position11 from baseTriangle with random transformation matrix
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
41 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
42 let transform1 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
43 randommesh.vertexData = VertexDataA(
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
44 position11: VertexAttribute[Vec2[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
45 data: @[
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
46 Vec2[float32](transform1 * baseTriangle[0]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
47 Vec2[float32](transform1 * baseTriangle[1]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
48 Vec2[float32](transform1 * baseTriangle[2]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
49 ]
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 color22: VertexAttribute[Vec3[float32]](
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
52 data: @[randomcolor1, randomcolor1, randomcolor1]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
53 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
54 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
55
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
56 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
57 let transform2 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
58 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
59 randomindexedmesh.vertexData = VertexDataA(
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
60 position11: VertexAttribute[Vec2[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
61 data: @[
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
62 Vec2[float32](transform2 * baseTriangle[0]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
63 Vec2[float32](transform2 * baseTriangle[1]),
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
64 Vec2[float32](transform2 * baseTriangle[2]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
65 ]
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 color22: VertexAttribute[Vec3[float32]](
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
68 data: @[randomcolor2, randomcolor2, randomcolor2]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
69 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
70 )
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
71 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
72 var childthing = new Thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
73 childthing.parts.add randommesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
74 childthing.parts.add randomindexedmesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
75 scene.children.add childthing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
76
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
77 var pipeline = setupPipeline[VertexDataA, float32, float32, uint16](
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
78 myengine,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
79 scene,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
80 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
81 generateFragmentShaderCode[VertexDataA]("main"),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
82 )
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
83 myengine.run(pipeline, globalUpdate)
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 22
diff changeset
84 pipeline.trash()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
85 myengine.trash()