comparison examples/alotof_triangles.nim @ 493:680c4b8ca28a

add: working implementation of uniforms
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 23:34:50 +0700
parents 54a1f8ee208e
children c3c963e7c1a6
comparison
equal deleted inserted replaced
492:d63d980fa3bb 493:680c4b8ca28a
1 import std/times 1 import std/times
2 import std/strutils
2 import std/math 3 import std/math
3 import std/random 4 import std/random
5 import std/enumerate
4 6
5 import zamikongine/engine 7 import zamikongine/engine
6 import zamikongine/math/vector 8 import zamikongine/math/vector
7 import zamikongine/math/matrix 9 import zamikongine/math/matrix
8 import zamikongine/vertex 10 import zamikongine/vertex
11 import zamikongine/descriptor
9 import zamikongine/mesh 12 import zamikongine/mesh
10 import zamikongine/thing 13 import zamikongine/thing
11 import zamikongine/shader 14 import zamikongine/shader
12 15
13 type 16 type
14 VertexDataA = object 17 VertexDataA = object
15 position11: VertexAttribute[Vec2[float32]] 18 position11: PositionAttribute[Vec2[float32]]
16 color22: VertexAttribute[Vec3[float32]] 19 color22: ColorAttribute[Vec3[float32]]
20 Uniforms = object
21 dt: Descriptor[float32]
17 22
18 proc globalUpdate(engine: var Engine, dt: Duration) = 23 proc globalUpdate(engine: var Engine, dt: float32) =
19 discard 24 discard
20 25
21 proc randomtransform(): Mat33[float32] = 26 proc randomtransform(): Mat33[float32] =
22 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) 27 let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5))
23 let randomrotate = rotate2d(float32(rand(2 * PI))) 28 let randomrotate = rotate2d(float32(rand(2 * PI)))
39 var randommesh = new Mesh[VertexDataA] 44 var randommesh = new Mesh[VertexDataA]
40 # TODO: create randomized position11 from baseTriangle with random transformation matrix 45 # TODO: create randomized position11 from baseTriangle with random transformation matrix
41 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) 46 let randomcolor1 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
42 let transform1 = randomtransform() 47 let transform1 = randomtransform()
43 randommesh.vertexData = VertexDataA( 48 randommesh.vertexData = VertexDataA(
44 position11: VertexAttribute[Vec2[float32]]( 49 position11: PositionAttribute[Vec2[float32]](
45 data: @[ 50 data: @[
46 Vec2[float32](transform1 * baseTriangle[0]), 51 Vec2[float32](transform1 * baseTriangle[0]),
47 Vec2[float32](transform1 * baseTriangle[1]), 52 Vec2[float32](transform1 * baseTriangle[1]),
48 Vec2[float32](transform1 * baseTriangle[2]), 53 Vec2[float32](transform1 * baseTriangle[2]),
49 ] 54 ]
50 ), 55 ),
51 color22: VertexAttribute[Vec3[float32]]( 56 color22: ColorAttribute[Vec3[float32]](
52 data: @[randomcolor1, randomcolor1, randomcolor1] 57 data: @[randomcolor1, randomcolor1, randomcolor1]
53 ) 58 )
54 ) 59 )
55 60
56 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]) 61 let randomcolor2 = Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))])
57 let transform2 = randomtransform() 62 let transform2 = randomtransform()
58 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] 63 var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
59 randomindexedmesh.vertexData = VertexDataA( 64 randomindexedmesh.vertexData = VertexDataA(
60 position11: VertexAttribute[Vec2[float32]]( 65 position11: PositionAttribute[Vec2[float32]](
61 data: @[ 66 data: @[
62 Vec2[float32](transform2 * baseTriangle[0]), 67 Vec2[float32](transform2 * baseTriangle[0]),
63 Vec2[float32](transform2 * baseTriangle[1]), 68 Vec2[float32](transform2 * baseTriangle[1]),
64 Vec2[float32](transform2 * baseTriangle[2]), 69 Vec2[float32](transform2 * baseTriangle[2]),
65 ] 70 ]
66 ), 71 ),
67 color22: VertexAttribute[Vec3[float32]]( 72 color22: ColorAttribute[Vec3[float32]](
68 data: @[randomcolor2, randomcolor2, randomcolor2] 73 data: @[randomcolor2, randomcolor2, randomcolor2]
69 ) 74 )
70 ) 75 )
71 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] 76 randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]]
72 var childthing = new Thing 77 var childthing = new Thing
73 childthing.parts.add randommesh 78 childthing.parts.add randommesh
74 childthing.parts.add randomindexedmesh 79 childthing.parts.add randomindexedmesh
75 scene.children.add childthing 80 scene.children.add childthing
76 81
77 var pipeline = setupPipeline[VertexDataA, float32, float32, uint16]( 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](
78 myengine, 92 myengine,
79 scene, 93 scene,
80 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"), 94 vertexShader,
81 generateFragmentShaderCode[VertexDataA]("main"), 95 fragmentShader
82 ) 96 )
83 myengine.run(pipeline, globalUpdate) 97 myengine.run(pipeline, globalUpdate)
84 pipeline.trash() 98 pipeline.trash()
85 myengine.trash() 99 myengine.trash()