comparison examples/input.nim @ 38:c3c963e7c1a6

did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author Sam <sam@basx.dev>
date Wed, 18 Jan 2023 09:52:03 +0700
parents
children 2771db8d4276
comparison
equal deleted inserted replaced
37:6859bcfabc62 38:c3c963e7c1a6
1 import std/times
2 import std/strutils
3 import std/enumerate
4
5 import zamikongine/engine
6 import zamikongine/math/vector
7 import zamikongine/math/matrix
8 import zamikongine/vertex
9 import zamikongine/descriptor
10 import zamikongine/mesh
11 import zamikongine/thing
12 import zamikongine/shader
13 import zamikongine/buffer
14
15 type
16 # define type of vertex
17 VertexDataA = object
18 position: PositionAttribute[Vec2[float32]]
19 color: ColorAttribute[Vec3[float32]]
20 Uniforms = object
21 cursor: Descriptor[Vec2[float32]]
22 aspect: Descriptor[float32]
23
24 var
25 pipeline: RenderPipeline[VertexDataA, Uniforms]
26 uniforms: Uniforms
27 uniforms.aspect.value = 1
28
29
30 proc globalUpdate(engine: var Engine, dt: float32) =
31 uniforms.aspect.value = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width)
32 uniforms.cursor.value[0] = ((float32(engine.input.mouseX) / float32(engine.vulkan.frameDimension.width)) * 2'f32 ) - 1'f32
33 uniforms.cursor.value[1] = ((float32(engine.input.mouseY) / float32(engine.vulkan.frameDimension.height)) * 2'f32 ) - 1'f32
34 for buffer in pipeline.uniformBuffers:
35 buffer.updateData(uniforms)
36
37 # vertex data (types must match the above VertexAttributes)
38 const
39 shape = @[
40 Vec2([- 1'f32, - 1'f32]),
41 Vec2([ 1'f32, - 1'f32]),
42 Vec2([-0.3'f32, -0.3'f32]),
43 Vec2([-0.3'f32, -0.3'f32]),
44 Vec2([- 1'f32, 1'f32]),
45 Vec2([- 1'f32, - 1'f32]),
46 ]
47 colors = @[
48 Vec3([1'f32, 0'f32, 0'f32]),
49 Vec3([1'f32, 0'f32, 0'f32]),
50 Vec3([1'f32, 0'f32, 0'f32]),
51 Vec3([0.8'f32, 0'f32, 0'f32]),
52 Vec3([0.8'f32, 0'f32, 0'f32]),
53 Vec3([0.8'f32, 0'f32, 0'f32]),
54 ]
55
56 when isMainModule:
57 var myengine = igniteEngine("Input")
58
59 # build a single-object scene graph
60 var cursor = new Thing
61 var cursorpart = new Mesh[VertexDataA]
62 cursorpart.vertexData = VertexDataA(
63 position: PositionAttribute[Vec2[float32]](data: shape),
64 color: ColorAttribute[Vec3[float32]](data: colors),
65 )
66 # transform the cursor a bit to make it look nice
67 for i in 0 ..< cursorpart.vertexData.position.data.len:
68 let cursorscale = (
69 scale2d(0.07'f32, 0.07'f32) *
70 translate2d(1'f32, 1'f32) *
71 rotate2d(-float32(PI) / 4'f32) *
72 scale2d(0.5'f32, 1'f32) *
73 rotate2d(float32(PI) / 4'f32)
74 )
75 let pos = Vec3[float32]([cursorpart.vertexData.position.data[i][0], cursorpart.vertexData.position.data[i][1], 1'f32])
76 cursorpart.vertexData.position.data[i] = (cursorscale * pos).xy
77 cursor.parts.add cursorpart
78
79 var scene = new Thing
80 scene.children.add cursor
81
82 # upload data, prepare shaders, etc
83 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
84 out_position.x = in_position.x * uniforms.aspect + uniforms.cursor.x;
85 out_position.y = in_position.y + uniforms.cursor.y;
86 """)
87 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
88 echo vertexShader
89 echo fragmentShader
90 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
91 myengine,
92 scene,
93 vertexShader,
94 fragmentShader
95 )
96 # show something
97 myengine.run(pipeline, globalUpdate)
98 pipeline.trash()
99 myengine.trash()