Mercurial > games > semicongine
comparison examples/alotof_triangles.nim @ 483:73a0954beabd
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 00:43:17 +0700 |
parents | 1670f8e70964 |
children | b1b05d4efb52 |
comparison
equal
deleted
inserted
replaced
482:1670f8e70964 | 483:73a0954beabd |
---|---|
1 import std/math | |
1 import std/random | 2 import std/random |
2 | 3 |
3 import zamikongine/engine | 4 import zamikongine/engine |
4 import zamikongine/math/vector | 5 import zamikongine/math/vector |
5 import zamikongine/math/matrix | 6 import zamikongine/math/matrix |
11 type | 12 type |
12 VertexDataA = object | 13 VertexDataA = object |
13 position11: VertexAttribute[Vec2[float32]] | 14 position11: VertexAttribute[Vec2[float32]] |
14 color22: VertexAttribute[Vec3[float32]] | 15 color22: VertexAttribute[Vec3[float32]] |
15 | 16 |
17 proc randomtransform(): Mat33[float32] = | |
18 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) | |
19 let randomrotate = rotate2d(float32(rand(2 * PI))) | |
20 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) | |
21 result = randomtranslate * randomrotate * randomscale | |
22 | |
16 when isMainModule: | 23 when isMainModule: |
17 randomize() | 24 randomize() |
18 var myengine = igniteEngine() | 25 var myengine = igniteEngine() |
19 const baseTriangle = [ | 26 const baseTriangle = [ |
20 Vec3([-0.3'f32, -0.3'f32, 1'f32]), | 27 Vec3([-0.1'f32, -0.1'f32, 1'f32]), |
21 Vec3([ 0.3'f32, 0.3'f32, 1'f32]), | 28 Vec3([ 0.1'f32, 0.1'f32, 1'f32]), |
22 Vec3([-0.3'f32, 0.3'f32, 1'f32]), | 29 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
23 ] | 30 ] |
24 | 31 |
25 var scene = new Thing | 32 var scene = new Thing |
26 | 33 |
27 for i in 1 .. 300: | 34 for i in 1 .. 300: |
28 var randommesh = new Mesh[VertexDataA] | 35 var randommesh = new Mesh[VertexDataA] |
29 # TODO: create randomized position11 from baseTriangle with random transformation matrix | 36 # TODO: create randomized position11 from baseTriangle with random transformation matrix |
30 var transform = (Mat33[float32]().randomized() * 2'f32) - 1'f32 | 37 let transform1 = randomtransform() |
31 randommesh.vertexData = VertexDataA( | 38 randommesh.vertexData = VertexDataA( |
32 position11: VertexAttribute[Vec2[float32]]( | 39 position11: VertexAttribute[Vec2[float32]]( |
33 data: @[ | 40 data: @[ |
34 Vec2[float32](transform * baseTriangle[0]), | 41 Vec2[float32](transform1 * baseTriangle[0]), |
35 Vec2[float32](transform * baseTriangle[1]), | 42 Vec2[float32](transform1 * baseTriangle[1]), |
36 Vec2[float32](transform * baseTriangle[2]), | 43 Vec2[float32](transform1 * baseTriangle[2]), |
37 ] | 44 ] |
38 ), | 45 ), |
39 color22: VertexAttribute[Vec3[float32]]( | 46 color22: VertexAttribute[Vec3[float32]]( |
40 data: @[ | 47 data: @[ |
41 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), | 48 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), |
43 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), | 50 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), |
44 ] | 51 ] |
45 ) | 52 ) |
46 ) | 53 ) |
47 | 54 |
55 let transform2 = randomtransform() | |
48 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] | 56 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] |
49 randomindexedmesh.vertexData = VertexDataA( | 57 randomindexedmesh.vertexData = VertexDataA( |
50 position11: VertexAttribute[Vec2[float32]]( | 58 position11: VertexAttribute[Vec2[float32]]( |
51 data: @[ | 59 data: @[ |
52 Vec2[float32](transform * baseTriangle[0]), | 60 Vec2[float32](transform2 * baseTriangle[0]), |
53 Vec2[float32](transform * baseTriangle[1]), | 61 Vec2[float32](transform2 * baseTriangle[1]), |
54 Vec2[float32](transform * baseTriangle[2]), | 62 Vec2[float32](transform2 * baseTriangle[2]), |
55 ] | 63 ] |
56 ), | 64 ), |
57 color22: VertexAttribute[Vec3[float32]]( | 65 color22: VertexAttribute[Vec3[float32]]( |
58 data: @[ | 66 data: @[ |
59 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), | 67 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), |
60 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), | 68 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), |
61 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), | 69 Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]), |
62 ] | 70 ] |
63 ) | 71 ) |
64 ) | 72 ) |
65 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16], [0'u16, 2'u16, 1'u16]] | 73 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] |
66 var childthing = new Thing | 74 var childthing = new Thing |
67 childthing.parts.add randommesh | 75 childthing.parts.add randommesh |
68 childthing.parts.add randomindexedmesh | 76 childthing.parts.add randomindexedmesh |
69 scene.children.add childthing | 77 scene.children.add childthing |
70 | 78 |