annotate examples/E03_hello_cube.nim @ 145:a4e6e76128e6

add: upgrade all simple examples to new engine version
author Sam <sam@basx.dev>
date Wed, 26 Apr 2023 02:15:43 +0700
parents 8bb27869b649
children 25d97fa0ad5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
1 #
c3c963e7c1a6 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!
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
3 #
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
4 #
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
5 #
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
6 #
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
7 #
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
8 #
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
9
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
10
38
c3c963e7c1a6 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
c3c963e7c1a6 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
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
13
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
14 import semicongine
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
15
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
16 const
141
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
17 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
18 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
19 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
20 BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
21 TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
22 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
23 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
24 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32)
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
25 const
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
26 cube_pos = @[
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
27 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
28 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
29 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
30 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
31 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
32 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
33 ]
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
34 cube_color = @[
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
35 Rf32, Rf32, Rf32, Rf32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
36 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
37 Gf32, Gf32, Gf32, Gf32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
38 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
39 Bf32, Bf32, Bf32, Bf32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
40 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32,
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
41 ]
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
42 var
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
43 tris: seq[array[3, uint16]]
39
0cb294c5d2fd fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 38
diff changeset
44 for i in 0'u16 ..< 6'u16:
0cb294c5d2fd fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 38
diff changeset
45 let off = i * 4
0cb294c5d2fd fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 38
diff changeset
46 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
0cb294c5d2fd fix: hello cube, add: run_all command
Sam <sam@basx.dev>
parents: 38
diff changeset
47 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
48
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
49 when isMainModule:
141
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
50 var myengine = initEngine("Hello cube")
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
51
141
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
52 const
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
53 vertexInput = @[
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
54 attr[Vec3f]("position", memoryLocation=VRAM),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
55 attr[Vec3f]("color", memoryLocation=VRAMVisible),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
56 ]
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
57 vertexOutput = @[attr[Vec3f]("outcolor")]
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
58 uniforms = @[
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
59 attr[Mat4]("projection"),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
60 attr[Mat4]("view"),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
61 attr[Mat4]("model"),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
62 ]
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
63 fragOutput = @[attr[Vec4f]("color")]
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
64 vertexCode = compileGlslShader(
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
65 stage=VK_SHADER_STAGE_VERTEX_BIT,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
66 inputs=vertexInput,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
67 uniforms=uniforms,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
68 outputs=vertexOutput,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
69 main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);"""
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
70 )
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
71 fragmentCode = compileGlslShader(
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
72 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
73 inputs=vertexOutput,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
74 uniforms=uniforms,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
75 outputs=fragOutput,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
76 main="color = vec4(outcolor, 1);"
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
77 )
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
78 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
79 var
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
80 projection = initShaderGlobal("projection", Unit4f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
81 view = initShaderGlobal("view", Unit4f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
82 model = initShaderGlobal("model", Unit4f32)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
83 cube = newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color))
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
84 cube.components.add projection
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
85 cube.components.add view
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
86 cube.components.add model
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
87 myengine.addScene(cube, vertexInput)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
88
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
89 var t: float32 = cpuTime()
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 141
diff changeset
90 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
141
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
91 setValue[Mat4](model.value, translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32))
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
92 setValue[Mat4](
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
93 projection.value,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
94 perspective(
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
95 float32(PI / 4),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
96 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
97 0.1'f32,
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
98 100'f32
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
99 )
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
100 )
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
101 t = cpuTime()
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
102
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
103 myengine.renderScene(cube)
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
104
8bb27869b649 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 74
diff changeset
105 myengine.destroy()