Mercurial > games > semicongine
annotate examples/E02_squares.nim @ 139:34112bf5abf8
add: small buffer adjustment
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 22 Apr 2023 16:52:55 +0700 |
parents | ac2b6777c5db |
children | a4e6e76128e6 |
rev | line source |
---|---|
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/times |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
2 import std/sequtils |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strutils |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/math |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/random |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
7 import semicongine |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 when isMainModule: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 randomize() |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 const |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 COLUMNS = 10 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 ROWS = 10 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 WIDTH = 2'f32 / COLUMNS |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 HEIGHT = 2'f32 / ROWS |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 var |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
18 vertices: array[COLUMNS * ROWS * 4, Vec3f] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
19 colors: array[COLUMNS * ROWS * 4, Vec3f] |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 iValues: array[COLUMNS * ROWS * 4, uint32] |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 indices: array[COLUMNS * ROWS * 2, array[3, uint16]] |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 for row in 0 ..< ROWS: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 for col in 0 ..< COLUMNS: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 let |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 y: float32 = (row * 2 / COLUMNS) - 1 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 x: float32 = (col * 2 / ROWS) - 1 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
28 color = Vec3f([(x + 1) / 2, (y + 1) / 2, 0'f32]) |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 squareIndex = row * COLUMNS + col |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 vertIndex = squareIndex * 4 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
31 vertices[vertIndex + 0] = newVec3f(x, y) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
32 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
33 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
|
34 vertices[vertIndex + 3] = newVec3f(x, y + HEIGHT) |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 colors[vertIndex + 0] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 colors[vertIndex + 1] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 colors[vertIndex + 2] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 colors[vertIndex + 3] = color |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 iValues[vertIndex + 0] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 iValues[vertIndex + 1] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 iValues[vertIndex + 2] = uint32(squareIndex) |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 iValues[vertIndex + 3] = uint32(squareIndex) |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
43 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
|
44 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
|
45 |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
47 const |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
48 vertexInput = @[ |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
49 attr[Vec3f]("position", memoryLocation=VRAM), |
139 | 50 attr[Vec3f]("color", memoryLocation=VRAMVisible), |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
51 attr[uint32]("index", memoryLocation=VRAM), |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
52 ] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
53 vertexOutput = @[attr[Vec3f]("outcolor")] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
54 uniforms = @[attr[float32]("time")] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
55 fragOutput = @[attr[Vec4f]("color")] |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
56 vertexCode = compileGlslShader( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
57 stage=VK_SHADER_STAGE_VERTEX_BIT, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
58 inputs=vertexInput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
59 uniforms=uniforms, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
60 outputs=vertexOutput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
61 main=""" |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 v = pow(1 - v, 3000); |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
66 outcolor = vec3(color.r, color.g, v * 0.5); |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
67 gl_Position = vec4(position, 1.0); |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
68 """ |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
69 ) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
70 fragmentCode = compileGlslShader( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
71 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
72 inputs=vertexOutput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
73 uniforms=uniforms, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
74 outputs=fragOutput, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
75 main="color = vec4(outcolor, 1);" |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
76 ) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
77 var squaremesh = newMesh( |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
78 positions=vertices, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
79 indices=indices, |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
80 colors=colors, |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 ) |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
82 setMeshData[uint32](squaremesh, "index", iValues.toSeq) |
137 | 83 var i = 0'u64 |
84 var (p, l) = squaremesh.getRawData("color") | |
85 while i < uint64(l): | |
86 echo (cast[ptr float32]((cast[uint64](p) + i)))[] | |
87 inc i | |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 |
136
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
89 var myengine = initEngine("Squares") |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
90 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
91 |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
92 var scene = newEntity("scene", newEntity("squares", squaremesh)) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
93 myengine.addScene(scene, vertexInput) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
94 var time = initShaderGlobal("time", 0.0'f32) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
95 scene.components.add time |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
96 while myengine.running and not myengine.keyWasPressed(Escape): |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
97 myengine.updateInputs() |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
98 setValue[float32](time.value, get[float32](time.value) + 0.0005) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
99 myengine.renderScene(scene) |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
100 |
fc5db4f1f94e
did: rewrite example 02 with new engine approach
Sam <sam@basx.dev>
parents:
74
diff
changeset
|
101 myengine.destroy() |