annotate examples/E03_hello_cube.nim @ 670:881a5c9ea50f

next version, because we got most low-level stuff, at least in a crappy version
author Sam <sam@basx.dev>
date Tue, 09 May 2023 01:37:46 +0700
parents e85f54b88afb
children d84b2e88776a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
1 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
2 # TODO: Needs Depth-Buffer first!
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
3 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
4 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
5 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
6 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
7 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
8 #
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
9
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
10
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
11 import std/times
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
12
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
13 import semicongine
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
14
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
15 const
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
16 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
17 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
18 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
19 BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
20 TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
21 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
22 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
23 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
24 const
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
25 cube_pos = @[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
26 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
27 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
28 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
29 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
30 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
31 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
32 ]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
33 cube_color = @[
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
34 Rf32, Rf32, Rf32, Rf32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
35 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
36 Gf32, Gf32, Gf32, Gf32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
37 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
38 Bf32, Bf32, Bf32, Bf32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
39 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
40 ]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
41 var
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
42 tris: seq[array[3, uint16]]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
43 for i in 0'u16 ..< 6'u16:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
44 let off = i * 4
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
45 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
46 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
47
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
48 when isMainModule:
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
49 var myengine = initEngine("Hello cube")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
50
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
51 const
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
52 vertexInput = @[
624
3f13de7d8ec4 fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 606
diff changeset
53 attr[Vec3f]("position"),
3f13de7d8ec4 fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 606
diff changeset
54 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
55 ]
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
56 vertexOutput = @[attr[Vec3f]("outcolor")]
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
57 uniforms = @[
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
58 attr[Mat4]("projection"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
59 attr[Mat4]("view"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
60 attr[Mat4]("model"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
61 ]
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
62 fragOutput = @[attr[Vec4f]("color")]
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
63 vertexCode = compileGlslShader(
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
64 stage=VK_SHADER_STAGE_VERTEX_BIT,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
65 inputs=vertexInput,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
66 uniforms=uniforms,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
67 outputs=vertexOutput,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
68 main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);"""
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
69 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
70 fragmentCode = compileGlslShader(
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
71 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
72 inputs=vertexOutput,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
73 uniforms=uniforms,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
74 outputs=fragOutput,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
75 main="color = vec4(outcolor, 1);"
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
76 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
77 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
78 var cube = newScene("scene", newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color)))
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
79 cube.addShaderGlobal("projection", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
80 cube.addShaderGlobal("view", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
81 cube.addShaderGlobal("model", Unit4f32)
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
82 myengine.addScene(cube, vertexInput)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
83
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
84 var t: float32 = cpuTime()
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 602
diff changeset
85 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
86 setShaderGlobal(cube, "model", translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32))
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
87 setShaderGlobal(cube, "projection",
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
88 perspective(
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
89 float32(PI / 4),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
90 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
91 0.1'f32,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
92 100'f32
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
93 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
94 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
95 t = cpuTime()
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
96
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
97 myengine.renderScene(cube)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
98
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
99 myengine.destroy()