Mercurial > games > semicongine
comparison 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 |
comparison
equal
deleted
inserted
replaced
27:8cb2d96ac28e | 28:b1b05d4efb52 |
---|---|
1 import std/times | |
2 | |
1 import zamikongine/engine | 3 import zamikongine/engine |
2 import zamikongine/math/vector | 4 import zamikongine/math/vector |
3 import zamikongine/vertex | 5 import zamikongine/vertex |
4 import zamikongine/mesh | 6 import zamikongine/mesh |
5 import zamikongine/thing | 7 import zamikongine/thing |
6 import zamikongine/shader | 8 import zamikongine/shader |
9 import zamikongine/buffer | |
7 | 10 |
8 type | 11 type |
9 # define type of vertex | 12 # define type of vertex |
10 VertexDataA = object | 13 VertexDataA = object |
11 position: VertexAttribute[Vec2[float32]] | 14 position: VertexAttribute[Vec2[float32]] |
12 color: VertexAttribute[Vec3[float32]] | 15 color: VertexAttribute[Vec3[float32]] |
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 | |
13 | 24 |
14 # vertex data (types must match the above VertexAttributes) | 25 # vertex data (types must match the above VertexAttributes) |
15 const | 26 const |
16 triangle_pos = @[ | 27 triangle_pos = @[ |
17 Vec2([ 0.0'f32, -0.5'f32]), | 28 Vec2([ 0.0'f32, -0.5'f32]), |
23 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), | 34 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), |
24 Vec3([0.0'f32, 0.0'f32, 1.0'f32]), | 35 Vec3([0.0'f32, 0.0'f32, 1.0'f32]), |
25 ] | 36 ] |
26 | 37 |
27 when isMainModule: | 38 when isMainModule: |
28 var myengine = igniteEngine() | 39 var myengine = igniteEngine("Hello triangle") |
29 | 40 |
30 # build a mesh | 41 # build a mesh |
31 var trianglemesh = new Mesh[VertexDataA] | 42 var trianglemesh = new Mesh[VertexDataA] |
32 trianglemesh.vertexData = VertexDataA( | 43 trianglemesh.vertexData = VertexDataA( |
33 position: VertexAttribute[Vec2[float32]](data: triangle_pos), | 44 position: VertexAttribute[Vec2[float32]](data: triangle_pos), |
37 var triangle = new Thing | 48 var triangle = new Thing |
38 # add the triangle mesh to the object | 49 # add the triangle mesh to the object |
39 triangle.parts.add trianglemesh | 50 triangle.parts.add trianglemesh |
40 | 51 |
41 # upload data, prepare shaders, etc | 52 # upload data, prepare shaders, etc |
42 setupPipeline[VertexDataA, uint16]( | 53 var pipeline = setupPipeline[VertexDataA, UniformType, float32, uint16]( |
43 myengine, | 54 myengine, |
44 triangle, | 55 triangle, |
45 generateVertexShaderCode[VertexDataA]("main", "position", "color"), | 56 generateVertexShaderCode[VertexDataA]("main", "position", "color"), |
46 generateFragmentShaderCode[VertexDataA]("main"), | 57 generateFragmentShaderCode[VertexDataA]("main"), |
47 ) | 58 ) |
48 # show something | 59 # show something |
49 myengine.fullThrottle() | 60 myengine.run(pipeline, globalUpdate) |
61 pipeline.trash() | |
50 myengine.trash() | 62 myengine.trash() |