Mercurial > games > semicongine
comparison examples/input.nim @ 57:547f3a374271
did: refactor Vector names
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 16:13:32 +0700 |
parents | 94d7eed3f118 |
children | 8287a91e5d56 |
comparison
equal
deleted
inserted
replaced
56:94d7eed3f118 | 57:547f3a374271 |
---|---|
5 import semicongine | 5 import semicongine |
6 | 6 |
7 type | 7 type |
8 # define type of vertex | 8 # define type of vertex |
9 VertexDataA = object | 9 VertexDataA = object |
10 position: PositionAttribute[Vec2[float32]] | 10 position: PositionAttribute[TVec2[float32]] |
11 color: ColorAttribute[Vec3[float32]] | 11 color: ColorAttribute[TVec3[float32]] |
12 translate: InstanceAttribute[Vec2[float32]] | 12 iscursor: GenericAttribute[int32] |
13 Uniforms = object | 13 Uniforms = object |
14 projection: Descriptor[Mat44[float32]] | 14 projection: Descriptor[Mat44[float32]] |
15 cursor: Descriptor[Vec2[float32]] | 15 cursor: Descriptor[TVec2[float32]] |
16 | 16 |
17 var | 17 var |
18 pipeline: RenderPipeline[VertexDataA, Uniforms] | 18 pipeline: RenderPipeline[VertexDataA, Uniforms] |
19 uniforms: Uniforms | 19 uniforms: Uniforms |
20 | 20 |
33 buffer.updateData(uniforms) | 33 buffer.updateData(uniforms) |
34 | 34 |
35 # vertex data (types must match the above VertexAttributes) | 35 # vertex data (types must match the above VertexAttributes) |
36 const | 36 const |
37 shape = @[ | 37 shape = @[ |
38 Vec2([- 1'f32, - 1'f32]), | 38 TVec2([- 1'f32, - 1'f32]), |
39 Vec2([ 1'f32, - 1'f32]), | 39 TVec2([ 1'f32, - 1'f32]), |
40 Vec2([-0.3'f32, -0.3'f32]), | 40 TVec2([-0.3'f32, -0.3'f32]), |
41 Vec2([-0.3'f32, -0.3'f32]), | 41 TVec2([-0.3'f32, -0.3'f32]), |
42 Vec2([- 1'f32, 1'f32]), | 42 TVec2([- 1'f32, 1'f32]), |
43 Vec2([- 1'f32, - 1'f32]), | 43 TVec2([- 1'f32, - 1'f32]), |
44 ] | 44 ] |
45 colors = @[ | 45 colors = @[ |
46 Vec3([1'f32, 0'f32, 0'f32]), | 46 TVec3([1'f32, 0'f32, 0'f32]), |
47 Vec3([1'f32, 0'f32, 0'f32]), | 47 TVec3([1'f32, 0'f32, 0'f32]), |
48 Vec3([1'f32, 0'f32, 0'f32]), | 48 TVec3([1'f32, 0'f32, 0'f32]), |
49 Vec3([0.8'f32, 0'f32, 0'f32]), | 49 TVec3([0.8'f32, 0'f32, 0'f32]), |
50 Vec3([0.8'f32, 0'f32, 0'f32]), | 50 TVec3([0.8'f32, 0'f32, 0'f32]), |
51 Vec3([0.8'f32, 0'f32, 0'f32]), | 51 TVec3([0.8'f32, 0'f32, 0'f32]), |
52 ] | 52 ] |
53 | 53 |
54 when isMainModule: | 54 when isMainModule: |
55 var myengine = igniteEngine("Input") | 55 var myengine = igniteEngine("Input") |
56 | 56 |
57 # build a single-object scene graph | 57 # build a single-object scene graph |
58 var cursor = new Thing | 58 var cursor = new Thing |
59 var cursorpart = new Mesh[VertexDataA] | 59 var cursormesh = new Mesh[VertexDataA] |
60 cursorpart.vertexData = VertexDataA( | 60 cursormesh.vertexData = VertexDataA( |
61 position: PositionAttribute[Vec2[float32]](data: shape), | 61 position: PositionAttribute[TVec2[float32]](data: shape), |
62 color: ColorAttribute[Vec3[float32]](data: colors), | 62 color: ColorAttribute[TVec3[float32]](data: colors), |
63 translate: InstanceAttribute[Vec2[float32]](data: @[Vec2[float32]([100'f32, 100'f32])]), | 63 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), |
64 ) | 64 ) |
65 # transform the cursor a bit to make it look nice | 65 # transform the cursor a bit to make it look nice |
66 for i in 0 ..< cursorpart.vertexData.position.data.len: | 66 for i in 0 ..< cursormesh.vertexData.position.data.len: |
67 let cursorscale = ( | 67 let cursorscale = ( |
68 scale2d(20'f32, 20'f32) * | 68 scale2d(20'f32, 20'f32) * |
69 translate2d(1'f32, 1'f32) * | 69 translate2d(1'f32, 1'f32) * |
70 rotate2d(-float32(PI) / 4'f32) * | 70 rotate2d(-float32(PI) / 4'f32) * |
71 scale2d(0.5'f32, 1'f32) * | 71 scale2d(0.5'f32, 1'f32) * |
72 rotate2d(float32(PI) / 4'f32) | 72 rotate2d(float32(PI) / 4'f32) |
73 ) | 73 ) |
74 let pos = Vec3[float32]([cursorpart.vertexData.position.data[i][0], cursorpart.vertexData.position.data[i][1], 1'f32]) | 74 let pos = TVec3[float32]([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32]) |
75 cursorpart.vertexData.position.data[i] = (cursorscale * pos).xy | 75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy |
76 cursor.parts.add cursorpart | 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 ) | |
77 | 85 |
78 var scene = new Thing | 86 var scene = new Thing |
79 scene.children.add cursor | 87 scene.children.add cursor |
80 | 88 |
81 # upload data, prepare shaders, etc | 89 # upload data, prepare shaders, etc |
82 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" | 90 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
83 out_position = uniforms.projection * vec4(in_position + uniforms.cursor, 0, 1); | 91 out_position = uniforms.projection * vec4(in_position + (uniforms.cursor * iscursor), 0, 1); |
84 """) | 92 """) |
85 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 93 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
86 echo vertexShader | 94 echo vertexShader |
87 echo fragmentShader | 95 echo fragmentShader |
88 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | 96 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |