annotate examples/E04_input.nim @ 1139:114f395b9144

did: finish refactoring and updated all tests accordingly
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 14:58:25 +0700
parents 71315636ba82
children 5934c5615f13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
1 import std/enumerate
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
2 import std/tables
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
3 import std/typetraits
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
4 import std/math
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
5
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
6 import ../semicongine
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
7
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
8 const
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
9 arrow = @[
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
10 NewVec3f(-1, -1),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
11 NewVec3f(1, -1),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
12 NewVec3f(-0.3, -0.3),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
13 NewVec3f(-0.3, -0.3),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
14 NewVec3f(-1, 1),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
15 NewVec3f(-1, -1),
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
16 ]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
17 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
18 keyrows = (
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
19 [1.0, -0.6, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.1, 1.0, 1.0, 1.0],
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
20 [1.2, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.8, -0.1, 1.0, 1.0, 1.0],
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
21 [1.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.5, 1.0, 1.0, 1.0],
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
22 [2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
23 [2.6, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.8, -1.3, 1.0],
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
24 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0],
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
25 )
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
26 keyDimension = 50'f32
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
27 keyGap = 10'f32
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
28 backgroundColor = NewVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
29 baseColor = NewVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
30 activeColor = NewVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1)
530
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
31 arrow_colors = @[
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
32 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
33 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
34 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
35 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
36 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
37 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
38 ]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
39 keyIndices = [
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
40 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
41 ScrollLock, Pause,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
42
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
43 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
44
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
45 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
46 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
47 LetterRow1Extra2, Delete, End, PageDown,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
48
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
49 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
50 LetterRow2Extra3, Enter,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
51
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
52 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
53 LetterRow3Extra3, ShiftR, Up,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
54
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
55 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
56 ]
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
57
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
58 # build keyboard and cursor meshes
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
59 var
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
60 scene: Scene
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
61 keyvertexpos: seq[Vec3f]
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 664
diff changeset
62 keyvertexcolor: seq[Vec4f]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
63 keymeshindices: seq[array[3, uint16]]
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
64 rowpos = NewVec2f(0, 0)
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
65 i = 0'u16
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
66 firstRow = true
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
67 rowWidth = 0'f32
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
68 for row in keyrows.fields:
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
69 for key in row:
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
70 let keySpace = float32(keyDimension * key)
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
71 if key > 0:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
72 if keyIndices[i div 4] == Enter:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
73 keyvertexpos.add NewVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
74 keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap)
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
75 else:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
76 keyvertexpos.add NewVec3f(rowpos[0], rowpos[1])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
77 keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
78 keyvertexpos.add NewVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
79 keyvertexpos.add NewVec3f(rowpos[0], rowpos[1] + keyDimension)
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
80 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
81 keymeshindices.add [i, i + 1, i + 2]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
82 keymeshindices.add [i + 2, i + 3, i]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
83 rowpos[0] += keySpace + keyGap
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
84 i += 4
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
85 else:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
86 rowpos[0] += -keySpace + keyGap
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
87 if firstRow:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
88 rowWidth = rowpos[0]
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
89 rowpos[0] = 0
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32)
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
91 firstRow = false
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
92
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
93
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
94 when isMainModule:
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
95 var myengine = initEngine("Input")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
96
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
97 # transform the cursor a bit to make it look nice
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
98 let cursorscale = (
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
99 scale2d(20'f32, 20'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
100 translate2d(1'f32, 1'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
101 rotate2d(-float32(PI) / 4'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
102 scale2d(0.5'f32, 1'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
103 rotate2d(float32(PI) / 4'f32)
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
104 )
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
105 var positions = arrow
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
106 for i in 0 ..< positions.len:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
107 positions[i] = cursorscale * NewVec3f(positions[i].x, positions[i].y)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
108
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
109 # define mesh objects
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
110 var
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
111 matDef = MaterialType(name: "default", vertexAttributes: {
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
112 "position": Vec3F32,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
113 "color": Vec4F32,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
114 }.toTable)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
115 cursormesh = newMesh(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
116 positions = positions,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
117 colors = arrow_colors,
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
118 material = matDef.InitMaterialData(),
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
119 )
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
120 keyboardmesh = newMesh(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
121 positions = keyvertexpos,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
122 colors = keyvertexcolor,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
123 indices = keymeshindices,
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
124 material = matDef.InitMaterialData(),
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
125 )
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
126 backgroundmesh = newMesh(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
127 positions = @[
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
128 NewVec3f(0'f32, 0'f32),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
129 NewVec3f(1'f32, 0'f32),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
130 NewVec3f(1'f32, 1'f32),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
131 NewVec3f(0'f32, 1'f32),
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
132 ],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
133 colors = @[
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
134 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
135 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
136 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
137 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
138 ],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
139 indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
140 material = matDef.InitMaterialData(),
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
141 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
142
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
143 # define mesh objects
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
144 var keyboard_center = translate(
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
145 -float32(rowWidth) / 2'f32,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
146 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
147 0'f32
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
148 )
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
149 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh])
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
150
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
151 # shaders
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
152 const
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
153 shaderConfiguration = createShaderConfiguration(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
154 name = "default shader",
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
155 inputs = [
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
156 attr[Vec3f]("position"),
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
157 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
158 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
159 ],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
160 intermediates = [attr[Vec4f]("outcolor")],
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
161 uniforms = [attr[Mat4]("projection")],
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
162 outputs = [attr[Vec4f]("color")],
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
163 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
164 fragmentCode = "color = outcolor;",
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
165 )
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
166
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
167 # set up rendering
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
168 myengine.initRenderer({matDef: shaderConfiguration})
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
169 scene.addShaderGlobal("projection", Unit4f32)
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
170 myengine.loadScene(scene)
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
171 myengine.HideSystemCursor()
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
172
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
173 # mainloop
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
174 while myengine.UpdateInputs():
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
175 if WindowWasResized():
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
176 scene.setShaderGlobal("projection",
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 671
diff changeset
177 ortho(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
178 0, float32(myengine.GetWindow().size[0]),
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
179 0, float32(myengine.GetWindow().size[1]),
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 671
diff changeset
180 0, 1,
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
181 )
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
182 )
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
183 let
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
184 winsize = myengine.GetWindow().size
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
185 center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
186 keyboardmesh.transform = keyboard_center * center
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
187 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
188
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
189 let mousePos = translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32)
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
190 cursormesh.transform = mousePos
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
191
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
192 for (index, key) in enumerate(keyIndices):
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
193 if KeyWasPressed(key):
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
194 let baseIndex = index * 4
800
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
195 keyboardmesh["color", baseIndex + 0] = activeColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
196 keyboardmesh["color", baseIndex + 1] = activeColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
197 keyboardmesh["color", baseIndex + 2] = activeColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
198 keyboardmesh["color", baseIndex + 3] = activeColor
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
199 if KeyWasReleased(key):
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
200 let baseIndex = index * 4
800
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
201 keyboardmesh["color", baseIndex + 0] = baseColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
202 keyboardmesh["color", baseIndex + 1] = baseColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
203 keyboardmesh["color", baseIndex + 2] = baseColor
668eed376029 add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents: 797
diff changeset
204 keyboardmesh["color", baseIndex + 3] = baseColor
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
205
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
206 myengine.renderScene(scene)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
207
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
208 myengine.destroy()