annotate examples/E01_hello_triangle.nim @ 113:7b695fb335ed

did: first final implementation of scene-graph <-> pipeline connection, not working yet
author Sam <sam@basx.dev>
date Sun, 02 Apr 2023 01:22:09 +0700
parents 779607656b12
children 9f2c178beb60
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
1 import std/times
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
2 import std/strutils
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
3 import std/enumerate
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
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
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
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]
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
12
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
13 var pipeline: RenderPipeline[VertexDataA, void]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
14
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents: 68
diff changeset
15 proc globalUpdate(engine: var Engine, t, dt: float32) =
33
94c38e4b5782 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 32
diff changeset
16 discard
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
17
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
18 # vertex data (types must match the above VertexAttributes)
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
19 const
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
20 triangle_pos = @[
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
21 Vec2([0.0'f32, -0.5'f32]),
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
22 Vec2([0.5'f32, 0.5'f32]),
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
23 Vec2([-0.5'f32, 0.5'f32]),
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
24 ]
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
25 triangle_color = @[
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
26 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
27 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
28 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
29 ]
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31 when isMainModule:
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
32 var myengine = igniteEngine("Hello triangle")
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
33
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
34 # build a mesh
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
35 var trianglemesh = new Mesh[VertexDataA, uint16]
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
36 trianglemesh.vertexData = VertexDataA(
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 57
diff changeset
37 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
38 color: ColorAttribute[Vec3](data: triangle_color),
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
39 )
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
40 # build a single-object scene graph
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
41 var triangle = newThing("triangle", trianglemesh)
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
42
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
43 # upload data, prepare shaders, etc
35
7f99b21a8777 add: support for instance data
Sam <sam@basx.dev>
parents: 33
diff changeset
44 const vertexShader = generateVertexShaderCode[VertexDataA, void]()
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
45 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
68
bdef52f3bc0d fix: API changes
Sam <sam@basx.dev>
parents: 61
diff changeset
46 pipeline = setupPipeline[VertexDataA, void, uint16](
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
47 myengine,
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
48 triangle,
32
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
49 vertexShader,
9edca5dc4e93 add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 28
diff changeset
50 fragmentShader
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 12
diff changeset
51 )
21
316923e9247c add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 19
diff changeset
52 # show something
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
53 myengine.run(pipeline, globalUpdate)
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 24
diff changeset
54 pipeline.trash()
0
5daf3f236d87 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 myengine.trash()