annotate examples/E03_hello_cube.nim @ 1140:5934c5615f13

did: update all examples to work with latest refactoring
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 15:16:17 +0700
parents 114f395b9144
children d3e014c3551c
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:
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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 = [
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
49 Attr[Vec3f]("position"),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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 ],
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
52 intermediates = [Attr[Vec4f]("outcolor")],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
53 uniforms = [
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
54 Attr[Mat4]("projection"),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
55 Attr[Mat4]("view"),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
56 Attr[Mat4]("model"),
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
57 ],
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
58 outputs = [Attr[Vec4f]("color")],
1027
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)
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
63 var cube = Scene(name: "scene", meshes: @[NewMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))])
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
64 cube.AddShaderGlobal("projection", Unit4f32)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
65 cube.AddShaderGlobal("view", Unit4f32)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
66 cube.AddShaderGlobal("model", Unit4f32)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
67 myengine.InitRenderer({matDef: shaderConfiguration})
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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):
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
72 SetShaderGlobal(cube, "model", Translate(0'f32, 0'f32, 10'f32) * Rotate(t, Yf32))
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
73 SetShaderGlobal(cube, "projection",
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
74 Perspective(
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
75 float32(PI / 4),
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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()
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
82 myengine.RenderScene(cube)
602
0d8858aa0671 did: migrate to new engine version
Sam <sam@basx.dev>
parents: 535
diff changeset
83
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
84 myengine.Destroy()