Mercurial > games > semicongine
annotate examples/hello_triangle.nim @ 26:8b4bc545958d
did: update gitignore
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 11:43:59 +0700 |
parents | 71bbe11d8de8 |
children | b1b05d4efb52 |
rev | line source |
---|---|
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
1 import zamikongine/engine |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
2 import zamikongine/math/vector |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
3 import zamikongine/vertex |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
4 import zamikongine/mesh |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
5 import zamikongine/thing |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
6 import zamikongine/shader |
0 | 7 |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
8 type |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
9 # define type of vertex |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
10 VertexDataA = object |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
11 position: VertexAttribute[Vec2[float32]] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
12 color: VertexAttribute[Vec3[float32]] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
13 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
14 # vertex data (types must match the above VertexAttributes) |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
15 const |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
16 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
|
17 Vec2([ 0.0'f32, -0.5'f32]), |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
18 Vec2([ 0.5'f32, 0.5'f32]), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
19 Vec2([-0.5'f32, 0.5'f32]), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
20 ] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 ] |
0 | 26 |
27 when isMainModule: | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
28 var myengine = igniteEngine() |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
29 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
30 # build a mesh |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
31 var trianglemesh = new Mesh[VertexDataA] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
32 trianglemesh.vertexData = VertexDataA( |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
33 position: VertexAttribute[Vec2[float32]](data: triangle_pos), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
34 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
|
35 ) |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
36 # build a single-object scene graph |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
37 var triangle = new Thing |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
38 # add the triangle mesh to the object |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
39 triangle.parts.add trianglemesh |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
40 |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
41 # upload data, prepare shaders, etc |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
42 setupPipeline[VertexDataA, uint16]( |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
43 myengine, |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
44 triangle, |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
45 generateVertexShaderCode[VertexDataA]("main", "position", "color"), |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
46 generateFragmentShaderCode[VertexDataA]("main"), |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
47 ) |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
48 # show something |
0 | 49 myengine.fullThrottle() |
50 myengine.trash() |