Mercurial > games > semicongine
comparison examples/squares.nim @ 33:94c38e4b5782
did: refactoring, move more from make to nimscript
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 15 Jan 2023 23:23:54 +0700 |
| parents | |
| children | c3c963e7c1a6 |
comparison
equal
deleted
inserted
replaced
| 32:9edca5dc4e93 | 33:94c38e4b5782 |
|---|---|
| 1 import std/times | |
| 2 import std/strutils | |
| 3 import std/math | |
| 4 import std/random | |
| 5 import std/enumerate | |
| 6 | |
| 7 import zamikongine/engine | |
| 8 import zamikongine/math/vector | |
| 9 import zamikongine/math/matrix | |
| 10 import zamikongine/vertex | |
| 11 import zamikongine/descriptor | |
| 12 import zamikongine/mesh | |
| 13 import zamikongine/thing | |
| 14 import zamikongine/shader | |
| 15 import zamikongine/buffer | |
| 16 | |
| 17 type | |
| 18 VertexDataA = object | |
| 19 position11: PositionAttribute[Vec2[float32]] | |
| 20 color22: ColorAttribute[Vec3[float32]] | |
| 21 index: GenericAttribute[uint32] | |
| 22 Uniforms = object | |
| 23 t: Descriptor[float32] | |
| 24 | |
| 25 var | |
| 26 pipeline: RenderPipeline[VertexDataA, Uniforms] | |
| 27 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32)) | |
| 28 | |
| 29 proc globalUpdate(engine: var Engine, dt: float32) = | |
| 30 uniformdata.t.value += dt | |
| 31 for buffer in pipeline.uniformBuffers: | |
| 32 buffer.updateData(uniformdata) | |
| 33 | |
| 34 when isMainModule: | |
| 35 randomize() | |
| 36 var myengine = igniteEngine("A lot of triangles") | |
| 37 const | |
| 38 COLUMNS = 10 | |
| 39 ROWS = 10 | |
| 40 WIDTH = 2'f32 / COLUMNS | |
| 41 HEIGHT = 2'f32 / ROWS | |
| 42 var | |
| 43 vertices: array[COLUMNS * ROWS * 4, Vec2[float32]] | |
| 44 colors: array[COLUMNS * ROWS * 4, Vec3[float32]] | |
| 45 iValues: array[COLUMNS * ROWS * 4, uint32] | |
| 46 indices: array[COLUMNS * ROWS * 2, array[3, uint16]] | |
| 47 | |
| 48 for row in 0 ..< ROWS: | |
| 49 for col in 0 ..< COLUMNS: | |
| 50 let | |
| 51 y: float32 = (row * 2 / COLUMNS) - 1 | |
| 52 x: float32 = (col * 2 / ROWS) - 1 | |
| 53 color = Vec3[float32]([(x + 1) / 2, (y + 1) / 2, 0'f32]) | |
| 54 squareIndex = row * COLUMNS + col | |
| 55 vertIndex = squareIndex * 4 | |
| 56 vertices[vertIndex + 0] = Vec2[float32]([x, y]) | |
| 57 vertices[vertIndex + 1] = Vec2[float32]([x + WIDTH, y]) | |
| 58 vertices[vertIndex + 2] = Vec2[float32]([x + WIDTH, y + HEIGHT]) | |
| 59 vertices[vertIndex + 3] = Vec2[float32]([x, y + HEIGHT]) | |
| 60 colors[vertIndex + 0] = color | |
| 61 colors[vertIndex + 1] = color | |
| 62 colors[vertIndex + 2] = color | |
| 63 colors[vertIndex + 3] = color | |
| 64 iValues[vertIndex + 0] = uint32(squareIndex) | |
| 65 iValues[vertIndex + 1] = uint32(squareIndex) | |
| 66 iValues[vertIndex + 2] = uint32(squareIndex) | |
| 67 iValues[vertIndex + 3] = uint32(squareIndex) | |
| 68 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] | |
| 69 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] | |
| 70 | |
| 71 var scene = new Thing | |
| 72 | |
| 73 type PIndexedMesh = ref IndexedMesh[VertexDataA, uint16] # required so we can use ctor with ref/on heap | |
| 74 var squaremesh = PIndexedMesh( | |
| 75 vertexData: VertexDataA( | |
| 76 position11: PositionAttribute[Vec2[float32]](data: @vertices), | |
| 77 color22: ColorAttribute[Vec3[float32]](data: @colors), | |
| 78 index: GenericAttribute[uint32](data: @iValues), | |
| 79 ), | |
| 80 indices: @indices | |
| 81 ) | |
| 82 var childthing = new Thing | |
| 83 childthing.parts.add squaremesh | |
| 84 scene.children.add childthing | |
| 85 | |
| 86 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]( | |
| 87 """ | |
| 88 float pos_weight = index / 100.0; // add some gamma correction? | |
| 89 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5; | |
| 90 float v = min(1, max(0, pow(pos_weight - t, 2))); | |
| 91 v = pow(1 - v, 3000); | |
| 92 out_color = vec3(in_color.r, in_color.g, v * 0.5); | |
| 93 """ | |
| 94 ) | |
| 95 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | |
| 96 static: | |
| 97 echo "--------------" | |
| 98 for (i, line) in enumerate(vertexShader.splitLines()): | |
| 99 echo $(i + 1) & " " & line | |
| 100 echo "--------------" | |
| 101 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | |
| 102 myengine, | |
| 103 scene, | |
| 104 vertexShader, | |
| 105 fragmentShader | |
| 106 ) | |
| 107 myengine.run(pipeline, globalUpdate) | |
| 108 pipeline.trash() | |
| 109 myengine.trash() |
