comparison examples/E04_input.nim @ 797:812b5e28f441

did: update examples to work with improved scenegraph/material api, notice removed complexity!
author Sam <sam@basx.dev>
date Tue, 05 Sep 2023 00:28:35 +0700
parents 754835bf175e
children 668eed376029
comparison
equal deleted inserted replaced
796:8084252b807a 797:812b5e28f441
1 import std/enumerate 1 import std/enumerate
2 import std/tables
2 import std/typetraits 3 import std/typetraits
3 import std/math 4 import std/math
4 5
5 import ../src/semicongine 6 import ../src/semicongine
6 7
105 for i in 0 ..< positions.len: 106 for i in 0 ..< positions.len:
106 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) 107 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y)
107 108
108 # define mesh objects 109 # define mesh objects
109 var 110 var
111 material = Material(name: "default")
110 cursormesh = newMesh( 112 cursormesh = newMesh(
111 positions=positions, 113 positions=positions,
112 colors=arrow_colors, 114 colors=arrow_colors,
113 instanceCount=1, 115 material=material,
114 ) 116 )
115 keyboardmesh = newMesh( 117 keyboardmesh = newMesh(
116 positions=keyvertexpos, 118 positions=keyvertexpos,
117 colors=keyvertexcolor, 119 colors=keyvertexcolor,
118 indices=keymeshindices 120 indices=keymeshindices,
121 material=material,
119 ) 122 )
120 backgroundmesh = newMesh( 123 backgroundmesh = newMesh(
121 positions= @[ 124 positions= @[
122 newVec3f(0'f32, 0'f32), 125 newVec3f(0'f32, 0'f32),
123 newVec3f(1'f32, 0'f32), 126 newVec3f(1'f32, 0'f32),
129 backgroundColor, 132 backgroundColor,
130 backgroundColor, 133 backgroundColor,
131 backgroundColor, 134 backgroundColor,
132 ], 135 ],
133 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], 136 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
137 material=material,
134 ) 138 )
135 139
136 # define mesh objects 140 # define mesh objects
137 scene = newScene("scene", newEntity("scene")) 141 var keyboard_center = translate(
138 scene.root.add newEntity("background", {"mesh": Component(backgroundmesh)})
139 let keyboard = newEntity("keyboard", {"mesh": Component(keyboardmesh)})
140 keyboard.transform = translate3d(
141 -float32(rowWidth) / 2'f32, 142 -float32(rowWidth) / 2'f32,
142 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 143 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
143 0'f32 144 0'f32
144 ) 145 )
145 scene.root.add newEntity("keyboard-center", [], keyboard) 146 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh])
146 scene.root.add newEntity("cursor", {"mesh": Component(cursormesh)})
147 147
148 # shaders 148 # shaders
149 const 149 const
150 inputs = @[ 150 shaderConfiguration = createShaderConfiguration(
151 attr[Vec3f]("position"), 151 inputs=[
152 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 152 attr[Vec3f]("position"),
153 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), 153 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
154 ] 154 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
155 intermediate = @[attr[Vec4f]("outcolor")] 155 ],
156 uniforms = @[attr[Mat4]("projection")] 156 intermediates=[attr[Vec4f]("outcolor")],
157 outputs = @[attr[Vec4f]("color")] 157 uniforms=[attr[Mat4]("projection")],
158 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 158 outputs=[attr[Vec4f]("color")],
159 inputs=inputs,
160 intermediate=intermediate,
161 outputs=outputs,
162 uniforms=uniforms,
163 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", 159 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
164 fragmentCode="color = outcolor;", 160 fragmentCode="color = outcolor;",
165 ) 161 )
166 162
167 # set up rendering 163 # set up rendering
168 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) 164 myengine.initRenderer({"default": shaderConfiguration}.toTable)
169 myengine.addScene(scene, inputs, @[], transformAttribute="transform")
170 scene.addShaderGlobal("projection", Unit4f32) 165 scene.addShaderGlobal("projection", Unit4f32)
166 myengine.addScene(scene)
167 myengine.hideSystemCursor()
171 168
172 # mainloop 169 # mainloop
173 while myengine.updateInputs() == Running: 170 while myengine.updateInputs() == Running:
174 if myengine.windowWasResized(): 171 if myengine.windowWasResized():
175 setShaderGlobal(scene, "projection", 172 scene.setShaderGlobal("projection",
176 ortho( 173 ortho(
177 0, float32(myengine.getWindow().size[0]), 174 0, float32(myengine.getWindow().size[0]),
178 0, float32(myengine.getWindow().size[1]), 175 0, float32(myengine.getWindow().size[1]),
179 0, 1, 176 0, 1,
180 ) 177 )
181 ) 178 )
182 let 179 let
183 winsize = myengine.getWindow().size 180 winsize = myengine.getWindow().size
184 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) 181 center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
185 scene.root.firstWithName("keyboard-center").transform = center 182 keyboardmesh.transform = keyboard_center * center
186 scene.root.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32) 183 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32)
187 184
188 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) 185 let mousePos = translate(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
189 scene.root.firstWithName("cursor").transform = mousePos 186 cursormesh.transform = mousePos
190 187
191 var mesh = Mesh(scene.root.firstWithName("keyboard")["mesh"])
192 for (index, key) in enumerate(keyIndices): 188 for (index, key) in enumerate(keyIndices):
193 if myengine.keyWasPressed(key): 189 if myengine.keyWasPressed(key):
194 let baseIndex = uint32(index * 4) 190 let baseIndex = index * 4
195 mesh.updateMeshData("color", baseIndex + 0, activeColor) 191 keyboardmesh[].updateAttributeData("color", baseIndex + 0, activeColor)
196 mesh.updateMeshData("color", baseIndex + 1, activeColor) 192 keyboardmesh[].updateAttributeData("color", baseIndex + 1, activeColor)
197 mesh.updateMeshData("color", baseIndex + 2, activeColor) 193 keyboardmesh[].updateAttributeData("color", baseIndex + 2, activeColor)
198 mesh.updateMeshData("color", baseIndex + 3, activeColor) 194 keyboardmesh[].updateAttributeData("color", baseIndex + 3, activeColor)
199 if myengine.keyWasReleased(key): 195 if myengine.keyWasReleased(key):
200 let baseIndex = uint32(index * 4) 196 let baseIndex = index * 4
201 mesh.updateMeshData("color", baseIndex + 0, baseColor) 197 keyboardmesh[].updateAttributeData("color", baseIndex + 0, baseColor)
202 mesh.updateMeshData("color", baseIndex + 1, baseColor) 198 keyboardmesh[].updateAttributeData("color", baseIndex + 1, baseColor)
203 mesh.updateMeshData("color", baseIndex + 2, baseColor) 199 keyboardmesh[].updateAttributeData("color", baseIndex + 2, baseColor)
204 mesh.updateMeshData("color", baseIndex + 3, baseColor) 200 keyboardmesh[].updateAttributeData("color", baseIndex + 3, baseColor)
205 201
206 myengine.renderScene(scene) 202 myengine.renderScene(scene)
207 203
208 myengine.destroy() 204 myengine.destroy()