annotate examples/hello_triangle.nim @ 521:a25325bec7f2

did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
author Sam <sam@basx.dev>
date Sun, 22 Jan 2023 22:46:53 +0700
parents cd73e429fc99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
1 import std/times
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
2 import std/strutils
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
3 import std/enumerate
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
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
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
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
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
10 position: PositionAttribute[Vec2]
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
11 color: ColorAttribute[Vec3]
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
12
494
0c18638c7217 did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents: 493
diff changeset
13 var pipeline: RenderPipeline[VertexDataA, void]
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
14
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
15 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
16 discard
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
17
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
18 # vertex data (types must match the above VertexAttributes)
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
19 const
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
20 triangle_pos = @[
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
21 Vec2([0.0'f32, -0.5'f32]),
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
22 Vec2([0.5'f32, 0.5'f32]),
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
23 Vec2([-0.5'f32, 0.5'f32]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
24 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
25 triangle_color = @[
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
26 Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
27 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
28 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
29 ]
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
30
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
31 when isMainModule:
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
32 var myengine = igniteEngine("Hello triangle")
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
33
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
34 # build a mesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
35 var trianglemesh = new Mesh[VertexDataA]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
36 trianglemesh.vertexData = VertexDataA(
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
37 position: PositionAttribute[Vec2](data: triangle_pos),
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 518
diff changeset
38 color: ColorAttribute[Vec3](data: triangle_color),
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
39 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
40 # build a single-object scene graph
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
41 var triangle = newThing("triangle", trianglemesh)
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
42
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
43 # upload data, prepare shaders, etc
496
b41733878bc6 add: support for instance data
Sam <sam@basx.dev>
parents: 494
diff changeset
44 const vertexShader = generateVertexShaderCode[VertexDataA, void]()
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
45 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
46 pipeline = setupPipeline[VertexDataA, void, void](
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
47 myengine,
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
48 triangle,
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
49 vertexShader,
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
50 fragmentShader
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
51 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
52 # show something
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
53 myengine.run(pipeline, globalUpdate)
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
54 pipeline.trash()
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
55 myengine.trash()