annotate examples/alotof_triangles.nim @ 517:836790efab48

did: cleanup main namespace, add: better coordinate handling in input example
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 00:41:55 +0700
parents e89fceb5a3a2
children 547f3a374271
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
1 import std/times
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
2 import std/strutils
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
3 import std/math
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
5 import std/enumerate
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
6
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
7 import semicongine
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
8
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
9 type
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
10 VertexDataA = object
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
11 position11: PositionAttribute[Vec2[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
12 color22: ColorAttribute[Vec3[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
13 Uniforms = object
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
14 dt: Descriptor[float32]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
15
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
16 proc globalUpdate(engine: var Engine, dt: float32) =
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
17 discard
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
18
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
19 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
20 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
21 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
22 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
23 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
24
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
25 when isMainModule:
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
26 randomize()
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
27 var myengine = igniteEngine("A lot of triangles")
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
28 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
29 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
30 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
31 Vec3([-0.1'f32, 0.1'f32, 1'f32]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
32 ]
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 var scene = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
35
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
36 for i in 1 .. 300:
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
37 var randommesh = new Mesh[VertexDataA]
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
38 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
39 let transform1 = randomtransform()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
40 randommesh.vertexData = VertexDataA(
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
41 position11: PositionAttribute[Vec2[float32]](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
42 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
43 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
44 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
45 Vec2[float32](transform1 * baseTriangle[2]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
46 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
47 ),
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
48 color22: ColorAttribute[Vec3[float32]](
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
49 data: @[randomcolor1, randomcolor1, randomcolor1]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
50 )
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
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
53 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
54 let transform2 = randomtransform()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
55 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
56 randomindexedmesh.vertexData = VertexDataA(
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
57 position11: PositionAttribute[Vec2[float32]](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
58 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
59 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
60 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
61 Vec2[float32](transform2 * baseTriangle[2]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
62 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
63 ),
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
64 color22: ColorAttribute[Vec3[float32]](
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
65 data: @[randomcolor2, randomcolor2, randomcolor2]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
66 )
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
67 )
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
68 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
69 var childthing = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
70 childthing.parts.add randommesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
71 childthing.parts.add randomindexedmesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
72 scene.children.add childthing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
73
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
74 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]()
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
76 var pipeline = setupPipeline[VertexDataA, float32, uint16](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
77 myengine,
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
78 scene,
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
79 vertexShader,
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
80 fragmentShader
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
81 )
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
82 myengine.run(pipeline, globalUpdate)
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
83 pipeline.trash()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
84 myengine.trash()