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