Mercurial > games > semicongine
comparison examples/hello_triangle.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 | 9edca5dc4e93 |
children | 7f99b21a8777 |
comparison
equal
deleted
inserted
replaced
32:9edca5dc4e93 | 33:94c38e4b5782 |
---|---|
15 type | 15 type |
16 # define type of vertex | 16 # define type of vertex |
17 VertexDataA = object | 17 VertexDataA = object |
18 position: PositionAttribute[Vec2[float32]] | 18 position: PositionAttribute[Vec2[float32]] |
19 color: ColorAttribute[Vec3[float32]] | 19 color: ColorAttribute[Vec3[float32]] |
20 Uniforms = object | |
21 mat: Descriptor[Mat44[float32]] | |
22 dt: Descriptor[float32] | |
23 | 20 |
24 var pipeline: RenderPipeline[VertexDataA, Uniforms] | 21 var pipeline: RenderPipeline[VertexDataA, void] |
25 | 22 |
26 var pos = 0'f32; | |
27 var uniforms = Uniforms( | |
28 mat: Descriptor[Mat44[float32]](value: Unit44f32), | |
29 dt: Descriptor[float32](value: 0'f32), | |
30 ) | |
31 var scaledir = 1'f32 | |
32 proc globalUpdate(engine: var Engine, dt: float32) = | 23 proc globalUpdate(engine: var Engine, dt: float32) = |
33 uniforms.mat.value = uniforms.mat.value * scale3d(1 + scaledir * dt, 1 + scaledir * dt, 0'f32) | 24 discard |
34 if uniforms.mat.value[0, 0] > 2'f32 or uniforms.mat.value[0, 0] < 0.5'f32: | |
35 scaledir = - scaledir | |
36 for buffer in pipeline.uniformBuffers: | |
37 buffer.updateData(uniforms) | |
38 echo uniforms.mat.value | |
39 | 25 |
40 # vertex data (types must match the above VertexAttributes) | 26 # vertex data (types must match the above VertexAttributes) |
41 const | 27 const |
42 triangle_pos = @[ | 28 triangle_pos = @[ |
43 Vec2([ 0.0'f32, -0.5'f32]), | 29 Vec2([ 0.0'f32, -0.5'f32]), |
63 var triangle = new Thing | 49 var triangle = new Thing |
64 # add the triangle mesh to the object | 50 # add the triangle mesh to the object |
65 triangle.parts.add trianglemesh | 51 triangle.parts.add trianglemesh |
66 | 52 |
67 # upload data, prepare shaders, etc | 53 # upload data, prepare shaders, etc |
68 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]( | 54 const vertexShader = generateVertexShaderCode[VertexDataA, void]( |
69 # have 1 at: | 55 # "out_position = uniforms.mat * vec4(in_position, 0, 1);" |
70 # [2][0] [0][3] | |
71 # "out_position = vec4(in_position[0] + uniforms.mat[0][0], in_position[1] + uniforms.mat[0][0], 0, 1);" | |
72 "out_position = uniforms.mat * vec4(in_position, 0, 1);" | |
73 # "out_position = vec4(in_position, 0, 1);" | |
74 ) | 56 ) |
75 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 57 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
76 static: | 58 pipeline = setupPipeline[VertexDataA, void, uint16]( |
77 echo "--------------" | |
78 for (i, line) in enumerate(vertexShader.splitLines()): | |
79 echo $(i + 1) & " " & line | |
80 echo "--------------" | |
81 echo fragmentShader | |
82 echo "--------------" | |
83 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | |
84 myengine, | 59 myengine, |
85 triangle, | 60 triangle, |
86 vertexShader, | 61 vertexShader, |
87 fragmentShader | 62 fragmentShader |
88 ) | 63 ) |