Mercurial > games > semicongine
comparison examples/hello_cube.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 |
---|---|
15 import semicongine | 15 import semicongine |
16 | 16 |
17 type | 17 type |
18 # define type of vertex | 18 # define type of vertex |
19 VertexDataA = object | 19 VertexDataA = object |
20 position: PositionAttribute[TVec3[float32]] | 20 position: PositionAttribute[Vec3] |
21 color: ColorAttribute[TVec3[float32]] | 21 color: ColorAttribute[Vec3] |
22 Uniforms = object | 22 Uniforms = object |
23 model: Descriptor[TMat44[float32]] | 23 model: Descriptor[Mat44] |
24 view: Descriptor[TMat44[float32]] | 24 view: Descriptor[Mat44] |
25 projection: Descriptor[TMat44[float32]] | 25 projection: Descriptor[Mat44] |
26 | 26 |
27 var | 27 var |
28 pipeline: RenderPipeline[VertexDataA, Uniforms] | 28 pipeline: RenderPipeline[VertexDataA, Uniforms] |
29 uniforms:Uniforms | 29 uniforms:Uniforms |
30 t: float32 | 30 t: float32 |
34 let ratio = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width) | 34 let ratio = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width) |
35 t += dt | 35 t += dt |
36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32) # * rotate3d(float32(PI), Yf32) | 36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32) # * rotate3d(float32(PI), Yf32) |
37 | 37 |
38 uniforms.view.value = Unit44f32 | 38 uniforms.view.value = Unit44f32 |
39 uniforms.projection.value = TMat44[float32](data:[ | 39 uniforms.projection.value = Mat44(data:[ |
40 ratio, 0'f32, 0'f32, 0'f32, | 40 ratio, 0'f32, 0'f32, 0'f32, |
41 0'f32, 1'f32, 0'f32, 0'f32, | 41 0'f32, 1'f32, 0'f32, 0'f32, |
42 0'f32, 0'f32, 1'f32, 0'f32, | 42 0'f32, 0'f32, 1'f32, 0'f32, |
43 0'f32, 0'f32, 0'f32, 1'f32, | 43 0'f32, 0'f32, 0'f32, 1'f32, |
44 ]) | 44 ]) |
45 uniforms.projection.value = perspective(float32(PI / 4), float32(engine.vulkan.frameDimension.width) / float32(engine.vulkan.frameDimension.height), 0.1'f32, 100'f32) | 45 uniforms.projection.value = perspective(float32(PI / 4), float32(engine.vulkan.frameDimension.width) / float32(engine.vulkan.frameDimension.height), 0.1'f32, 100'f32) |
46 for buffer in pipeline.uniformBuffers: | 46 for buffer in pipeline.uniformBuffers: |
47 buffer.updateData(uniforms) | 47 buffer.updateData(uniforms) |
48 | 48 |
49 const | 49 const |
50 TopLeftFront = TVec3([ -0.5'f32, -0.5'f32, -0.5'f32]) | 50 TopLeftFront = Vec3([ -0.5'f32, -0.5'f32, -0.5'f32]) |
51 TopRightFront = TVec3([ 0.5'f32, -0.5'f32, -0.5'f32]) | 51 TopRightFront = Vec3([ 0.5'f32, -0.5'f32, -0.5'f32]) |
52 BottomRightFront = TVec3([ 0.5'f32, 0.5'f32, -0.5'f32]) | 52 BottomRightFront = Vec3([ 0.5'f32, 0.5'f32, -0.5'f32]) |
53 BottomLeftFront = TVec3([ -0.5'f32, 0.5'f32, -0.5'f32]) | 53 BottomLeftFront = Vec3([ -0.5'f32, 0.5'f32, -0.5'f32]) |
54 TopLeftBack = TVec3([ 0.5'f32, -0.5'f32, 0.5'f32]) | 54 TopLeftBack = Vec3([ 0.5'f32, -0.5'f32, 0.5'f32]) |
55 TopRightBack = TVec3([ -0.5'f32, -0.5'f32, 0.5'f32]) | 55 TopRightBack = Vec3([ -0.5'f32, -0.5'f32, 0.5'f32]) |
56 BottomRightBack = TVec3([ -0.5'f32, 0.5'f32, 0.5'f32]) | 56 BottomRightBack = Vec3([ -0.5'f32, 0.5'f32, 0.5'f32]) |
57 BottomLeftBack = TVec3([ 0.5'f32, 0.5'f32, 0.5'f32]) | 57 BottomLeftBack = Vec3([ 0.5'f32, 0.5'f32, 0.5'f32]) |
58 const | 58 const |
59 cube_pos = @[ | 59 cube_pos = @[ |
60 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front | 60 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front |
61 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back | 61 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back |
62 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left | 62 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left |
95 var myengine = igniteEngine("Hello cube") | 95 var myengine = igniteEngine("Hello cube") |
96 | 96 |
97 # build a mesh | 97 # build a mesh |
98 var trianglemesh = new IndexedMesh[VertexDataA, uint16] | 98 var trianglemesh = new IndexedMesh[VertexDataA, uint16] |
99 trianglemesh.vertexData = VertexDataA( | 99 trianglemesh.vertexData = VertexDataA( |
100 position: PositionAttribute[TVec3[float32]](data: cube_pos), | 100 position: PositionAttribute[Vec3](data: cube_pos), |
101 color: ColorAttribute[TVec3[float32]](data: cube_color), | 101 color: ColorAttribute[Vec3](data: cube_color), |
102 ) | 102 ) |
103 trianglemesh.indices = tris | 103 trianglemesh.indices = tris |
104 # build a single-object scene graph | 104 # build a single-object scene graph |
105 var triangle = new Thing | 105 var triangle = new Thing |
106 # add the triangle mesh to the object | 106 # add the triangle mesh to the object |