annotate examples/E03_hello_cube.nim @ 785:1880ab140165

add: first complete working version of multiple materials and shaders per scene, yipie :)
author Sam <sam@basx.dev>
date Sat, 19 Aug 2023 23:30:25 +0700
parents 754835bf175e
children 812b5e28f441
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
732
dcc12ab20a91 did: fix API changes broke examples
Sam <sam@basx.dev>
parents: 714
diff changeset
13 import ../src/semicongine
522
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 ]
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
33 R = newVec4f(1, 0, 0, 1)
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
34 G = newVec4f(0, 1, 0, 1)
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
35 B = newVec4f(0, 0, 1, 1)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
36 cube_color = @[
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
37 R, R, R, R,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
38 R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
39 G, G, G, G,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
40 G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
41 B, B, B, B,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
42 B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, B * 0.5'f32,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
43 ]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
44 var
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
45 tris: seq[array[3, uint16]]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
46 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
47 let off = i * 4
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
48 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
49 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
50
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
51 when isMainModule:
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
52 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
53
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
54 const
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
55 inputs = @[
624
3f13de7d8ec4 fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 606
diff changeset
56 attr[Vec3f]("position"),
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
57 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
58 ]
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
59 intermediate = @[attr[Vec4f]("outcolor")]
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
60 uniforms = @[
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
61 attr[Mat4]("projection"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
62 attr[Mat4]("view"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
63 attr[Mat4]("model"),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
64 ]
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
65 fragOutput = @[attr[Vec4f]("color")]
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
66 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
67 inputs=inputs,
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
68 intermediate=intermediate,
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
69 outputs=fragOutput,
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
70 uniforms=uniforms,
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
71 vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
72 fragmentCode="color = outcolor;",
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
73 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
74 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
763
a4c757f5d17f did: change API for ECS
Sam <sam@basx.dev>
parents: 739
diff changeset
75 var cube = newScene("scene", newEntity("cube", {"mesh": Component(newMesh(positions=cube_pos, indices=tris, colors=cube_color))}))
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
76 cube.addShaderGlobal("projection", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
77 cube.addShaderGlobal("view", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
78 cube.addShaderGlobal("model", Unit4f32)
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
79 myengine.addScene(cube, inputs, @[], transformAttribute="")
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
80
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
81 var t: float32 = cpuTime()
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 602
diff changeset
82 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
83 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
84 setShaderGlobal(cube, "projection",
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
85 perspective(
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
86 float32(PI / 4),
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
87 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
88 0.1'f32,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
89 100'f32
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
90 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
91 )
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
92 t = cpuTime()
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 myengine.renderScene(cube)
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
95
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
96 myengine.destroy()