Mercurial > games > semicongine
comparison examples/alotof_triangles.nim @ 60:c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 22 Jan 2023 22:46:53 +0700 |
parents | d7d9420ba675 |
children |
comparison
equal
deleted
inserted
replaced
59:d7d9420ba675 | 60:c57285d292b6 |
---|---|
1 import std/times | 1 import std/times |
2 import std/strutils | 2 import std/strutils |
3 import std/math | 3 import std/math |
4 import std/random | 4 import std/random |
5 import std/enumerate | |
6 | 5 |
7 import semicongine | 6 import semicongine |
8 | 7 |
9 type | 8 type |
10 VertexDataA = object | 9 VertexDataA = object |
17 discard | 16 discard |
18 | 17 |
19 proc randomtransform(): Mat33 = | 18 proc randomtransform(): Mat33 = |
20 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) | 19 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) |
21 let randomrotate = rotate2d(float32(rand(2 * PI))) | 20 let randomrotate = rotate2d(float32(rand(2 * PI))) |
22 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) | 21 let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand( |
22 1.6) - 0.8)) | |
23 result = randomtranslate * randomrotate * randomscale | 23 result = randomtranslate * randomrotate * randomscale |
24 | 24 |
25 when isMainModule: | 25 when isMainModule: |
26 randomize() | 26 randomize() |
27 var myengine = igniteEngine("A lot of triangles") | 27 var myengine = igniteEngine("A lot of triangles") |
28 const baseTriangle = [ | 28 const baseTriangle = [ |
29 Vec3([-0.1'f32, -0.1'f32, 1'f32]), | 29 Vec3([-0.1'f32, -0.1'f32, 1'f32]), |
30 Vec3([ 0.1'f32, 0.1'f32, 1'f32]), | 30 Vec3([0.1'f32, 0.1'f32, 1'f32]), |
31 Vec3([-0.1'f32, 0.1'f32, 1'f32]), | 31 Vec3([-0.1'f32, 0.1'f32, 1'f32]), |
32 ] | 32 ] |
33 | 33 |
34 var scene = new Thing | 34 var scene = newThing("scene") |
35 | 35 |
36 for i in 1 .. 300: | 36 for i in 1 .. 300: |
37 var randommesh = new Mesh[VertexDataA] | 37 var randommesh = new Mesh[VertexDataA] |
38 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) | 38 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) |
39 let transform1 = randomtransform() | 39 let transform1 = randomtransform() |
41 position11: PositionAttribute[Vec2]( | 41 position11: PositionAttribute[Vec2]( |
42 data: @[ | 42 data: @[ |
43 Vec2(transform1 * baseTriangle[0]), | 43 Vec2(transform1 * baseTriangle[0]), |
44 Vec2(transform1 * baseTriangle[1]), | 44 Vec2(transform1 * baseTriangle[1]), |
45 Vec2(transform1 * baseTriangle[2]), | 45 Vec2(transform1 * baseTriangle[2]), |
46 ] | 46 ] |
47 ), | 47 ), |
48 color22: ColorAttribute[Vec3]( | 48 color22: ColorAttribute[Vec3]( |
49 data: @[randomcolor1, randomcolor1, randomcolor1] | 49 data: @[randomcolor1, randomcolor1, randomcolor1] |
50 ) | 50 ) |
51 ) | 51 ) |
52 | 52 |
57 position11: PositionAttribute[Vec2]( | 57 position11: PositionAttribute[Vec2]( |
58 data: @[ | 58 data: @[ |
59 Vec2(transform2 * baseTriangle[0]), | 59 Vec2(transform2 * baseTriangle[0]), |
60 Vec2(transform2 * baseTriangle[1]), | 60 Vec2(transform2 * baseTriangle[1]), |
61 Vec2(transform2 * baseTriangle[2]), | 61 Vec2(transform2 * baseTriangle[2]), |
62 ] | 62 ] |
63 ), | 63 ), |
64 color22: ColorAttribute[Vec3]( | 64 color22: ColorAttribute[Vec3]( |
65 data: @[randomcolor2, randomcolor2, randomcolor2] | 65 data: @[randomcolor2, randomcolor2, randomcolor2] |
66 ) | 66 ) |
67 ) | 67 ) |
68 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] | 68 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] |
69 var childthing = new Thing | 69 scene.add newThing("randommesh", randommesh, randomindexedmesh) |
70 childthing.parts.add randommesh | |
71 childthing.parts.add randomindexedmesh | |
72 scene.children.add childthing | |
73 | 70 |
74 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() | 71 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() |
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 72 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
76 var pipeline = setupPipeline[VertexDataA, float32, uint16]( | 73 var pipeline = setupPipeline[VertexDataA, float32, uint16]( |
77 myengine, | 74 myengine, |