Mercurial > games > semicongine
annotate examples/hello_triangle.nim @ 517:836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 00:41:55 +0700 |
parents | e89fceb5a3a2 |
children | 5d406c17bbcb |
rev | line source |
---|---|
489 | 1 import std/times |
493 | 2 import std/strutils |
3 import std/enumerate | |
489 | 4 |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
5 import semicongine |
473 | 6 |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
7 type |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
8 # define type of vertex |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
9 VertexDataA = object |
493 | 10 position: PositionAttribute[Vec2[float32]] |
11 color: ColorAttribute[Vec3[float32]] | |
496 | 12 id: InstanceAttribute[Vec3[float32]] |
493 | 13 |
494
0c18638c7217
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
493
diff
changeset
|
14 var pipeline: RenderPipeline[VertexDataA, void] |
489 | 15 |
493 | 16 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
|
17 discard |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
18 |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
19 # vertex data (types must match the above VertexAttributes) |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
20 const |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
21 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
|
22 Vec2([ 0.0'f32, -0.5'f32]), |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
23 Vec2([ 0.5'f32, 0.5'f32]), |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
24 Vec2([-0.5'f32, 0.5'f32]), |
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 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
|
27 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
|
28 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
|
29 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
|
30 ] |
473 | 31 |
32 when isMainModule: | |
489 | 33 var myengine = igniteEngine("Hello triangle") |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
34 |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
35 # build a mesh |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
36 var trianglemesh = new Mesh[VertexDataA] |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
37 trianglemesh.vertexData = VertexDataA( |
493 | 38 position: PositionAttribute[Vec2[float32]](data: triangle_pos), |
39 color: ColorAttribute[Vec3[float32]](data: triangle_color), | |
496 | 40 id: InstanceAttribute[Vec3[float32]](data: @[Vec3[float32]([0.5'f32, 0.5'f32, 0.5'f32])]), |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
41 ) |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
42 # build a single-object scene graph |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
43 var triangle = new Thing |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
44 # add the triangle mesh to the object |
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
45 triangle.parts.add trianglemesh |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
46 |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
47 # upload data, prepare shaders, etc |
496 | 48 const vertexShader = generateVertexShaderCode[VertexDataA, void]() |
493 | 49 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
499
3f1111f3b9f8
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
496
diff
changeset
|
50 pipeline = setupPipeline[VertexDataA, void, void]( |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
51 myengine, |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
52 triangle, |
493 | 53 vertexShader, |
54 fragmentShader | |
480
14e5151f68d1
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
473
diff
changeset
|
55 ) |
482
1670f8e70964
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
480
diff
changeset
|
56 # show something |
489 | 57 myengine.run(pipeline, globalUpdate) |
58 pipeline.trash() | |
473 | 59 myengine.trash() |