annotate examples/alotof_triangles.nim @ 511:2d81453d8f91

fix: correct address retrieval
author Sam <sam@basx.dev>
date Thu, 19 Jan 2023 16:41:50 +0700
parents e89fceb5a3a2
children 94d7eed3f118
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
1 import std/times
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
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
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
5 import std/enumerate
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
6
501
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
7 import semicongine/engine
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
8 import semicongine/math/vector
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
9 import semicongine/math/matrix
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
10 import semicongine/vertex
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
11 import semicongine/descriptor
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
12 import semicongine/mesh
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
13 import semicongine/thing
e89fceb5a3a2 did: rename project
Sam <sam@basx.dev>
parents: 499
diff changeset
14 import semicongine/shader
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
15
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
16 type
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
17 VertexDataA = object
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
18 position11: PositionAttribute[Vec2[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
19 color22: ColorAttribute[Vec3[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
20 Uniforms = object
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
21 dt: Descriptor[float32]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
22
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
23 proc globalUpdate(engine: var Engine, dt: float32) =
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
24 discard
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
25
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
26 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
27 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
28 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
29 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
30 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
31
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
32 when isMainModule:
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
33 randomize()
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
34 var myengine = igniteEngine("A lot of triangles")
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
35 const baseTriangle = [
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
36 Vec3([-0.1'f32, -0.1'f32, 1'f32]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
37 Vec3([ 0.1'f32, 0.1'f32, 1'f32]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
38 Vec3([-0.1'f32, 0.1'f32, 1'f32]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
39 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
40
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
41 var scene = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
42
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
43 for i in 1 .. 300:
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
44 var randommesh = new Mesh[VertexDataA]
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
45 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
46 let transform1 = randomtransform()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
47 randommesh.vertexData = VertexDataA(
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
48 position11: PositionAttribute[Vec2[float32]](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
49 data: @[
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
50 Vec2[float32](transform1 * baseTriangle[0]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
51 Vec2[float32](transform1 * baseTriangle[1]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
52 Vec2[float32](transform1 * baseTriangle[2]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
53 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
54 ),
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
55 color22: ColorAttribute[Vec3[float32]](
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
56 data: @[randomcolor1, randomcolor1, randomcolor1]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
57 )
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
58 )
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
59
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
60 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
61 let transform2 = randomtransform()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
62 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
63 randomindexedmesh.vertexData = VertexDataA(
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
64 position11: PositionAttribute[Vec2[float32]](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
65 data: @[
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
66 Vec2[float32](transform2 * baseTriangle[0]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
67 Vec2[float32](transform2 * baseTriangle[1]),
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
68 Vec2[float32](transform2 * baseTriangle[2]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
69 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
70 ),
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
71 color22: ColorAttribute[Vec3[float32]](
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
72 data: @[randomcolor2, randomcolor2, randomcolor2]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
73 )
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
74 )
483
73a0954beabd did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
Sam <sam@basx.dev>
parents: 482
diff changeset
75 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
76 var childthing = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
77 childthing.parts.add randommesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
78 childthing.parts.add randomindexedmesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
79 scene.children.add childthing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
80
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
81 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]()
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
82 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
83 var pipeline = setupPipeline[VertexDataA, float32, uint16](
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
84 myengine,
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
85 scene,
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
86 vertexShader,
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
87 fragmentShader
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
88 )
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
89 myengine.run(pipeline, globalUpdate)
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 483
diff changeset
90 pipeline.trash()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
diff changeset
91 myengine.trash()