Mercurial > games > semicongine
annotate examples/hello_triangle.nim @ 32:9edca5dc4e93
add: working implementation of uniforms
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 14 Jan 2023 23:34:50 +0700 |
parents | b1b05d4efb52 |
children | 94c38e4b5782 |
rev | line source |
---|---|
28 | 1 import std/times |
32 | 2 import std/strutils |
3 import std/enumerate | |
28 | 4 |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
5 import zamikongine/engine |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
6 import zamikongine/math/vector |
32 | 7 import zamikongine/math/matrix |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
8 import zamikongine/vertex |
32 | 9 import zamikongine/descriptor |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
10 import zamikongine/mesh |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
11 import zamikongine/thing |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
12 import zamikongine/shader |
28 | 13 import zamikongine/buffer |
0 | 14 |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
15 type |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
16 # define type of vertex |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
17 VertexDataA = object |
32 | 18 position: PositionAttribute[Vec2[float32]] |
19 color: ColorAttribute[Vec3[float32]] | |
20 Uniforms = object | |
21 mat: Descriptor[Mat44[float32]] | |
22 dt: Descriptor[float32] | |
23 | |
24 var pipeline: RenderPipeline[VertexDataA, Uniforms] | |
28 | 25 |
32 | 26 var pos = 0'f32; |
27 var uniforms = Uniforms( | |
28 mat: Descriptor[Mat44[float32]](value: Unit44f32), | |
29 dt: Descriptor[float32](value: 0'f32), | |
30 ) | |
31 var scaledir = 1'f32 | |
32 proc globalUpdate(engine: var Engine, dt: float32) = | |
33 uniforms.mat.value = uniforms.mat.value * scale3d(1 + scaledir * dt, 1 + scaledir * dt, 0'f32) | |
34 if uniforms.mat.value[0, 0] > 2'f32 or uniforms.mat.value[0, 0] < 0.5'f32: | |
35 scaledir = - scaledir | |
36 for buffer in pipeline.uniformBuffers: | |
37 buffer.updateData(uniforms) | |
38 echo uniforms.mat.value | |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
39 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
40 # vertex data (types must match the above VertexAttributes) |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
41 const |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
42 triangle_pos = @[ |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
21
diff
changeset
|
43 Vec2([ 0.0'f32, -0.5'f32]), |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
44 Vec2([ 0.5'f32, 0.5'f32]), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
45 Vec2([-0.5'f32, 0.5'f32]), |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
46 ] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
47 triangle_color = @[ |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
21
diff
changeset
|
48 Vec3([1.0'f32, 0.0'f32, 0.0'f32]), |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
49 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), |
24
71bbe11d8de8
did: change shader compilation to run during program compilation, maybe add dynamic version later
Sam <sam@basx.dev>
parents:
21
diff
changeset
|
50 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
|
51 ] |
0 | 52 |
53 when isMainModule: | |
28 | 54 var myengine = igniteEngine("Hello triangle") |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
55 |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
56 # build a mesh |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
57 var trianglemesh = new Mesh[VertexDataA] |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
58 trianglemesh.vertexData = VertexDataA( |
32 | 59 position: PositionAttribute[Vec2[float32]](data: triangle_pos), |
60 color: ColorAttribute[Vec3[float32]](data: triangle_color), | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
61 ) |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
62 # build a single-object scene graph |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
63 var triangle = new Thing |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
64 # add the triangle mesh to the object |
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
65 triangle.parts.add trianglemesh |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
66 |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
67 # upload data, prepare shaders, etc |
32 | 68 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]( |
69 # have 1 at: | |
70 # [2][0] [0][3] | |
71 # "out_position = vec4(in_position[0] + uniforms.mat[0][0], in_position[1] + uniforms.mat[0][0], 0, 1);" | |
72 "out_position = uniforms.mat * vec4(in_position, 0, 1);" | |
73 # "out_position = vec4(in_position, 0, 1);" | |
74 ) | |
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
76 static: | |
77 echo "--------------" | |
78 for (i, line) in enumerate(vertexShader.splitLines()): | |
79 echo $(i + 1) & " " & line | |
80 echo "--------------" | |
81 echo fragmentShader | |
82 echo "--------------" | |
83 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
84 myengine, |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
85 triangle, |
32 | 86 vertexShader, |
87 fragmentShader | |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
12
diff
changeset
|
88 ) |
21
316923e9247c
add: clean examples, update build configs
Sam <sam@basx.dev>
parents:
19
diff
changeset
|
89 # show something |
28 | 90 myengine.run(pipeline, globalUpdate) |
91 pipeline.trash() | |
0 | 92 myengine.trash() |