Mercurial > games > semicongine
annotate examples/input.nim @ 520:cd73e429fc99
did: use new vector and matrix names for simpler code
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 16:53:37 +0700 |
parents | 03a94b905f1a |
children | c57285d292b6 |
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 |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
10 position: PositionAttribute[Vec2] |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
11 color: ColorAttribute[Vec3] |
518 | 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 |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
14 projection: Descriptor[Mat44] |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
15 cursor: Descriptor[Vec2] |
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 = @[ |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
38 Vec2([- 1'f32, - 1'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
39 Vec2([ 1'f32, - 1'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
40 Vec2([-0.3'f32, -0.3'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
41 Vec2([-0.3'f32, -0.3'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
42 Vec2([- 1'f32, 1'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
43 Vec2([- 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 = @[ |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
46 Vec3([1'f32, 0'f32, 0'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
47 Vec3([1'f32, 0'f32, 0'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
48 Vec3([1'f32, 0'f32, 0'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
49 Vec3([0.8'f32, 0'f32, 0'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
50 Vec3([0.8'f32, 0'f32, 0'f32]), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
51 Vec3([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( | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
61 position: PositionAttribute[Vec2](data: shape), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
62 color: ColorAttribute[Vec3](data: colors), |
518 | 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 ) |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
74 let pos = Vec3([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32]) |
518 | 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( | |
520
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
81 position: PositionAttribute[Vec2](data: shape), |
cd73e429fc99
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
519
diff
changeset
|
82 color: ColorAttribute[Vec3](data: colors), |
518 | 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() |