Mercurial > games > semicongine
annotate examples/E02_squares.nim @ 777:754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 07 Aug 2023 00:23:00 +0700 |
parents | a4c757f5d17f |
children | 812b5e28f441 |
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 |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/random |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
732 | 4 import ../src/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 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 when isMainModule: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 randomize() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 COLUMNS = 10 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 ROWS = 10 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 WIDTH = 2'f32 / COLUMNS |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 HEIGHT = 2'f32 / ROWS |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 var |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
15 vertices: array[COLUMNS * ROWS * 4, Vec3f] |
671 | 16 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
|
17 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
|
18 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
|
19 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 for row in 0 ..< ROWS: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 for col in 0 ..< COLUMNS: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 let |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 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
|
24 x: float32 = (col * 2 / ROWS) - 1 |
671 | 25 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
|
26 squareIndex = row * COLUMNS + col |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 vertIndex = squareIndex * 4 |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
28 vertices[vertIndex + 0] = newVec3f(x, y) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
29 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
30 vertices[vertIndex + 2] = newVec3f(x + WIDTH, y + HEIGHT) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
31 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
|
32 colors[vertIndex + 0] = color |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 colors[vertIndex + 1] = color |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 colors[vertIndex + 2] = color |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 colors[vertIndex + 3] = color |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 iValues[vertIndex + 0] = uint32(squareIndex) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 iValues[vertIndex + 1] = uint32(squareIndex) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 iValues[vertIndex + 2] = uint32(squareIndex) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 iValues[vertIndex + 3] = uint32(squareIndex) |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
40 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
|
41 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
|
42 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
44 const |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
45 inputs = @[ |
624 | 46 attr[Vec3f]("position"), |
671 | 47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
624 | 48 attr[uint32]("index"), |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
49 ] |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
50 intermediate = @[attr[Vec4f]("outcolor")] |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
51 uniforms = @[attr[float32]("time")] |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
52 outputs = @[attr[Vec4f]("color")] |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
53 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
54 inputs=inputs, |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
55 intermediate=intermediate, |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
56 outputs=outputs, |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
57 uniforms=uniforms, |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
58 vertexCode=""" |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 v = pow(1 - v, 3000); |
671 | 63 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
|
64 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
|
65 """, |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
66 fragmentCode="color = outcolor;", |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
67 ) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
68 var squaremesh = newMesh( |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
69 positions=vertices, |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
70 indices=indices, |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
71 colors=colors, |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 ) |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
73 setMeshData[uint32](squaremesh, "index", iValues.toSeq) |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
75 var myengine = initEngine("Squares") |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
76 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
77 |
763 | 78 var scene = newScene("scene", newEntity("scene", [], newEntity("squares", {"mesh": Component(squaremesh)}))) |
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(scene, inputs, @[], transformAttribute="") |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
80 scene.addShaderGlobal("time", 0.0'f32) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
600
diff
changeset
|
81 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
|
82 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) |
597
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
83 myengine.renderScene(scene) |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
84 |
fdae5f50fa00
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
535
diff
changeset
|
85 myengine.destroy() |