annotate examples/E02_squares.nim @ 1214:04e446a7eb2b compiletime-tests

add: multipass renderer, finish tets for now
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 20:11:55 +0700
parents 5934c5615f13
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
1 import std/sequtils
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
2 import std/tables
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/random
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
4
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
5 import ../semicongine
522
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 when isMainModule:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
9 randomize()
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
10 const
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
11 COLUMNS = 10
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
12 ROWS = 10
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
13 WIDTH = 2'f32 / COLUMNS
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
14 HEIGHT = 2'f32 / ROWS
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
15 var
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
16 vertices: array[COLUMNS * ROWS * 4, Vec3f]
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
17 colors: array[COLUMNS * ROWS * 4, Vec4f]
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
18 iValues: array[COLUMNS * ROWS * 4, uint32]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
19 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
20
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
21 for row in 0 ..< ROWS:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
22 for col in 0 ..< COLUMNS:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
23 let
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
24 y: float32 = (row * 2 / COLUMNS) - 1
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
25 x: float32 = (col * 2 / ROWS) - 1
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
26 color = NewVec4f((x + 1) / 2, (y + 1) / 2, 0, 1)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
27 squareIndex = row * COLUMNS + col
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
28 vertIndex = squareIndex * 4
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
29 vertices[vertIndex + 0] = NewVec3f(x, y)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
30 vertices[vertIndex + 1] = NewVec3f(x + WIDTH, y)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
31 vertices[vertIndex + 2] = NewVec3f(x + WIDTH, y + HEIGHT)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1027
diff changeset
32 vertices[vertIndex + 3] = NewVec3f(x, y + HEIGHT)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
33 colors[vertIndex + 0] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
34 colors[vertIndex + 1] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
35 colors[vertIndex + 2] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
36 colors[vertIndex + 3] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
37 iValues[vertIndex + 0] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
38 iValues[vertIndex + 1] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
39 iValues[vertIndex + 2] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
40 iValues[vertIndex + 3] = uint32(squareIndex)
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
41 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)]
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
42 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
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
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
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),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
51 Attr[uint32]("index"),
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
52 ],
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
53 intermediates = [Attr[Vec4f]("outcolor")],
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
54 uniforms = [Attr[float32]("time")],
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
55 outputs = [Attr[Vec4f]("color")],
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
56 vertexCode = """
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
57 float pos_weight = index / 100.0; // add some gamma correction?
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
58 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
59 float v = min(1, max(0, pow(pos_weight - t, 2)));
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
60 v = pow(1 - v, 3000);
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 665
diff changeset
61 outcolor = vec4(color.r, color.g, v * 0.5, 1);
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
62 gl_Position = vec4(position, 1.0);
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
63 """,
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
64 fragmentCode = "color = outcolor;",
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
65 )
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
66 let matDef = MaterialType(name: "default", vertexAttributes: {
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
67 "position": Vec3F32,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
68 "color": Vec4F32,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
69 "index": UInt32,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
70 }.toTable)
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
71 var squaremesh = NewMesh(
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
72 positions = vertices,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
73 indices = indices,
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
74 colors = colors,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
75 )
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
76 squaremesh[].InitVertexAttribute("index", iValues.toSeq)
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
77 squaremesh.material = matDef.InitMaterialData(name = "default")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
78
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
79 var myengine = InitEngine("Squares")
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
80 myengine.InitRenderer({matDef: shaderConfiguration})
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
81
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
82 var scene = Scene(name: "scene", meshes: @[squaremesh])
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
83 scene.AddShaderGlobal("time", 0.0'f32)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
84 myengine.LoadScene(scene)
1027
d6c27f0ed3e4 fix: examples not compiling
sam <sam@basx.dev>
parents: 920
diff changeset
85 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
86 scene.SetShaderGlobal("time", GetShaderGlobal[float32](scene, "time") + 0.0005'f)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
87 myengine.RenderScene(scene)
597
fdae5f50fa00 did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents: 535
diff changeset
88
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
89 myengine.Destroy()