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