annotate examples/hello_triangle.nim @ 28:b1b05d4efb52

big refactoring, part1
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 14:08:00 +0700
parents 71bbe11d8de8
children 9edca5dc4e93
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
1 import std/times
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
2
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
3 import zamikongine/engine
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
4 import zamikongine/math/vector
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
5 import zamikongine/vertex
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
6 import zamikongine/mesh
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
7 import zamikongine/thing
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
8 import zamikongine/shader
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
9 import zamikongine/buffer
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
11 type
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
12 # define type of vertex
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
13 VertexDataA = object
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
14 position: VertexAttribute[Vec2[float32]]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
15 color: VertexAttribute[Vec3[float32]]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
16 UniformType = float32
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
17
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
18 proc globalUpdate(engine: var Engine, dt: Duration) =
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
19 # var t = float32(dt.inNanoseconds) / 1_000_000_000'f32
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
20 # for buffer in engine.vulkan.uniformBuffers:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
21 # buffer.updateData(t)
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
22
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
23 echo dt
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
24
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
25 # vertex data (types must match the above VertexAttributes)
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
26 const
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
27 triangle_pos = @[
24
71bbe11d8de8 did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents: 21
diff changeset
28 Vec2([ 0.0'f32, -0.5'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
29 Vec2([ 0.5'f32, 0.5'f32]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
30 Vec2([-0.5'f32, 0.5'f32]),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
31 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
32 triangle_color = @[
24
71bbe11d8de8 did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents: 21
diff changeset
33 Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
34 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
24
71bbe11d8de8 did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents: 21
diff changeset
35 Vec3([0.0'f32, 0.0'f32, 1.0'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
36 ]
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38 when isMainModule:
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
39 var myengine = igniteEngine("Hello triangle")
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
40
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
41 # build a mesh
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
42 var trianglemesh = new Mesh[VertexDataA]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
43 trianglemesh.vertexData = VertexDataA(
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
44 position: VertexAttribute[Vec2[float32]](data: triangle_pos),
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
45 color: VertexAttribute[Vec3[float32]](data: triangle_color),
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
46 )
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
47 # build a single-object scene graph
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
48 var triangle = new Thing
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
49 # add the triangle mesh to the object
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
50 triangle.parts.add trianglemesh
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
51
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
52 # upload data, prepare shaders, etc
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
53 var pipeline = setupPipeline[VertexDataA, UniformType, float32, uint16](
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
54 myengine,
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
55 triangle,
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
56 generateVertexShaderCode[VertexDataA]("main", "position", "color"),
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
57 generateFragmentShaderCode[VertexDataA]("main"),
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
58 )
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
59 # show something
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
60 myengine.run(pipeline, globalUpdate)
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
61 pipeline.trash()
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 myengine.trash()