comparison examples/input.nim @ 517:836790efab48

did: cleanup main namespace, add: better coordinate handling in input example
author Sam <sam@basx.dev>
date Fri, 20 Jan 2023 00:41:55 +0700
parents e89fceb5a3a2
children 547f3a374271
comparison
equal deleted inserted replaced
516:fe0cad0ecf67 517:836790efab48
1 import std/times 1 import std/times
2 import std/strutils 2 import std/strutils
3 import std/enumerate 3 import std/enumerate
4 4
5 import semicongine/engine 5 import semicongine
6 import semicongine/math/vector
7 import semicongine/math/matrix
8 import semicongine/vertex
9 import semicongine/descriptor
10 import semicongine/mesh
11 import semicongine/thing
12 import semicongine/shader
13 import semicongine/buffer
14 6
15 type 7 type
16 # define type of vertex 8 # define type of vertex
17 VertexDataA = object 9 VertexDataA = object
18 position: PositionAttribute[Vec2[float32]] 10 position: PositionAttribute[Vec2[float32]]
19 color: ColorAttribute[Vec3[float32]] 11 color: ColorAttribute[Vec3[float32]]
12 translate: InstanceAttribute[Vec2[float32]]
20 Uniforms = object 13 Uniforms = object
14 projection: Descriptor[Mat44[float32]]
21 cursor: Descriptor[Vec2[float32]] 15 cursor: Descriptor[Vec2[float32]]
22 aspect: Descriptor[float32]
23 16
24 var 17 var
25 pipeline: RenderPipeline[VertexDataA, Uniforms] 18 pipeline: RenderPipeline[VertexDataA, Uniforms]
26 uniforms: Uniforms 19 uniforms: Uniforms
27 uniforms.aspect.value = 1
28 20
29 21
30 proc globalUpdate(engine: var Engine, dt: float32) = 22 proc globalUpdate(engine: var Engine, dt: float32) =
31 uniforms.aspect.value = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width) 23 uniforms.cursor.value[0] = float32(engine.input.mouseX)
32 uniforms.cursor.value[0] = ((float32(engine.input.mouseX) / float32(engine.vulkan.frameDimension.width)) * 2'f32 ) - 1'f32 24 uniforms.cursor.value[1] = float32(engine.input.mouseY)
33 uniforms.cursor.value[1] = ((float32(engine.input.mouseY) / float32(engine.vulkan.frameDimension.height)) * 2'f32 ) - 1'f32 25 uniforms.projection.value = ortho[float32](
26 0'f32, float32(engine.vulkan.frameDimension.width),
27 0'f32, float32(engine.vulkan.frameDimension.height),
28 0'f32, 1'f32,
29 )
30 echo uniforms.projection.value
31 # echo uniforms.projection
34 for buffer in pipeline.uniformBuffers: 32 for buffer in pipeline.uniformBuffers:
35 buffer.updateData(uniforms) 33 buffer.updateData(uniforms)
36 34
37 # vertex data (types must match the above VertexAttributes) 35 # vertex data (types must match the above VertexAttributes)
38 const 36 const
60 var cursor = new Thing 58 var cursor = new Thing
61 var cursorpart = new Mesh[VertexDataA] 59 var cursorpart = new Mesh[VertexDataA]
62 cursorpart.vertexData = VertexDataA( 60 cursorpart.vertexData = VertexDataA(
63 position: PositionAttribute[Vec2[float32]](data: shape), 61 position: PositionAttribute[Vec2[float32]](data: shape),
64 color: ColorAttribute[Vec3[float32]](data: colors), 62 color: ColorAttribute[Vec3[float32]](data: colors),
63 translate: InstanceAttribute[Vec2[float32]](data: @[Vec2[float32]([100'f32, 100'f32])]),
65 ) 64 )
66 # transform the cursor a bit to make it look nice 65 # transform the cursor a bit to make it look nice
67 for i in 0 ..< cursorpart.vertexData.position.data.len: 66 for i in 0 ..< cursorpart.vertexData.position.data.len:
68 let cursorscale = ( 67 let cursorscale = (
69 scale2d(0.07'f32, 0.07'f32) * 68 scale2d(20'f32, 20'f32) *
70 translate2d(1'f32, 1'f32) * 69 translate2d(1'f32, 1'f32) *
71 rotate2d(-float32(PI) / 4'f32) * 70 rotate2d(-float32(PI) / 4'f32) *
72 scale2d(0.5'f32, 1'f32) * 71 scale2d(0.5'f32, 1'f32) *
73 rotate2d(float32(PI) / 4'f32) 72 rotate2d(float32(PI) / 4'f32)
74 ) 73 )
79 var scene = new Thing 78 var scene = new Thing
80 scene.children.add cursor 79 scene.children.add cursor
81 80
82 # upload data, prepare shaders, etc 81 # upload data, prepare shaders, etc
83 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" 82 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
84 out_position.x = in_position.x * uniforms.aspect + uniforms.cursor.x; 83 out_position = uniforms.projection * vec4(in_position + uniforms.cursor, 0, 1);
85 out_position.y = in_position.y + uniforms.cursor.y;
86 """) 84 """)
87 const fragmentShader = generateFragmentShaderCode[VertexDataA]() 85 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
88 echo vertexShader 86 echo vertexShader
89 echo fragmentShader 87 echo fragmentShader
90 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( 88 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](