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