annotate examples/input.nim @ 57:547f3a374271

did: refactor Vector names
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 16:13:32 +0700
parents 94d7eed3f118
children 8287a91e5d56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/times
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/enumerate
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
4
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
5 import semicongine
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
6
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
7 type
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
8 # define type of vertex
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
9 VertexDataA = object
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
10 position: PositionAttribute[TVec2[float32]]
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
11 color: ColorAttribute[TVec3[float32]]
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
12 iscursor: GenericAttribute[int32]
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
13 Uniforms = object
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
14 projection: Descriptor[Mat44[float32]]
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
15 cursor: Descriptor[TVec2[float32]]
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
16
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
17 var
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
18 pipeline: RenderPipeline[VertexDataA, Uniforms]
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
19 uniforms: Uniforms
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
20
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
21
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
22 proc globalUpdate(engine: var Engine, dt: float32) =
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
23 uniforms.cursor.value[0] = float32(engine.input.mouseX)
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
24 uniforms.cursor.value[1] = float32(engine.input.mouseY)
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
25 uniforms.projection.value = ortho[float32](
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
26 0'f32, float32(engine.vulkan.frameDimension.width),
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
27 0'f32, float32(engine.vulkan.frameDimension.height),
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
28 0'f32, 1'f32,
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
29 )
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
30 echo uniforms.projection.value
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
31 # echo uniforms.projection
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
32 for buffer in pipeline.uniformBuffers:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
33 buffer.updateData(uniforms)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
34
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
35 # vertex data (types must match the above VertexAttributes)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
36 const
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
37 shape = @[
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
38 TVec2([- 1'f32, - 1'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
39 TVec2([ 1'f32, - 1'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
40 TVec2([-0.3'f32, -0.3'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
41 TVec2([-0.3'f32, -0.3'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
42 TVec2([- 1'f32, 1'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
43 TVec2([- 1'f32, - 1'f32]),
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
44 ]
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
45 colors = @[
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
46 TVec3([1'f32, 0'f32, 0'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
47 TVec3([1'f32, 0'f32, 0'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
48 TVec3([1'f32, 0'f32, 0'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
49 TVec3([0.8'f32, 0'f32, 0'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
50 TVec3([0.8'f32, 0'f32, 0'f32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
51 TVec3([0.8'f32, 0'f32, 0'f32]),
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
52 ]
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
53
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
54 when isMainModule:
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
55 var myengine = igniteEngine("Input")
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
56
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
57 # build a single-object scene graph
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
58 var cursor = new Thing
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
59 var cursormesh = new Mesh[VertexDataA]
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
60 cursormesh.vertexData = VertexDataA(
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
61 position: PositionAttribute[TVec2[float32]](data: shape),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
62 color: ColorAttribute[TVec3[float32]](data: colors),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
63 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
64 )
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
65 # transform the cursor a bit to make it look nice
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
66 for i in 0 ..< cursormesh.vertexData.position.data.len:
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
67 let cursorscale = (
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
68 scale2d(20'f32, 20'f32) *
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
69 translate2d(1'f32, 1'f32) *
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
70 rotate2d(-float32(PI) / 4'f32) *
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
71 scale2d(0.5'f32, 1'f32) *
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
72 rotate2d(float32(PI) / 4'f32)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
73 )
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
74 let pos = TVec3[float32]([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32])
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
76 cursor.parts.add cursormesh
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
77
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
78 var box = new Thing
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
79 var boxmesh = new Mesh[VertexDataA]
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
80 boxmesh.vertexData = VertexDataA(
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
81 position: PositionAttribute[TVec2[float32]](data: shape),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
82 color: ColorAttribute[TVec3[float32]](data: colors),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
84 )
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
85
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
86 var scene = new Thing
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
87 scene.children.add cursor
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
88
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
89 # upload data, prepare shaders, etc
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
90 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
91 out_position = uniforms.projection * vec4(in_position + (uniforms.cursor * iscursor), 0, 1);
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
92 """)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
93 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
94 echo vertexShader
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
95 echo fragmentShader
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
96 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
97 myengine,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
98 scene,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
99 vertexShader,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
100 fragmentShader
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
101 )
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
102 # show something
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
103 myengine.run(pipeline, globalUpdate)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
104 pipeline.trash()
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
105 myengine.trash()