Mercurial > games > semicongine
comparison examples/input.nim @ 521:a25325bec7f2
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 | cd73e429fc99 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 520:cd73e429fc99 | 521:a25325bec7f2 | 
|---|---|
| 1 import std/strutils | |
| 1 import std/times | 2 import std/times | 
| 2 import std/strutils | |
| 3 import std/enumerate | |
| 4 | 3 | 
| 5 import semicongine | 4 import semicongine | 
| 6 | 5 | 
| 7 type | 6 type | 
| 8 # define type of vertex | 7 # define type of vertex | 
| 9 VertexDataA = object | 8 VertexDataA = object | 
| 10 position: PositionAttribute[Vec2] | 9 position: PositionAttribute[Vec2] | 
| 11 color: ColorAttribute[Vec3] | 10 color: ColorAttribute[Vec3] | 
| 12 iscursor: GenericAttribute[int32] | 11 # transform: ModelTransformAttribute | 
| 12 # TODO: make this somehow a single vertex attribute | |
| 13 m1: GenericInstanceAttribute[Vec4] | |
| 14 m2: GenericInstanceAttribute[Vec4] | |
| 15 m3: GenericInstanceAttribute[Vec4] | |
| 16 m4: GenericInstanceAttribute[Vec4] | |
| 13 Uniforms = object | 17 Uniforms = object | 
| 14 projection: Descriptor[Mat44] | 18 projection: Descriptor[Mat44] | 
| 15 cursor: Descriptor[Vec2] | 19 cursor: Descriptor[Vec2] | 
| 16 | 20 | 
| 17 var | 21 var | 
| 18 pipeline: RenderPipeline[VertexDataA, Uniforms] | 22 pipeline: RenderPipeline[VertexDataA, Uniforms] | 
| 19 uniforms: Uniforms | 23 uniforms: Uniforms | 
| 24 scene: Thing | |
| 25 time: float | |
| 20 | 26 | 
| 21 | 27 | 
| 22 proc globalUpdate(engine: var Engine, dt: float32) = | 28 proc globalUpdate(engine: var Engine, dt: float32) = | 
| 23 uniforms.cursor.value[0] = float32(engine.input.mouseX) | 29 time += dt | 
| 24 uniforms.cursor.value[1] = float32(engine.input.mouseY) | 30 uniforms.cursor.value = engine.input.mousePos | 
| 25 uniforms.projection.value = ortho[float32]( | 31 uniforms.projection.value = ortho[float32]( | 
| 26 0'f32, float32(engine.vulkan.frameDimension.width), | 32 0'f32, float32(engine.vulkan.frameSize.x), | 
| 27 0'f32, float32(engine.vulkan.frameDimension.height), | 33 0'f32, float32(engine.vulkan.frameSize.y), | 
| 28 0'f32, 1'f32, | 34 0'f32, 1'f32, | 
| 29 ) | 35 ) | 
| 30 echo uniforms.projection.value | 36 engine.vulkan.device.updateUniformData(pipeline, uniforms) | 
| 31 # echo uniforms.projection | |
| 32 for buffer in pipeline.uniformBuffers: | |
| 33 buffer.updateData(uniforms) | |
| 34 | 37 | 
| 35 # vertex data (types must match the above VertexAttributes) | 38 let cursor = firstPartWithName[Mesh[VertexDataA]](scene, "cursor") | 
| 39 if cursor != nil: | |
| 40 for c in cursor.vertexData.color.data.mitems: | |
| 41 c[1] = (sin(time * 8) * 0.5 + 0.5) * 0.2 | |
| 42 c[2] = (sin(time * 8) * 0.5 + 0.5) * 0.2 | |
| 43 engine.vulkan.device.updateVertexData(cursor.vertexData.color) | |
| 44 var trans = Unit44 * translate3d(engine.input.mousePos.x, | |
| 45 engine.input.mousePos.y, 0'f32) | |
| 46 cursor.vertexData.m1.data = @[trans.col(0)] | |
| 47 cursor.vertexData.m2.data = @[trans.col(1)] | |
| 48 cursor.vertexData.m3.data = @[trans.col(2)] | |
| 49 cursor.vertexData.m4.data = @[trans.col(3)] | |
| 50 engine.vulkan.device.updateVertexData(cursor.vertexData.m1) | |
| 51 engine.vulkan.device.updateVertexData(cursor.vertexData.m2) | |
| 52 engine.vulkan.device.updateVertexData(cursor.vertexData.m3) | |
| 53 engine.vulkan.device.updateVertexData(cursor.vertexData.m4) | |
| 54 | |
| 55 | |
| 36 const | 56 const | 
| 37 shape = @[ | 57 shape = @[ | 
| 38 Vec2([- 1'f32, - 1'f32]), | 58 Vec2([ - 1'f32, - 1'f32]), | 
| 39 Vec2([ 1'f32, - 1'f32]), | 59 Vec2([1'f32, - 1'f32]), | 
| 40 Vec2([-0.3'f32, -0.3'f32]), | 60 Vec2([-0.3'f32, -0.3'f32]), | 
| 41 Vec2([-0.3'f32, -0.3'f32]), | 61 Vec2([-0.3'f32, -0.3'f32]), | 
| 42 Vec2([- 1'f32, 1'f32]), | 62 Vec2([ - 1'f32, 1'f32]), | 
| 43 Vec2([- 1'f32, - 1'f32]), | 63 Vec2([ - 1'f32, - 1'f32]), | 
| 44 ] | 64 ] | 
| 45 colors = @[ | 65 colors = @[ | 
| 46 Vec3([1'f32, 0'f32, 0'f32]), | 66 Vec3([1'f32, 0'f32, 0'f32]), | 
| 47 Vec3([1'f32, 0'f32, 0'f32]), | 67 Vec3([1'f32, 0'f32, 0'f32]), | 
| 48 Vec3([1'f32, 0'f32, 0'f32]), | 68 Vec3([1'f32, 0'f32, 0'f32]), | 
| 52 ] | 72 ] | 
| 53 | 73 | 
| 54 when isMainModule: | 74 when isMainModule: | 
| 55 var myengine = igniteEngine("Input") | 75 var myengine = igniteEngine("Input") | 
| 56 | 76 | 
| 57 # build a single-object scene graph | |
| 58 var cursor = new Thing | |
| 59 var cursormesh = new Mesh[VertexDataA] | 77 var cursormesh = new Mesh[VertexDataA] | 
| 60 cursormesh.vertexData = VertexDataA( | 78 cursormesh.vertexData = VertexDataA( | 
| 61 position: PositionAttribute[Vec2](data: shape), | 79 position: PositionAttribute[Vec2](data: shape, useOnDeviceMemory: true), | 
| 62 color: ColorAttribute[Vec3](data: colors), | 80 color: ColorAttribute[Vec3](data: colors), | 
| 63 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), | 81 # transform: ModelTransformAttribute(data: @[Unit44]), | 
| 82 m1: GenericInstanceAttribute[Vec4](data: @[Unit44.row(0)]), | |
| 83 m2: GenericInstanceAttribute[Vec4](data: @[Unit44.row(1)]), | |
| 84 m3: GenericInstanceAttribute[Vec4](data: @[Unit44.row(2)]), | |
| 85 m4: GenericInstanceAttribute[Vec4](data: @[Unit44.row(3)]), | |
| 64 ) | 86 ) | 
| 65 # transform the cursor a bit to make it look nice | 87 # transform the cursor a bit to make it look nice | 
| 66 for i in 0 ..< cursormesh.vertexData.position.data.len: | 88 for i in 0 ..< cursormesh.vertexData.position.data.len: | 
| 67 let cursorscale = ( | 89 let cursorscale = ( | 
| 68 scale2d(20'f32, 20'f32) * | 90 scale2d(20'f32, 20'f32) * | 
| 69 translate2d(1'f32, 1'f32) * | 91 translate2d(1'f32, 1'f32) * | 
| 70 rotate2d(-float32(PI) / 4'f32) * | 92 rotate2d(-float32(PI) / 4'f32) * | 
| 71 scale2d(0.5'f32, 1'f32) * | 93 scale2d(0.5'f32, 1'f32) * | 
| 72 rotate2d(float32(PI) / 4'f32) | 94 rotate2d(float32(PI) / 4'f32) | 
| 73 ) | 95 ) | 
| 74 let pos = Vec3([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32]) | 96 let pos = Vec3([cursormesh.vertexData.position.data[i][0], | 
| 97 cursormesh.vertexData.position.data[i][1], 1'f32]) | |
| 75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy | 98 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy | 
| 76 cursor.parts.add cursormesh | |
| 77 | 99 | 
| 78 var box = new Thing | |
| 79 var boxmesh = new Mesh[VertexDataA] | 100 var boxmesh = new Mesh[VertexDataA] | 
| 80 boxmesh.vertexData = VertexDataA( | 101 boxmesh.vertexData = VertexDataA( | 
| 81 position: PositionAttribute[Vec2](data: shape), | 102 position: PositionAttribute[Vec2](data: shape), | 
| 82 color: ColorAttribute[Vec3](data: colors), | 103 color: ColorAttribute[Vec3](data: colors), | 
| 83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), | 104 # transform: ModelTransformAttribute(data: @[Unit44]), | 
| 105 m1: GenericInstanceAttribute[Vec4](data: @[Unit44.row(0)]), | |
| 106 m2: GenericInstanceAttribute[Vec4](data: @[Unit44.row(1)]), | |
| 107 m3: GenericInstanceAttribute[Vec4](data: @[Unit44.row(2)]), | |
| 108 m4: GenericInstanceAttribute[Vec4](data: @[Unit44.row(3)]), | |
| 84 ) | 109 ) | 
| 110 for i in 0 ..< boxmesh.vertexData.position.data.len: | |
| 111 let boxscale = translate2d(100'f32, 100'f32) * scale2d(100'f32, 100'f32) | |
| 112 let pos = Vec3([boxmesh.vertexData.position.data[i][0], | |
| 113 boxmesh.vertexData.position.data[i][1], 1'f32]) | |
| 114 boxmesh.vertexData.position.data[i] = (boxscale * pos).xy | |
| 115 echo boxmesh.vertexData.position.data | |
| 85 | 116 | 
| 86 var scene = new Thing | 117 scene = newThing("scene") | 
| 87 scene.children.add cursor | 118 scene.add newThing("cursor", cursormesh) | 
| 119 scene.add newThing("a box", boxmesh, newTransform(Unit44), newTransform( | |
| 120 translate3d(1'f32, 0'f32, 0'f32))) | |
| 121 scene.add newTransform(scale3d(1.5'f32, 1.5'f32, 1.5'f32)) | |
| 88 | 122 | 
| 89 # upload data, prepare shaders, etc | 123 # upload data, prepare shaders, etc | 
| 90 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" | 124 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" | 
| 91 out_position = uniforms.projection * vec4(in_position + (uniforms.cursor * iscursor), 0, 1); | 125 mat4 mat = mat4(m1, m2, m3, m4); | 
| 126 out_position = uniforms.projection * mat * vec4(position, 0, 1); | |
| 92 """) | 127 """) | 
| 128 echo vertexShader | |
| 93 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 129 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 
| 94 echo vertexShader | |
| 95 echo fragmentShader | |
| 96 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | 130 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | 
| 97 myengine, | 131 myengine, | 
| 98 scene, | 132 scene, | 
| 99 vertexShader, | 133 vertexShader, | 
| 100 fragmentShader | 134 fragmentShader | 
