annotate examples/hello_triangle.nim @ 482:1670f8e70964

add: clean examples, update build configs
author Sam <sam@basx.dev>
date Tue, 10 Jan 2023 00:24:37 +0700
parents 14e5151f68d1
children b4a972bd37d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
1 import zamikongine/engine
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
2 import zamikongine/math/vector
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
3 import zamikongine/vertex
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
4 import zamikongine/mesh
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
5 import zamikongine/thing
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
6 import zamikongine/shader
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
7
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
8 type
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
9 # define type of vertex
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
10 VertexDataA = object
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
11 position: VertexAttribute[Vec2[float32]]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
12 color: VertexAttribute[Vec3[float32]]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
13
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
14 # vertex data (types must match the above VertexAttributes)
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
15 const
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
16 triangle_pos = @[
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
17 Vec2([-0.5'f32, -0.5'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
18 Vec2([ 0.5'f32, 0.5'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
19 Vec2([-0.5'f32, 0.5'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
20 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
21 triangle_color = @[
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
22 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
23 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
24 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
25 ]
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
26
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
27 when isMainModule:
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
28 var myengine = igniteEngine()
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
29
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
30 # build a mesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
31 var trianglemesh = new Mesh[VertexDataA]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
32 trianglemesh.vertexData = VertexDataA(
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
33 position: VertexAttribute[Vec2[float32]](data: triangle_pos),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
34 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
35 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
36 # build a single-object scene graph
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
37 var triangle = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
38 # add the triangle mesh to the object
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
39 triangle.parts.add trianglemesh
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
40
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
41 # upload data, prepare shaders, etc
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
42 setupPipeline[VertexDataA, uint16](
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
43 myengine,
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
44 triangle,
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
45 generateVertexShaderCode[VertexDataA]("main", "position", "color"),
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
46 generateFragmentShaderCode[VertexDataA]("main"),
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
47 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
48 # show something
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
49 myengine.fullThrottle()
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
50 myengine.trash()