Mercurial > games > semicongine
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 488:455f1a416fe7 | 489:54a1f8ee208e |
|---|---|
| 1 import std/times | |
| 1 import std/math | 2 import std/math |
| 2 import std/random | 3 import std/random |
| 3 | 4 |
| 4 import zamikongine/engine | 5 import zamikongine/engine |
| 5 import zamikongine/math/vector | 6 import zamikongine/math/vector |
| 12 type | 13 type |
| 13 VertexDataA = object | 14 VertexDataA = object |
| 14 position11: VertexAttribute[Vec2[float32]] | 15 position11: VertexAttribute[Vec2[float32]] |
| 15 color22: VertexAttribute[Vec3[float32]] | 16 color22: VertexAttribute[Vec3[float32]] |
| 16 | 17 |
| 18 proc globalUpdate(engine: var Engine, dt: Duration) = | |
| 19 discard | |
| 20 | |
| 17 proc randomtransform(): Mat33[float32] = | 21 proc randomtransform(): Mat33[float32] = |
| 18 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) | 22 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) |
| 19 let randomrotate = rotate2d(float32(rand(2 * PI))) | 23 let randomrotate = rotate2d(float32(rand(2 * PI))) |
| 20 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) | 24 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) |
| 21 result = randomtranslate * randomrotate * randomscale | 25 result = randomtranslate * randomrotate * randomscale |
| 22 | 26 |
| 23 when isMainModule: | 27 when isMainModule: |
| 24 randomize() | 28 randomize() |
| 25 var myengine = igniteEngine() | 29 var myengine = igniteEngine("A lot of triangles") |
| 26 const baseTriangle = [ | 30 const baseTriangle = [ |
| 27 Vec3([-0.1'f32, -0.1'f32, 1'f32]), | 31 Vec3([-0.1'f32, -0.1'f32, 1'f32]), |
| 28 Vec3([ 0.1'f32, 0.1'f32, 1'f32]), | 32 Vec3([ 0.1'f32, 0.1'f32, 1'f32]), |
| 29 Vec3([-0.1'f32, 0.1'f32, 1'f32]), | 33 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
| 30 ] | 34 ] |
| 32 var scene = new Thing | 36 var scene = new Thing |
| 33 | 37 |
| 34 for i in 1 .. 300: | 38 for i in 1 .. 300: |
| 35 var randommesh = new Mesh[VertexDataA] | 39 var randommesh = new Mesh[VertexDataA] |
| 36 # TODO: create randomized position11 from baseTriangle with random transformation matrix | 40 # TODO: create randomized position11 from baseTriangle with random transformation matrix |
| 41 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) | |
| 37 let transform1 = randomtransform() | 42 let transform1 = randomtransform() |
| 38 randommesh.vertexData = VertexDataA( | 43 randommesh.vertexData = VertexDataA( |
| 39 position11: VertexAttribute[Vec2[float32]]( | 44 position11: VertexAttribute[Vec2[float32]]( |
| 40 data: @[ | 45 data: @[ |
| 41 Vec2[float32](transform1 * baseTriangle[0]), | 46 Vec2[float32](transform1 * baseTriangle[0]), |
| 42 Vec2[float32](transform1 * baseTriangle[1]), | 47 Vec2[float32](transform1 * baseTriangle[1]), |
| 43 Vec2[float32](transform1 * baseTriangle[2]), | 48 Vec2[float32](transform1 * baseTriangle[2]), |
| 44 ] | 49 ] |
| 45 ), | 50 ), |
| 46 color22: VertexAttribute[Vec3[float32]]( | 51 color22: VertexAttribute[Vec3[float32]]( |
| 47 data: @[ | 52 data: @[randomcolor1, randomcolor1, randomcolor1] |
| 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 ) |
| 53 ) | 54 ) |
| 54 | 55 |
| 56 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) | |
| 55 let transform2 = randomtransform() | 57 let transform2 = randomtransform() |
| 56 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] | 58 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] |
| 57 randomindexedmesh.vertexData = VertexDataA( | 59 randomindexedmesh.vertexData = VertexDataA( |
| 58 position11: VertexAttribute[Vec2[float32]]( | 60 position11: VertexAttribute[Vec2[float32]]( |
| 59 data: @[ | 61 data: @[ |
| 61 Vec2[float32](transform2 * baseTriangle[1]), | 63 Vec2[float32](transform2 * baseTriangle[1]), |
| 62 Vec2[float32](transform2 * baseTriangle[2]), | 64 Vec2[float32](transform2 * baseTriangle[2]), |
| 63 ] | 65 ] |
| 64 ), | 66 ), |
| 65 color22: VertexAttribute[Vec3[float32]]( | 67 color22: VertexAttribute[Vec3[float32]]( |
| 66 data: @[ | 68 data: @[randomcolor2, randomcolor2, randomcolor2] |
| 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 ) | 69 ) |
| 72 ) | 70 ) |
| 73 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] | 71 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] |
| 74 var childthing = new Thing | 72 var childthing = new Thing |
| 75 childthing.parts.add randommesh | 73 childthing.parts.add randommesh |
| 76 childthing.parts.add randomindexedmesh | 74 childthing.parts.add randomindexedmesh |
| 77 scene.children.add childthing | 75 scene.children.add childthing |
| 78 | 76 |
| 79 setupPipeline[VertexDataA, uint16]( | 77 var pipeline = setupPipeline[VertexDataA, float32, float32, uint16]( |
| 80 myengine, | 78 myengine, |
| 81 scene, | 79 scene, |
| 82 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"), | 80 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"), |
| 83 generateFragmentShaderCode[VertexDataA]("main"), | 81 generateFragmentShaderCode[VertexDataA]("main"), |
| 84 ) | 82 ) |
| 85 myengine.fullThrottle() | 83 myengine.run(pipeline, globalUpdate) |
| 84 pipeline.trash() | |
| 86 myengine.trash() | 85 myengine.trash() |
