annotate examples/E03_hello_cube.nim @ 1139:114f395b9144

did: finish refactoring and updated all tests accordingly
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 14:58:25 +0700
parents 71315636ba82
children 5934c5615f13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
1 import std/tables
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/times
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
3
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
4 import ../semicongine
522
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 const
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
7 TopLeftFront = NewVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
8 TopRightFront = NewVec3f(0.5'f32, -0.5'f32, -0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
9 BottomRightFront = NewVec3f(0.5'f32, 0.5'f32, -0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
10 BottomLeftFront = NewVec3f(-0.5'f32, 0.5'f32, -0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
11 TopLeftBack = NewVec3f(0.5'f32, -0.5'f32, 0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
12 TopRightBack = NewVec3f(-0.5'f32, -0.5'f32, 0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
13 BottomRightBack = NewVec3f(-0.5'f32, 0.5'f32, 0.5'f32)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
14 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
15 const
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
16 cube_pos = @[
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
17 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
18 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
19 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
20 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
21 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
22 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
23 ]
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
24 R = NewVec4f(1, 0, 0, 1)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
25 G = NewVec4f(0, 1, 0, 1)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
26 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
27 cube_color = @[
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
28 R, R, R, R,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
29 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
30 G, G, G, G,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
31 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
32 B, B, B, B,
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
33 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
34 ]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
35 var
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
36 tris: seq[array[3, uint16]]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
37 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
38 let off = i * 4
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
39 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
40 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
41
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
42 when isMainModule:
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
43 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
44
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
45 const
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
46 shaderConfiguration = createShaderConfiguration(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
47 name = "default shader",
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
48 inputs = [
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
49 attr[Vec3f]("position"),
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
50 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
51 ],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
52 intermediates = [attr[Vec4f]("outcolor")],
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
53 uniforms = [
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
54 attr[Mat4]("projection"),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
55 attr[Mat4]("view"),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
56 attr[Mat4]("model"),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
57 ],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
58 outputs = [attr[Vec4f]("color")],
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
59 vertexCode = """outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
60 fragmentCode = "color = outcolor;",
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
61 )
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
62 var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable)
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
63 var cube = Scene(name: "scene", meshes: @[newMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))])
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
64 cube.addShaderGlobal("projection", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
65 cube.addShaderGlobal("view", Unit4f32)
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
66 cube.addShaderGlobal("model", Unit4f32)
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
67 myengine.initRenderer({matDef: shaderConfiguration})
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
68 myengine.loadScene(cube)
602
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 var t: float32 = cpuTime()
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
71 while myengine.UpdateInputs() and not KeyWasPressed(Escape):
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
72 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32))
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 624
diff changeset
73 setShaderGlobal(cube, "projection",
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
74 perspective(
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
75 float32(PI / 4),
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
76 float32(myengine.GetWindow().size[0]) / float32(myengine.GetWindow().size[1]),
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
77 0.1'f32,
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
78 100'f32
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
79 )
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 t = cpuTime()
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
82 myengine.renderScene(cube)
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 myengine.destroy()