Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 567:05ac2455ff60
add: render main loop structure
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 26 Mar 2023 01:56:19 +0700 |
parents | 21c276c0a968 |
children | 0d8858aa0671 |
rev | line source |
---|---|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 # TODO: Needs Depth-Buffer first! |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 import std/times |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 import std/strutils |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 type |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 # define type of vertex |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 VertexDataA = object |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 position: PositionAttribute[Vec3] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 color: ColorAttribute[Vec3] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 Uniforms = object |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 model: Descriptor[Mat44] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 view: Descriptor[Mat44] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 projection: Descriptor[Mat44] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 pipeline: RenderPipeline[VertexDataA, Uniforms] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 uniforms: Uniforms |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 |
535 | 31 proc globalUpdate(engine: var Engine, t, dt: float32) = |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 let ratio = float32(engine.vulkan.frameSize.y) / float32( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 engine.vulkan.frameSize.x) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 Yf32) # * rotate3d(float32(PI), Yf32) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 uniforms.view.value = Unit44f32 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 uniforms.projection.value = Mat44(data: [ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 ratio, 0'f32, 0'f32, 0'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 0'f32, 1'f32, 0'f32, 0'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 0'f32, 0'f32, 1'f32, 0'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 0'f32, 0'f32, 0'f32, 1'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 ]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 uniforms.projection.value = perspective(float32(PI / 4), float32( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 engine.vulkan.frameSize.x) / float32( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 engine.vulkan.frameSize.y), 0.1'f32, 100'f32) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 engine.vulkan.device.updateUniformData(pipeline, uniforms) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 TopLeftFront = Vec3([-0.5'f32, -0.5'f32, -0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 TopRightFront = Vec3([0.5'f32, -0.5'f32, -0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 BottomRightFront = Vec3([0.5'f32, 0.5'f32, -0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 BottomLeftFront = Vec3([-0.5'f32, 0.5'f32, -0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 TopLeftBack = Vec3([0.5'f32, -0.5'f32, 0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 TopRightBack = Vec3([-0.5'f32, -0.5'f32, 0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 BottomRightBack = Vec3([-0.5'f32, 0.5'f32, 0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32]) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 cube_pos = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 cube_color = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 Rf32, Rf32, Rf32, Rf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 Gf32, Gf32, Gf32, Gf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 Bf32, Bf32, Bf32, Bf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 tris: seq[array[3, uint16]] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 for i in 0'u16 ..< 6'u16: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 let off = i * 4 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 tris.add [off + 2'u16, off + 3'u16, off + 0'u16] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 when isMainModule: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 var myengine = igniteEngine("Hello cube") |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 # build a mesh |
529 | 86 var trianglemesh = new Mesh[VertexDataA, uint16] |
87 trianglemesh.indexed = true | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 trianglemesh.vertexData = VertexDataA( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 position: PositionAttribute[Vec3](data: cube_pos), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 color: ColorAttribute[Vec3](data: cube_color), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 trianglemesh.indices = tris |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 var cube = newThing("cube", trianglemesh) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 # upload data, prepare shaders, etc |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1); |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 """) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 myengine, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 cube, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 fragmentShader |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 # show something |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 myengine.run(pipeline, globalUpdate) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 pipeline.trash() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 myengine.trash() |