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