Mercurial > games > semicongine
comparison examples/hello_cube.nim @ 38:c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 18 Jan 2023 09:52:03 +0700 |
parents | |
children | 0cb294c5d2fd |
comparison
equal
deleted
inserted
replaced
37:6859bcfabc62 | 38:c3c963e7c1a6 |
---|---|
1 # | |
2 # TODO: Needs Depth-Buffer first! | |
3 # | |
4 # | |
5 # | |
6 # | |
7 # | |
8 # | |
9 # | |
10 # | |
11 import std/times | |
12 import std/strutils | |
13 import std/enumerate | |
14 | |
15 import zamikongine/engine | |
16 import zamikongine/math/vector | |
17 import zamikongine/math/matrix | |
18 import zamikongine/vertex | |
19 import zamikongine/descriptor | |
20 import zamikongine/mesh | |
21 import zamikongine/thing | |
22 import zamikongine/shader | |
23 import zamikongine/buffer | |
24 | |
25 type | |
26 # define type of vertex | |
27 VertexDataA = object | |
28 position: PositionAttribute[Vec3[float32]] | |
29 color: ColorAttribute[Vec3[float32]] | |
30 Uniforms = object | |
31 model: Descriptor[Mat44[float32]] | |
32 view: Descriptor[Mat44[float32]] | |
33 projection: Descriptor[Mat44[float32]] | |
34 | |
35 var | |
36 pipeline: RenderPipeline[VertexDataA, Uniforms] | |
37 uniforms:Uniforms | |
38 t: float32 | |
39 | |
40 | |
41 proc globalUpdate(engine: var Engine, dt: float32) = | |
42 let ratio = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width) | |
43 t += dt | |
44 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32) # * rotate3d(float32(PI), Yf32) | |
45 | |
46 uniforms.view.value = Unit44f32 | |
47 uniforms.projection.value = Mat44[float32](data:[ | |
48 ratio, 0'f32, 0'f32, 0'f32, | |
49 0'f32, 1'f32, 0'f32, 0'f32, | |
50 0'f32, 0'f32, 1'f32, 0'f32, | |
51 0'f32, 0'f32, 0'f32, 1'f32, | |
52 ]) | |
53 uniforms.projection.value = perspective(float32(PI / 4), float32(engine.vulkan.frameDimension.width) / float32(engine.vulkan.frameDimension.height), 0.1'f32, 100'f32) | |
54 for buffer in pipeline.uniformBuffers: | |
55 buffer.updateData(uniforms) | |
56 echo uniforms.projection.value | |
57 | |
58 const | |
59 TopLeftFront = Vec3([ -0.5'f32, -0.5'f32, -0.5'f32]) | |
60 TopRightFront = Vec3([ 0.5'f32, -0.5'f32, -0.5'f32]) | |
61 BottomRightFront = Vec3([ 0.5'f32, 0.5'f32, -0.5'f32]) | |
62 BottomLeftFront = Vec3([ -0.5'f32, 0.5'f32, -0.5'f32]) | |
63 TopLeftBack = Vec3([ 0.5'f32, -0.5'f32, 0.5'f32]) | |
64 TopRightBack = Vec3([ -0.5'f32, -0.5'f32, 0.5'f32]) | |
65 BottomRightBack = Vec3([ -0.5'f32, 0.5'f32, 0.5'f32]) | |
66 BottomLeftBack = Vec3([ 0.5'f32, 0.5'f32, 0.5'f32]) | |
67 const | |
68 cube_pos = @[ | |
69 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front | |
70 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back | |
71 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left | |
72 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right | |
73 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top | |
74 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom | |
75 ] | |
76 cube_color = @[ | |
77 Rf32, Rf32, Rf32, Rf32, | |
78 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, | |
79 Gf32, Gf32, Gf32, Gf32, | |
80 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, | |
81 Bf32, Bf32, Bf32, Bf32, | |
82 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, | |
83 ] | |
84 var | |
85 tris: seq[array[3, uint16]] | |
86 # for i in 0'u16 ..< 6'u16: | |
87 # let off = i * 4 | |
88 var off = 0'u16 * 4 | |
89 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
90 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16] | |
91 # off = 1'u16 * 4 | |
92 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
93 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16] | |
94 # off = 4'u16 * 4 | |
95 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
96 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16] | |
97 # off = 3'u16 * 4 | |
98 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
99 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16] | |
100 | |
101 when isMainModule: | |
102 var myengine = igniteEngine("Hello cube") | |
103 | |
104 # build a mesh | |
105 var trianglemesh = new IndexedMesh[VertexDataA, uint16] | |
106 trianglemesh.vertexData = VertexDataA( | |
107 position: PositionAttribute[Vec3[float32]](data: cube_pos), | |
108 color: ColorAttribute[Vec3[float32]](data: cube_color), | |
109 ) | |
110 trianglemesh.indices = tris | |
111 # build a single-object scene graph | |
112 var triangle = new Thing | |
113 # add the triangle mesh to the object | |
114 triangle.parts.add trianglemesh | |
115 | |
116 # upload data, prepare shaders, etc | |
117 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" | |
118 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1); | |
119 """) | |
120 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
121 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | |
122 myengine, | |
123 triangle, | |
124 vertexShader, | |
125 fragmentShader | |
126 ) | |
127 # show something | |
128 myengine.run(pipeline, globalUpdate) | |
129 pipeline.trash() | |
130 myengine.trash() |