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