annotate examples/hello_cube.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
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
1 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
2 # TODO: Needs Depth-Buffer first!
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
3 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
4 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
5 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
6 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
7 #
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
8 #
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
9
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
10
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
11 import std/times
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
12 import std/strutils
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
13
517
836790efab48 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 501
diff changeset
14 import semicongine
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
15
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
16 type
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
17 # define type of vertex
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
18 VertexDataA = object
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
19 position: PositionAttribute[Vec3]
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
20 color: ColorAttribute[Vec3]
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
21 Uniforms = object
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
22 model: Descriptor[Mat44]
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
23 view: Descriptor[Mat44]
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
24 projection: Descriptor[Mat44]
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
25
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
26 var
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
27 pipeline: RenderPipeline[VertexDataA, Uniforms]
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
28 uniforms: Uniforms
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
29 t: float32
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
30
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
31
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
32 proc globalUpdate(engine: var Engine, dt: float32) =
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
33 let ratio = float32(engine.vulkan.frameDimension.height) / float32(
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
34 engine.vulkan.frameDimension.width)
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
35 t += dt
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t,
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
37 Yf32) # * rotate3d(float32(PI), Yf32)
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
38
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
39 uniforms.view.value = Unit44f32
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
40 uniforms.projection.value = Mat44(data: [
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
41 ratio, 0'f32, 0'f32, 0'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
42 0'f32, 1'f32, 0'f32, 0'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
43 0'f32, 0'f32, 1'f32, 0'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
44 0'f32, 0'f32, 0'f32, 1'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
45 ])
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
46 uniforms.projection.value = perspective(float32(PI / 4), float32(
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
47 engine.vulkan.frameDimension.width) / float32(
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
48 engine.vulkan.frameDimension.height), 0.1'f32, 100'f32)
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
49 pipeline.updateUniformValues(uniforms)
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
50
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
51 const
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
52 TopLeftFront = Vec3([-0.5'f32, -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
53 TopRightFront = Vec3([0.5'f32, -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
54 BottomRightFront = Vec3([0.5'f32, 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
55 BottomLeftFront = Vec3([-0.5'f32, 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
56 TopLeftBack = Vec3([0.5'f32, -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
57 TopRightBack = Vec3([-0.5'f32, -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
58 BottomRightBack = Vec3([-0.5'f32, 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
59 BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32])
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
60 const
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
61 cube_pos = @[
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
62 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
63 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
64 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
65 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
66 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
67 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
68 ]
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
69 cube_color = @[
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
70 Rf32, Rf32, Rf32, Rf32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
71 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
72 Gf32, Gf32, Gf32, Gf32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
73 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
74 Bf32, Bf32, Bf32, Bf32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
75 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
76 ]
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
77 var
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
78 tris: seq[array[3, uint16]]
500
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
79 for i in 0'u16 ..< 6'u16:
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
80 let off = i * 4
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
81 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
8025ab67d931 fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 499
diff changeset
82 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
83
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
84 when isMainModule:
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
85 var myengine = igniteEngine("Hello cube")
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
86
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
87 # build a mesh
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
88 var trianglemesh = new IndexedMesh[VertexDataA, uint16]
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
89 trianglemesh.vertexData = VertexDataA(
520
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
90 position: PositionAttribute[Vec3](data: cube_pos),
cd73e429fc99 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 519
diff changeset
91 color: ColorAttribute[Vec3](data: cube_color),
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
92 )
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
93 trianglemesh.indices = tris
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
94 var cube = newThing("cube", trianglemesh)
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
95
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
96 # upload data, prepare shaders, etc
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
97 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
98 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1);
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
99 """)
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
100 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
101 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
102 myengine,
521
a25325bec7f2 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 520
diff changeset
103 cube,
499
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
104 vertexShader,
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
105 fragmentShader
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
106 )
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
107 # show something
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
108 myengine.run(pipeline, globalUpdate)
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
109 pipeline.trash()
3f1111f3b9f8 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
110 myengine.trash()