Mercurial > games > semicongine
annotate examples/alotof_triangles.nim @ 32:9edca5dc4e93
add: working implementation of uniforms
| author | Sam <sam@basx.dev> | 
|---|---|
| date | Sat, 14 Jan 2023 23:34:50 +0700 | 
| parents | b1b05d4efb52 | 
| children | c3c963e7c1a6 | 
| rev | line source | 
|---|---|
| 28 | 1 import std/times | 
| 32 | 2 import std/strutils | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
3 import std/math | 
| 21 | 4 import std/random | 
| 32 | 5 import std/enumerate | 
| 21 | 6 | 
| 7 import zamikongine/engine | |
| 8 import zamikongine/math/vector | |
| 9 import zamikongine/math/matrix | |
| 10 import zamikongine/vertex | |
| 32 | 11 import zamikongine/descriptor | 
| 21 | 12 import zamikongine/mesh | 
| 13 import zamikongine/thing | |
| 14 import zamikongine/shader | |
| 15 | |
| 16 type | |
| 17 VertexDataA = object | |
| 32 | 18 position11: PositionAttribute[Vec2[float32]] | 
| 19 color22: ColorAttribute[Vec3[float32]] | |
| 20 Uniforms = object | |
| 21 dt: Descriptor[float32] | |
| 21 | 22 | 
| 32 | 23 proc globalUpdate(engine: var Engine, dt: float32) = | 
| 28 | 24 discard | 
| 25 | |
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
26 proc randomtransform(): Mat33[float32] = | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
27 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
28 let randomrotate = rotate2d(float32(rand(2 * PI))) | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
29 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
30 result = randomtranslate * randomrotate * randomscale | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
31 | 
| 21 | 32 when isMainModule: | 
| 33 randomize() | |
| 28 | 34 var myengine = igniteEngine("A lot of triangles") | 
| 21 | 35 const baseTriangle = [ | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
36 Vec3([-0.1'f32, -0.1'f32, 1'f32]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
37 Vec3([ 0.1'f32, 0.1'f32, 1'f32]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
38 Vec3([-0.1'f32, 0.1'f32, 1'f32]), | 
| 21 | 39 ] | 
| 40 | |
| 41 var scene = new Thing | |
| 42 | |
| 43 for i in 1 .. 300: | |
| 44 var randommesh = new Mesh[VertexDataA] | |
| 45 # TODO: create randomized position11 from baseTriangle with random transformation matrix | |
| 28 | 46 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
47 let transform1 = randomtransform() | 
| 21 | 48 randommesh.vertexData = VertexDataA( | 
| 32 | 49 position11: PositionAttribute[Vec2[float32]]( | 
| 21 | 50 data: @[ | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
51 Vec2[float32](transform1 * baseTriangle[0]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
52 Vec2[float32](transform1 * baseTriangle[1]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
53 Vec2[float32](transform1 * baseTriangle[2]), | 
| 21 | 54 ] | 
| 55 ), | |
| 32 | 56 color22: ColorAttribute[Vec3[float32]]( | 
| 28 | 57 data: @[randomcolor1, randomcolor1, randomcolor1] | 
| 21 | 58 ) | 
| 59 ) | |
| 60 | |
| 28 | 61 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
62 let transform2 = randomtransform() | 
| 21 | 63 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] | 
| 64 randomindexedmesh.vertexData = VertexDataA( | |
| 32 | 65 position11: PositionAttribute[Vec2[float32]]( | 
| 21 | 66 data: @[ | 
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
67 Vec2[float32](transform2 * baseTriangle[0]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
68 Vec2[float32](transform2 * baseTriangle[1]), | 
| 
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
69 Vec2[float32](transform2 * baseTriangle[2]), | 
| 21 | 70 ] | 
| 71 ), | |
| 32 | 72 color22: ColorAttribute[Vec3[float32]]( | 
| 28 | 73 data: @[randomcolor2, randomcolor2, randomcolor2] | 
| 21 | 74 ) | 
| 75 ) | |
| 
22
 
b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
 
Sam <sam@basx.dev> 
parents: 
21 
diff
changeset
 | 
76 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] | 
| 21 | 77 var childthing = new Thing | 
| 78 childthing.parts.add randommesh | |
| 79 childthing.parts.add randomindexedmesh | |
| 80 scene.children.add childthing | |
| 81 | |
| 32 | 82 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() | 
| 83 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
| 84 static: | |
| 85 echo "--------------" | |
| 86 for (i, line) in enumerate(vertexShader.splitLines()): | |
| 87 echo $(i + 1) & " " & line | |
| 88 echo "--------------" | |
| 89 echo fragmentShader | |
| 90 echo "--------------" | |
| 91 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | |
| 21 | 92 myengine, | 
| 93 scene, | |
| 32 | 94 vertexShader, | 
| 95 fragmentShader | |
| 21 | 96 ) | 
| 28 | 97 myengine.run(pipeline, globalUpdate) | 
| 98 pipeline.trash() | |
| 21 | 99 myengine.trash() | 
