annotate examples/hello_triangle.nim @ 493:680c4b8ca28a

add: working implementation of uniforms
author Sam <sam@basx.dev>
date Sat, 14 Jan 2023 23:34:50 +0700
parents 54a1f8ee208e
children 0c18638c7217
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
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
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
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
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
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
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
13 import zamikongine/buffer
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
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
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
18 position: PositionAttribute[Vec2[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
19 color: ColorAttribute[Vec3[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
20 Uniforms = object
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
21 mat: Descriptor[Mat44[float32]]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
22 dt: Descriptor[float32]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
23
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
24 var pipeline: RenderPipeline[VertexDataA, Uniforms]
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
25
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
26 var pos = 0'f32;
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
27 var uniforms = Uniforms(
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
28 mat: Descriptor[Mat44[float32]](value: Unit44f32),
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
29 dt: Descriptor[float32](value: 0'f32),
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
30 )
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
31 var scaledir = 1'f32
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
32 proc globalUpdate(engine: var Engine, dt: float32) =
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
33 uniforms.mat.value = uniforms.mat.value * scale3d(1 + scaledir * dt, 1 + scaledir * dt, 0'f32)
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
34 if uniforms.mat.value[0, 0] > 2'f32 or uniforms.mat.value[0, 0] < 0.5'f32:
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
35 scaledir = - scaledir
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
36 for buffer in pipeline.uniformBuffers:
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
37 buffer.updateData(uniforms)
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
38 echo uniforms.mat.value
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
39
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
40 # vertex data (types must match the above VertexAttributes)
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
41 const
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
42 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
43 Vec2([ 0.0'f32, -0.5'f32]),
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
44 Vec2([ 0.5'f32, 0.5'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
45 Vec2([-0.5'f32, 0.5'f32]),
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
46 ]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
47 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
48 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
49 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
50 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
51 ]
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
52
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
53 when isMainModule:
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
54 var myengine = igniteEngine("Hello triangle")
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
55
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
56 # build a mesh
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
57 var trianglemesh = new Mesh[VertexDataA]
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
58 trianglemesh.vertexData = VertexDataA(
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
59 position: PositionAttribute[Vec2[float32]](data: triangle_pos),
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
60 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
61 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
62 # build a single-object scene graph
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
63 var triangle = new Thing
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
64 # add the triangle mesh to the object
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
65 triangle.parts.add trianglemesh
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
66
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
67 # upload data, prepare shaders, etc
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
68 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
69 # have 1 at:
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
70 # [2][0] [0][3]
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
71 # "out_position = vec4(in_position[0] + uniforms.mat[0][0], in_position[1] + uniforms.mat[0][0], 0, 1);"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
72 "out_position = uniforms.mat * vec4(in_position, 0, 1);"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
73 # "out_position = vec4(in_position, 0, 1);"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
74 )
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
76 static:
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
77 echo "--------------"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
78 for (i, line) in enumerate(vertexShader.splitLines()):
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
79 echo $(i + 1) & " " & line
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
80 echo "--------------"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
81 echo fragmentShader
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
82 echo "--------------"
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
83 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
84 myengine,
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
85 triangle,
493
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
86 vertexShader,
680c4b8ca28a add: working implementation of uniforms
Sam <sam@basx.dev>
parents: 489
diff changeset
87 fragmentShader
480
14e5151f68d1 did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents: 473
diff changeset
88 )
482
1670f8e70964 add: clean examples, update build configs
Sam <sam@basx.dev>
parents: 480
diff changeset
89 # show something
489
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
90 myengine.run(pipeline, globalUpdate)
54a1f8ee208e big refactoring, part1
Sam <sam@basx.dev>
parents: 485
diff changeset
91 pipeline.trash()
473
04b8471bdab4 did: cleanup up a bit
Sam <sam@basx.dev>
parents:
diff changeset
92 myengine.trash()