Mercurial > games > semicongine
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 |
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 | 10 position: PositionAttribute[TVec2[float32]] |
11 color: ColorAttribute[TVec3[float32]] | |
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 | 14 projection: Descriptor[TMat44[float32]] |
518 | 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 | 38 TVec2([- 1'f32, - 1'f32]), |
39 TVec2([ 1'f32, - 1'f32]), | |
40 TVec2([-0.3'f32, -0.3'f32]), | |
41 TVec2([-0.3'f32, -0.3'f32]), | |
42 TVec2([- 1'f32, 1'f32]), | |
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 | 46 TVec3([1'f32, 0'f32, 0'f32]), |
47 TVec3([1'f32, 0'f32, 0'f32]), | |
48 TVec3([1'f32, 0'f32, 0'f32]), | |
49 TVec3([0.8'f32, 0'f32, 0'f32]), | |
50 TVec3([0.8'f32, 0'f32, 0'f32]), | |
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 | 59 var cursormesh = new Mesh[VertexDataA] |
60 cursormesh.vertexData = VertexDataA( | |
61 position: PositionAttribute[TVec2[float32]](data: shape), | |
62 color: ColorAttribute[TVec3[float32]](data: colors), | |
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 | 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 | 74 let pos = TVec3[float32]([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32]) |
75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy | |
76 cursor.parts.add cursormesh | |
77 | |
78 var box = new Thing | |
79 var boxmesh = new Mesh[VertexDataA] | |
80 boxmesh.vertexData = VertexDataA( | |
81 position: PositionAttribute[TVec2[float32]](data: shape), | |
82 color: ColorAttribute[TVec3[float32]](data: colors), | |
83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), | |
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 | 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() |