Mercurial > games > semicongine
annotate examples/hello_triangle.nim @ 33:94c38e4b5782
did: refactoring, move more from make to nimscript
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 15 Jan 2023 23:23:54 +0700 |
parents | 9edca5dc4e93 |
children | 7f99b21a8777 |
rev | line source |
---|---|
28 | 1 import std/times |
32 | 2 import std/strutils |
3 import std/enumerate | |
28 | 4 |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
5 import zamikongine/engine |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
6 import zamikongine/math/vector |
32 | 7 import zamikongine/math/matrix |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
8 import zamikongine/vertex |
32 | 9 import zamikongine/descriptor |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
10 import zamikongine/mesh |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
11 import zamikongine/thing |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
12 import zamikongine/shader |
28 | 13 import zamikongine/buffer |
0 | 14 |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
15 type |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
16 # define type of vertex |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
17 VertexDataA = object |
32 | 18 position: PositionAttribute[Vec2[float32]] |
19 color: ColorAttribute[Vec3[float32]] | |
20 | |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
21 var pipeline: RenderPipeline[VertexDataA, void] |
28 | 22 |
32 | 23 proc globalUpdate(engine: var Engine, dt: float32) = |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
24 discard |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
25 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
26 # vertex data (types must match the above VertexAttributes) |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
27 const |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
28 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
|
29 Vec2([ 0.0'f32, -0.5'f32]), |
21
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 Vec2([-0.5'f32, 0.5'f32]), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
32 ] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 ] |
0 | 38 |
39 when isMainModule: | |
28 | 40 var myengine = igniteEngine("Hello triangle") |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
41 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
42 # build a mesh |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
43 var trianglemesh = new Mesh[VertexDataA] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
44 trianglemesh.vertexData = VertexDataA( |
32 | 45 position: PositionAttribute[Vec2[float32]](data: triangle_pos), |
46 color: ColorAttribute[Vec3[float32]](data: triangle_color), | |
19
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 # build a single-object scene graph |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
49 var triangle = new Thing |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
50 # add the triangle mesh to the object |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
51 triangle.parts.add trianglemesh |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
52 |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
53 # upload data, prepare shaders, etc |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
54 const vertexShader = generateVertexShaderCode[VertexDataA, void]( |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
55 # "out_position = uniforms.mat * vec4(in_position, 0, 1);" |
32 | 56 ) |
57 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
58 pipeline = setupPipeline[VertexDataA, void, uint16]( |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
59 myengine, |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
60 triangle, |
32 | 61 vertexShader, |
62 fragmentShader | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
63 ) |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
64 # show something |
28 | 65 myengine.run(pipeline, globalUpdate) |
66 pipeline.trash() | |
0 | 67 myengine.trash() |