Mercurial > games > semicongine
comparison examples/E02_squares.nim @ 61:0f04ba283558
did: rename and update older demos to work with new APIs
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 24 Jan 2023 10:22:38 +0700 |
parents | examples/squares.nim@c57285d292b6 |
children | bdef52f3bc0d |
comparison
equal
deleted
inserted
replaced
60:c57285d292b6 | 61:0f04ba283558 |
---|---|
1 import std/times | |
2 import std/strutils | |
3 import std/math | |
4 import std/random | |
5 | |
6 import semicongine | |
7 | |
8 type | |
9 VertexDataA = object | |
10 position11: PositionAttribute[Vec2] | |
11 color22: ColorAttribute[Vec3] | |
12 index: GenericAttribute[uint32] | |
13 Uniforms = object | |
14 t: Descriptor[float32] | |
15 | |
16 var | |
17 pipeline: RenderPipeline[VertexDataA, Uniforms] | |
18 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32)) | |
19 | |
20 proc globalUpdate(engine: var Engine, dt: float32) = | |
21 uniformdata.t.value += dt | |
22 engine.vulkan.device.updateUniformData(pipeline, uniformdata) | |
23 | |
24 when isMainModule: | |
25 randomize() | |
26 var myengine = igniteEngine("Squares") | |
27 const | |
28 COLUMNS = 10 | |
29 ROWS = 10 | |
30 WIDTH = 2'f32 / COLUMNS | |
31 HEIGHT = 2'f32 / ROWS | |
32 var | |
33 vertices: array[COLUMNS * ROWS * 4, Vec2] | |
34 colors: array[COLUMNS * ROWS * 4, Vec3] | |
35 iValues: array[COLUMNS * ROWS * 4, uint32] | |
36 indices: array[COLUMNS * ROWS * 2, array[3, uint16]] | |
37 | |
38 for row in 0 ..< ROWS: | |
39 for col in 0 ..< COLUMNS: | |
40 let | |
41 y: float32 = (row * 2 / COLUMNS) - 1 | |
42 x: float32 = (col * 2 / ROWS) - 1 | |
43 color = Vec3([(x + 1) / 2, (y + 1) / 2, 0'f32]) | |
44 squareIndex = row * COLUMNS + col | |
45 vertIndex = squareIndex * 4 | |
46 vertices[vertIndex + 0] = Vec2([x, y]) | |
47 vertices[vertIndex + 1] = Vec2([x + WIDTH, y]) | |
48 vertices[vertIndex + 2] = Vec2([x + WIDTH, y + HEIGHT]) | |
49 vertices[vertIndex + 3] = Vec2([x, y + HEIGHT]) | |
50 colors[vertIndex + 0] = color | |
51 colors[vertIndex + 1] = color | |
52 colors[vertIndex + 2] = color | |
53 colors[vertIndex + 3] = color | |
54 iValues[vertIndex + 0] = uint32(squareIndex) | |
55 iValues[vertIndex + 1] = uint32(squareIndex) | |
56 iValues[vertIndex + 2] = uint32(squareIndex) | |
57 iValues[vertIndex + 3] = uint32(squareIndex) | |
58 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + | |
59 1), uint16(vertIndex + 2)] | |
60 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + | |
61 3), uint16(vertIndex + 0)] | |
62 | |
63 | |
64 type PIndexedMesh = IndexedMesh[VertexDataA, | |
65 uint16] # required so we can use ctor with ref/on heap | |
66 var squaremesh = PIndexedMesh( | |
67 vertexData: VertexDataA( | |
68 position11: PositionAttribute[Vec2](data: @vertices), | |
69 color22: ColorAttribute[Vec3](data: @colors), | |
70 index: GenericAttribute[uint32](data: @iValues), | |
71 ), | |
72 indices: @indices | |
73 ) | |
74 var scene = newThing("scene", newThing("squares", squaremesh)) | |
75 | |
76 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]( | |
77 """ | |
78 float pos_weight = index / 100.0; // add some gamma correction? | |
79 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5; | |
80 float v = min(1, max(0, pow(pos_weight - t, 2))); | |
81 v = pow(1 - v, 3000); | |
82 out_color = vec3(in_color.r, in_color.g, v * 0.5); | |
83 """ | |
84 ) | |
85 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
86 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | |
87 myengine, | |
88 scene, | |
89 vertexShader, | |
90 fragmentShader | |
91 ) | |
92 myengine.run(pipeline, globalUpdate) | |
93 pipeline.trash() | |
94 myengine.trash() |