comparison 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
comparison
equal deleted inserted replaced
519:03a94b905f1a 520:cd73e429fc99
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[TVec2[float32]] 10 position: PositionAttribute[Vec2]
11 color: ColorAttribute[TVec3[float32]] 11 color: ColorAttribute[Vec3]
12 iscursor: GenericAttribute[int32] 12 iscursor: GenericAttribute[int32]
13 Uniforms = object 13 Uniforms = object
14 projection: Descriptor[TMat44[float32]] 14 projection: Descriptor[Mat44]
15 cursor: Descriptor[TVec2[float32]] 15 cursor: Descriptor[Vec2]
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 TVec2([- 1'f32, - 1'f32]), 38 Vec2([- 1'f32, - 1'f32]),
39 TVec2([ 1'f32, - 1'f32]), 39 Vec2([ 1'f32, - 1'f32]),
40 TVec2([-0.3'f32, -0.3'f32]), 40 Vec2([-0.3'f32, -0.3'f32]),
41 TVec2([-0.3'f32, -0.3'f32]), 41 Vec2([-0.3'f32, -0.3'f32]),
42 TVec2([- 1'f32, 1'f32]), 42 Vec2([- 1'f32, 1'f32]),
43 TVec2([- 1'f32, - 1'f32]), 43 Vec2([- 1'f32, - 1'f32]),
44 ] 44 ]
45 colors = @[ 45 colors = @[
46 TVec3([1'f32, 0'f32, 0'f32]), 46 Vec3([1'f32, 0'f32, 0'f32]),
47 TVec3([1'f32, 0'f32, 0'f32]), 47 Vec3([1'f32, 0'f32, 0'f32]),
48 TVec3([1'f32, 0'f32, 0'f32]), 48 Vec3([1'f32, 0'f32, 0'f32]),
49 TVec3([0.8'f32, 0'f32, 0'f32]), 49 Vec3([0.8'f32, 0'f32, 0'f32]),
50 TVec3([0.8'f32, 0'f32, 0'f32]), 50 Vec3([0.8'f32, 0'f32, 0'f32]),
51 TVec3([0.8'f32, 0'f32, 0'f32]), 51 Vec3([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 cursormesh = new Mesh[VertexDataA] 59 var cursormesh = new Mesh[VertexDataA]
60 cursormesh.vertexData = VertexDataA( 60 cursormesh.vertexData = VertexDataA(
61 position: PositionAttribute[TVec2[float32]](data: shape), 61 position: PositionAttribute[Vec2](data: shape),
62 color: ColorAttribute[TVec3[float32]](data: colors), 62 color: ColorAttribute[Vec3](data: colors),
63 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), 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 ..< cursormesh.vertexData.position.data.len: 66 for i in 0 ..< cursormesh.vertexData.position.data.len:
67 let cursorscale = ( 67 let cursorscale = (
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 = TVec3[float32]([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32]) 74 let pos = Vec3([cursormesh.vertexData.position.data[i][0], cursormesh.vertexData.position.data[i][1], 1'f32])
75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy 75 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy
76 cursor.parts.add cursormesh 76 cursor.parts.add cursormesh
77 77
78 var box = new Thing 78 var box = new Thing
79 var boxmesh = new Mesh[VertexDataA] 79 var boxmesh = new Mesh[VertexDataA]
80 boxmesh.vertexData = VertexDataA( 80 boxmesh.vertexData = VertexDataA(
81 position: PositionAttribute[TVec2[float32]](data: shape), 81 position: PositionAttribute[Vec2](data: shape),
82 color: ColorAttribute[TVec3[float32]](data: colors), 82 color: ColorAttribute[Vec3](data: colors),
83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]), 83 iscursor: GenericAttribute[int32](data: @[1'i32, 1'i32, 1'i32, 1'i32, 1'i32, 1'i32]),
84 ) 84 )
85 85
86 var scene = new Thing 86 var scene = new Thing
87 scene.children.add cursor 87 scene.children.add cursor