annotate examples/alotof_triangles.nim @ 485:b4a972bd37d5

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