annotate examples/alotof_triangles.nim @ 24:71bbe11d8de8

did: change shader compilation to run during program compilation, maybe add dynamic version later
author Sam <sam@basx.dev>
date Wed, 11 Jan 2023 11:15:02 +0700
parents b45a5d338cd0
children b1b05d4efb52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
1 import std/math
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/random
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
3
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
4 import zamikongine/engine
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
5 import zamikongine/math/vector
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
6 import zamikongine/math/matrix
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
7 import zamikongine/vertex
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
8 import zamikongine/mesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
9 import zamikongine/thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
10 import zamikongine/shader
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
11
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
12 type
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
13 VertexDataA = object
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
14 position11: VertexAttribute[Vec2[float32]]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
15 color22: VertexAttribute[Vec3[float32]]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
16
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
17 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
18 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
19 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
20 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
21 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
22
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
23 when isMainModule:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
24 randomize()
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
25 var myengine = igniteEngine()
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
26 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
27 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
28 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
29 Vec3([-0.1'f32, 0.1'f32, 1'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
30 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
31
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
32 var scene = new Thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
33
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
34 for i in 1 .. 300:
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
35 var randommesh = new Mesh[VertexDataA]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
36 # TODO: create randomized position11 from baseTriangle with random transformation matrix
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
37 let transform1 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
38 randommesh.vertexData = VertexDataA(
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
39 position11: VertexAttribute[Vec2[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
40 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
41 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
42 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
43 Vec2[float32](transform1 * baseTriangle[2]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
44 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
45 ),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
46 color22: VertexAttribute[Vec3[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
47 data: @[
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
48 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
49 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
50 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
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 )
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
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
55 let transform2 = randomtransform()
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
56 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
57 randomindexedmesh.vertexData = VertexDataA(
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
58 position11: VertexAttribute[Vec2[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
59 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
60 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
61 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
62 Vec2[float32](transform2 * baseTriangle[2]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
63 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
64 ),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
65 color22: VertexAttribute[Vec3[float32]](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
66 data: @[
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
67 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
68 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
69 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
70 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
71 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
72 )
22
b45a5d338cd0 did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 21
diff changeset
73 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
74 var childthing = new Thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
75 childthing.parts.add randommesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
76 childthing.parts.add randomindexedmesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
77 scene.children.add childthing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
78
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
79 setupPipeline[VertexDataA, uint16](
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
80 myengine,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
81 scene,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
82 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
83 generateFragmentShaderCode[VertexDataA]("main"),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
84 )
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
85 myengine.fullThrottle()
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
86 myengine.trash()