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