Mercurial > games > semicongine
annotate examples/alotof_triangles.nim @ 518:5d406c17bbcb
did: refactor Vector names
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 16:13:32 +0700 |
parents | 836790efab48 |
children | 8287a91e5d56 |
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 |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
7 import semicongine |
482 | 8 |
9 type | |
10 VertexDataA = object | |
518 | 11 position11: PositionAttribute[TVec2[float32]] |
12 color22: ColorAttribute[TVec3[float32]] | |
493 | 13 Uniforms = object |
14 dt: Descriptor[float32] | |
482 | 15 |
493 | 16 proc globalUpdate(engine: var Engine, dt: float32) = |
489 | 17 discard |
18 | |
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 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
|
20 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
|
21 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
|
22 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
|
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 = [ |
518 | 29 TVec3([-0.1'f32, -0.1'f32, 1'f32]), |
30 TVec3([ 0.1'f32, 0.1'f32, 1'f32]), | |
31 TVec3([-0.1'f32, 0.1'f32, 1'f32]), | |
482 | 32 ] |
33 | |
34 var scene = new Thing | |
35 | |
36 for i in 1 .. 300: | |
37 var randommesh = new Mesh[VertexDataA] | |
518 | 38 let randomcolor1 = TVec3([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( |
518 | 41 position11: PositionAttribute[TVec2[float32]]( |
482 | 42 data: @[ |
518 | 43 TVec2[float32](transform1 * baseTriangle[0]), |
44 TVec2[float32](transform1 * baseTriangle[1]), | |
45 TVec2[float32](transform1 * baseTriangle[2]), | |
482 | 46 ] |
47 ), | |
518 | 48 color22: ColorAttribute[TVec3[float32]]( |
489 | 49 data: @[randomcolor1, randomcolor1, randomcolor1] |
482 | 50 ) |
51 ) | |
52 | |
518 | 53 let randomcolor2 = TVec3([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( | |
518 | 57 position11: PositionAttribute[TVec2[float32]]( |
482 | 58 data: @[ |
518 | 59 TVec2[float32](transform2 * baseTriangle[0]), |
60 TVec2[float32](transform2 * baseTriangle[1]), | |
61 TVec2[float32](transform2 * baseTriangle[2]), | |
482 | 62 ] |
63 ), | |
518 | 64 color22: ColorAttribute[TVec3[float32]]( |
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]] |
482 | 69 var childthing = new Thing |
70 childthing.parts.add randommesh | |
71 childthing.parts.add randomindexedmesh | |
72 scene.children.add childthing | |
73 | |
493 | 74 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() |
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
76 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | |
482 | 77 myengine, |
78 scene, | |
493 | 79 vertexShader, |
80 fragmentShader | |
482 | 81 ) |
489 | 82 myengine.run(pipeline, globalUpdate) |
83 pipeline.trash() | |
482 | 84 myengine.trash() |