comparison examples/squares.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
18 pipeline: RenderPipeline[VertexDataA, Uniforms] 17 pipeline: RenderPipeline[VertexDataA, Uniforms]
19 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32)) 18 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
20 19
21 proc globalUpdate(engine: var Engine, dt: float32) = 20 proc globalUpdate(engine: var Engine, dt: float32) =
22 uniformdata.t.value += dt 21 uniformdata.t.value += dt
23 for buffer in pipeline.uniformBuffers: 22 pipeline.updateUniformValues(uniformdata)
24 buffer.updateData(uniformdata)
25 23
26 when isMainModule: 24 when isMainModule:
27 randomize() 25 randomize()
28 var myengine = igniteEngine("Squares") 26 var myengine = igniteEngine("Squares")
29 const 27 const
55 colors[vertIndex + 3] = color 53 colors[vertIndex + 3] = color
56 iValues[vertIndex + 0] = uint32(squareIndex) 54 iValues[vertIndex + 0] = uint32(squareIndex)
57 iValues[vertIndex + 1] = uint32(squareIndex) 55 iValues[vertIndex + 1] = uint32(squareIndex)
58 iValues[vertIndex + 2] = uint32(squareIndex) 56 iValues[vertIndex + 2] = uint32(squareIndex)
59 iValues[vertIndex + 3] = uint32(squareIndex) 57 iValues[vertIndex + 3] = uint32(squareIndex)
60 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] 58 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex +
61 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] 59 1), uint16(vertIndex + 2)]
60 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex +
61 3), uint16(vertIndex + 0)]
62 62
63 63
64 type PIndexedMesh = ref IndexedMesh[VertexDataA, uint16] # required so we can use ctor with ref/on heap 64 type PIndexedMesh = ref IndexedMesh[VertexDataA,
65 uint16] # required so we can use ctor with ref/on heap
65 var squaremesh = PIndexedMesh( 66 var squaremesh = PIndexedMesh(
66 vertexData: VertexDataA( 67 vertexData: VertexDataA(
67 position11: PositionAttribute[Vec2](data: @vertices), 68 position11: PositionAttribute[Vec2](data: @vertices),
68 color22: ColorAttribute[Vec3](data: @colors), 69 color22: ColorAttribute[Vec3](data: @colors),
69 index: GenericAttribute[uint32](data: @iValues), 70 index: GenericAttribute[uint32](data: @iValues),
70 ), 71 ),
71 indices: @indices 72 indices: @indices
72 ) 73 )
73 var scene = new Thing 74 var scene = newThing("scene", newThing("squares", squaremesh))
74 var childthing = new Thing
75 childthing.parts.add squaremesh
76 scene.children.add childthing
77 75
78 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]( 76 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
79 """ 77 """
80 float pos_weight = index / 100.0; // add some gamma correction? 78 float pos_weight = index / 100.0; // add some gamma correction?
81 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5; 79 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5;