Mercurial > games > semicongine
annotate examples/alotof_triangles.nim @ 41:cc8724250d65
fix: organize notes a bit
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 18 Jan 2023 14:15:08 +0700 |
parents | 2771db8d4276 |
children | 94d7eed3f118 |
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 |
40 | 7 import semicongine/engine |
8 import semicongine/math/vector | |
9 import semicongine/math/matrix | |
10 import semicongine/vertex | |
11 import semicongine/descriptor | |
12 import semicongine/mesh | |
13 import semicongine/thing | |
14 import semicongine/shader | |
21 | 15 |
16 type | |
17 VertexDataA = object | |
32 | 18 position11: PositionAttribute[Vec2[float32]] |
19 color22: ColorAttribute[Vec3[float32]] | |
20 Uniforms = object | |
21 dt: Descriptor[float32] | |
21 | 22 |
32 | 23 proc globalUpdate(engine: var Engine, dt: float32) = |
28 | 24 discard |
25 | |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
21
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 |
21 | 32 when isMainModule: |
33 randomize() | |
28 | 34 var myengine = igniteEngine("A lot of triangles") |
21 | 35 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
|
36 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
|
37 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
|
38 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
21 | 39 ] |
40 | |
41 var scene = new Thing | |
42 | |
43 for i in 1 .. 300: | |
44 var randommesh = new Mesh[VertexDataA] | |
28 | 45 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
|
46 let transform1 = randomtransform() |
21 | 47 randommesh.vertexData = VertexDataA( |
32 | 48 position11: PositionAttribute[Vec2[float32]]( |
21 | 49 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
|
50 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
|
51 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
|
52 Vec2[float32](transform1 * baseTriangle[2]), |
21 | 53 ] |
54 ), | |
32 | 55 color22: ColorAttribute[Vec3[float32]]( |
28 | 56 data: @[randomcolor1, randomcolor1, randomcolor1] |
21 | 57 ) |
58 ) | |
59 | |
28 | 60 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
|
61 let transform2 = randomtransform() |
21 | 62 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] |
63 randomindexedmesh.vertexData = VertexDataA( | |
32 | 64 position11: PositionAttribute[Vec2[float32]]( |
21 | 65 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
|
66 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
|
67 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
|
68 Vec2[float32](transform2 * baseTriangle[2]), |
21 | 69 ] |
70 ), | |
32 | 71 color22: ColorAttribute[Vec3[float32]]( |
28 | 72 data: @[randomcolor2, randomcolor2, randomcolor2] |
21 | 73 ) |
74 ) | |
22
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
21
diff
changeset
|
75 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] |
21 | 76 var childthing = new Thing |
77 childthing.parts.add randommesh | |
78 childthing.parts.add randomindexedmesh | |
79 scene.children.add childthing | |
80 | |
32 | 81 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() |
82 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
83 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | |
21 | 84 myengine, |
85 scene, | |
32 | 86 vertexShader, |
87 fragmentShader | |
21 | 88 ) |
28 | 89 myengine.run(pipeline, globalUpdate) |
90 pipeline.trash() | |
21 | 91 myengine.trash() |