Mercurial > games > semicongine
annotate examples/alotof_triangles.nim @ 521:a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 22 Jan 2023 22:46:53 +0700 |
parents | cd73e429fc99 |
children |
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 |
5 | |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
6 import semicongine |
482 | 7 |
8 type | |
9 VertexDataA = object | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
10 position11: PositionAttribute[Vec2] |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
11 color22: ColorAttribute[Vec3] |
493 | 12 Uniforms = object |
13 dt: Descriptor[float32] | |
482 | 14 |
493 | 15 proc globalUpdate(engine: var Engine, dt: float32) = |
489 | 16 discard |
17 | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
18 proc randomtransform(): Mat33 = |
483
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 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
|
20 let randomrotate = rotate2d(float32(rand(2 * PI))) |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
21 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand( |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
22 1.6) - 0.8)) |
483
73a0954beabd
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
482
diff
changeset
|
23 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
|
24 |
482 | 25 when isMainModule: |
26 randomize() | |
489 | 27 var myengine = igniteEngine("A lot of triangles") |
482 | 28 const baseTriangle = [ |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
29 Vec3([-0.1'f32, -0.1'f32, 1'f32]), |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
30 Vec3([0.1'f32, 0.1'f32, 1'f32]), |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
31 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
482 | 32 ] |
33 | |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
34 var scene = newThing("scene") |
482 | 35 |
36 for i in 1 .. 300: | |
37 var randommesh = new Mesh[VertexDataA] | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
38 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
|
39 let transform1 = randomtransform() |
482 | 40 randommesh.vertexData = VertexDataA( |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
41 position11: PositionAttribute[Vec2]( |
482 | 42 data: @[ |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
43 Vec2(transform1 * baseTriangle[0]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
44 Vec2(transform1 * baseTriangle[1]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
45 Vec2(transform1 * baseTriangle[2]), |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
46 ] |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
47 ), |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
48 color22: ColorAttribute[Vec3]( |
489 | 49 data: @[randomcolor1, randomcolor1, randomcolor1] |
482 | 50 ) |
51 ) | |
52 | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
53 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
|
54 let transform2 = randomtransform() |
482 | 55 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] |
56 randomindexedmesh.vertexData = VertexDataA( | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
57 position11: PositionAttribute[Vec2]( |
482 | 58 data: @[ |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
59 Vec2(transform2 * baseTriangle[0]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
60 Vec2(transform2 * baseTriangle[1]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
61 Vec2(transform2 * baseTriangle[2]), |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
62 ] |
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
63 ), |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
64 color22: ColorAttribute[Vec3]( |
489 | 65 data: @[randomcolor2, randomcolor2, randomcolor2] |
482 | 66 ) |
67 ) | |
483
73a0954beabd
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents:
482
diff
changeset
|
68 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] |
521
a25325bec7f2
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
520
diff
changeset
|
69 scene.add newThing("randommesh", randommesh, randomindexedmesh) |
482 | 70 |
493 | 71 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() |
72 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
73 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | |
482 | 74 myengine, |
75 scene, | |
493 | 76 vertexShader, |
77 fragmentShader | |
482 | 78 ) |
489 | 79 myengine.run(pipeline, globalUpdate) |
80 pipeline.trash() | |
482 | 81 myengine.trash() |