Mercurial > games > semicongine
annotate examples/E02_squares.nim @ 302:da0bd61abe91
did: change API for ECS
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 27 Jun 2023 00:17:40 +0700 |
parents | b17df28712d6 |
children | b145a05c2459 |
rev | line source |
---|---|
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
1 import std/sequtils |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/random |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
271 | 4 import ../src/semicongine |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 when isMainModule: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 randomize() |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 const |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 COLUMNS = 10 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 ROWS = 10 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 WIDTH = 2'f32 / COLUMNS |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 HEIGHT = 2'f32 / ROWS |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 var |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
15 vertices: array[COLUMNS * ROWS * 4, Vec3f] |
210 | 16 colors: array[COLUMNS * ROWS * 4, Vec4f] |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 iValues: array[COLUMNS * ROWS * 4, uint32] |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 indices: array[COLUMNS * ROWS * 2, array[3, uint16]] |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 for row in 0 ..< ROWS: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 for col in 0 ..< COLUMNS: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 let |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 y: float32 = (row * 2 / COLUMNS) - 1 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 x: float32 = (col * 2 / ROWS) - 1 |
210 | 25 color = newVec4f((x + 1) / 2, (y + 1) / 2, 0, 1) |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 squareIndex = row * COLUMNS + col |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 vertIndex = squareIndex * 4 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
28 vertices[vertIndex + 0] = newVec3f(x, y) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
29 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
30 vertices[vertIndex + 2] = newVec3f(x + WIDTH, y + HEIGHT) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
31 vertices[vertIndex + 3] = newVec3f(x, y + HEIGHT) |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 colors[vertIndex + 0] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 colors[vertIndex + 1] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 colors[vertIndex + 2] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 colors[vertIndex + 3] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 iValues[vertIndex + 0] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 iValues[vertIndex + 1] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 iValues[vertIndex + 2] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 iValues[vertIndex + 3] = uint32(squareIndex) |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
40 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
41 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
44 const |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
45 vertexInput = @[ |
163 | 46 attr[Vec3f]("position"), |
210 | 47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
163 | 48 attr[uint32]("index"), |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
49 ] |
210 | 50 vertexOutput = @[attr[Vec4f]("outcolor")] |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
51 uniforms = @[attr[float32]("time")] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
52 fragOutput = @[attr[Vec4f]("color")] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
53 vertexCode = compileGlslShader( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
54 stage=VK_SHADER_STAGE_VERTEX_BIT, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
55 inputs=vertexInput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
56 uniforms=uniforms, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
57 outputs=vertexOutput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
58 main=""" |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
59 float pos_weight = index / 100.0; // add some gamma correction? |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
60 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
61 float v = min(1, max(0, pow(pos_weight - t, 2))); |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
62 v = pow(1 - v, 3000); |
210 | 63 outcolor = vec4(color.r, color.g, v * 0.5, 1); |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
64 gl_Position = vec4(position, 1.0); |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
65 """ |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
66 ) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
67 fragmentCode = compileGlslShader( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
68 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
69 inputs=vertexOutput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
70 uniforms=uniforms, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
71 outputs=fragOutput, |
210 | 72 main="color = outcolor;" |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
73 ) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
74 var squaremesh = newMesh( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
75 positions=vertices, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
76 indices=indices, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
77 colors=colors, |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 ) |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
79 setMeshData[uint32](squaremesh, "index", iValues.toSeq) |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
81 var myengine = initEngine("Squares") |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
82 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
83 |
302 | 84 var scene = newScene("scene", newEntity("scene", [], newEntity("squares", {"mesh": Component(squaremesh)}))) |
278 | 85 myengine.addScene(scene, vertexInput, @[], transformAttribute="") |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
86 scene.addShaderGlobal("time", 0.0'f32) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
139
diff
changeset
|
87 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
88 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
89 myengine.renderScene(scene) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
90 |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
91 myengine.destroy() |