Mercurial > games > semicongine
annotate examples/alotof_triangles.nim @ 56:94d7eed3f118
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 | 2771db8d4276 |
children | 547f3a374271 |
rev | line source |
---|---|
28 | 1 import std/times |
32 | 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 | 4 import std/random |
32 | 5 import std/enumerate |
21 | 6 |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
7 import semicongine |
21 | 8 |
9 type | |
10 VertexDataA = object | |
32 | 11 position11: PositionAttribute[Vec2[float32]] |
12 color22: ColorAttribute[Vec3[float32]] | |
13 Uniforms = object | |
14 dt: Descriptor[float32] | |
21 | 15 |
32 | 16 proc globalUpdate(engine: var Engine, dt: float32) = |
28 | 17 discard |
18 | |
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 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
|
20 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
|
21 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
|
22 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
|
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 | 25 when isMainModule: |
26 randomize() | |
28 | 27 var myengine = igniteEngine("A lot of triangles") |
21 | 28 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
|
29 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
|
30 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
|
31 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
21 | 32 ] |
33 | |
34 var scene = new Thing | |
35 | |
36 for i in 1 .. 300: | |
37 var randommesh = new Mesh[VertexDataA] | |
28 | 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 | 40 randommesh.vertexData = VertexDataA( |
32 | 41 position11: PositionAttribute[Vec2[float32]]( |
21 | 42 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
|
43 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
|
44 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
|
45 Vec2[float32](transform1 * baseTriangle[2]), |
21 | 46 ] |
47 ), | |
32 | 48 color22: ColorAttribute[Vec3[float32]]( |
28 | 49 data: @[randomcolor1, randomcolor1, randomcolor1] |
21 | 50 ) |
51 ) | |
52 | |
28 | 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 | 55 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] |
56 randomindexedmesh.vertexData = VertexDataA( | |
32 | 57 position11: PositionAttribute[Vec2[float32]]( |
21 | 58 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
|
59 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
|
60 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
|
61 Vec2[float32](transform2 * baseTriangle[2]), |
21 | 62 ] |
63 ), | |
32 | 64 color22: ColorAttribute[Vec3[float32]]( |
28 | 65 data: @[randomcolor2, randomcolor2, randomcolor2] |
21 | 66 ) |
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]] |
21 | 69 var childthing = new Thing |
70 childthing.parts.add randommesh | |
71 childthing.parts.add randomindexedmesh | |
72 scene.children.add childthing | |
73 | |
32 | 74 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() |
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
76 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | |
21 | 77 myengine, |
78 scene, | |
32 | 79 vertexShader, |
80 fragmentShader | |
21 | 81 ) |
28 | 82 myengine.run(pipeline, globalUpdate) |
83 pipeline.trash() | |
21 | 84 myengine.trash() |