annotate examples/input.nim @ 519:03a94b905f1a

did: refactor Matrix names
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 16:36:52 +0700
parents 5d406c17bbcb
children d7d9420ba675
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
499
3f1111f3b9f8 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
3f1111f3b9f8 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
3f1111f3b9f8 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
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
4
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
5 import semicongine
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
6
3f1111f3b9f8 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
3f1111f3b9f8 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
3f1111f3b9f8 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
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
10 position: PositionAttribute[TVec2[float32]]
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
11 color: ColorAttribute[TVec3[float32]]
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
12 iscursor: GenericAttribute[int32]
499
3f1111f3b9f8 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
519
03a94b905f1a did: refactor Matrix names
Sam <sam@basx.dev>
parents: 518
diff changeset
14 projection: Descriptor[TMat44[float32]]
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
15 cursor: Descriptor[TVec2[float32]]
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
16
3f1111f3b9f8 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
3f1111f3b9f8 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]
3f1111f3b9f8 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
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
20
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
21
3f1111f3b9f8 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) =
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
23 uniforms.cursor.value[0] = float32(engine.input.mouseX)
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
24 uniforms.cursor.value[1] = float32(engine.input.mouseY)
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
25 uniforms.projection.value = ortho[float32](
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
26 0'f32, float32(engine.vulkan.frameDimension.width),
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
27 0'f32, float32(engine.vulkan.frameDimension.height),
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
28 0'f32, 1'f32,
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
29 )
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
30 echo uniforms.projection.value
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
31 # echo uniforms.projection
499
3f1111f3b9f8 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:
3f1111f3b9f8 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)
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
34
3f1111f3b9f8 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)
3f1111f3b9f8 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
3f1111f3b9f8 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 = @[
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
38 TVec2([- 1'f32, - 1'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
39 TVec2([ 1'f32, - 1'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
40 TVec2([-0.3'f32, -0.3'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
41 TVec2([-0.3'f32, -0.3'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
42 TVec2([- 1'f32, 1'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
43 TVec2([- 1'f32, - 1'f32]),
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
44 ]
3f1111f3b9f8 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 = @[
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
46 TVec3([1'f32, 0'f32, 0'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
47 TVec3([1'f32, 0'f32, 0'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
48 TVec3([1'f32, 0'f32, 0'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
49 TVec3([0.8'f32, 0'f32, 0'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
50 TVec3([0.8'f32, 0'f32, 0'f32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
51 TVec3([0.8'f32, 0'f32, 0'f32]),
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
52 ]
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
53
3f1111f3b9f8 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:
3f1111f3b9f8 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")
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
56
3f1111f3b9f8 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
3f1111f3b9f8 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
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
59 var cursormesh = new Mesh[VertexDataA]
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
60 cursormesh.vertexData = VertexDataA(
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
61 position: PositionAttribute[TVec2[float32]](data: shape),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
62 color: ColorAttribute[TVec3[float32]](data: colors),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
63 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
64 )
3f1111f3b9f8 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
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
66 for i in 0 ..< cursormesh.vertexData.position.data.len:
499
3f1111f3b9f8 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 = (
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
68 scale2d(20'f32, 20'f32) *
499
3f1111f3b9f8 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) *
3f1111f3b9f8 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) *
3f1111f3b9f8 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) *
3f1111f3b9f8 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)
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
73 )
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
74 let pos = TVec3[float32]([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32])
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
76 cursor.parts.add cursormesh
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
77
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
78 var box = new Thing
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
79 var boxmesh = new Mesh[VertexDataA]
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
80 boxmesh.vertexData = VertexDataA(
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
81 position: PositionAttribute[TVec2[float32]](data: shape),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
82 color: ColorAttribute[TVec3[float32]](data: colors),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
84 )
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
85
3f1111f3b9f8 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
3f1111f3b9f8 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
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
88
3f1111f3b9f8 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
3f1111f3b9f8 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]("""
518
5d406c17bbcb did: refactor Vector names
Sam <sam@basx.dev>
parents: 517
diff changeset
91 out_position = uniforms.projection * vec4(in_position + (uniforms.cursor * iscursor), 0, 1);
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
92 """)
3f1111f3b9f8 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]()
3f1111f3b9f8 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
3f1111f3b9f8 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
3f1111f3b9f8 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](
3f1111f3b9f8 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,
3f1111f3b9f8 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,
3f1111f3b9f8 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,
3f1111f3b9f8 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
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
101 )
3f1111f3b9f8 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
3f1111f3b9f8 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)
3f1111f3b9f8 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()
3f1111f3b9f8 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()