Mercurial > games > semicongine
comparison examples/hello_triangle.nim @ 60:c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 22 Jan 2023 22:46:53 +0700 |
| parents | d7d9420ba675 |
| children |
comparison
equal
deleted
inserted
replaced
| 59:d7d9420ba675 | 60:c57285d292b6 |
|---|---|
| 7 type | 7 type |
| 8 # define type of vertex | 8 # define type of vertex |
| 9 VertexDataA = object | 9 VertexDataA = object |
| 10 position: PositionAttribute[Vec2] | 10 position: PositionAttribute[Vec2] |
| 11 color: ColorAttribute[Vec3] | 11 color: ColorAttribute[Vec3] |
| 12 id: InstanceAttribute[Vec3] | |
| 13 | 12 |
| 14 var pipeline: RenderPipeline[VertexDataA, void] | 13 var pipeline: RenderPipeline[VertexDataA, void] |
| 15 | 14 |
| 16 proc globalUpdate(engine: var Engine, dt: float32) = | 15 proc globalUpdate(engine: var Engine, dt: float32) = |
| 17 discard | 16 discard |
| 18 | 17 |
| 19 # vertex data (types must match the above VertexAttributes) | 18 # vertex data (types must match the above VertexAttributes) |
| 20 const | 19 const |
| 21 triangle_pos = @[ | 20 triangle_pos = @[ |
| 22 Vec2([ 0.0'f32, -0.5'f32]), | 21 Vec2([0.0'f32, -0.5'f32]), |
| 23 Vec2([ 0.5'f32, 0.5'f32]), | 22 Vec2([0.5'f32, 0.5'f32]), |
| 24 Vec2([-0.5'f32, 0.5'f32]), | 23 Vec2([-0.5'f32, 0.5'f32]), |
| 25 ] | 24 ] |
| 26 triangle_color = @[ | 25 triangle_color = @[ |
| 27 Vec3([1.0'f32, 0.0'f32, 0.0'f32]), | 26 Vec3([1.0'f32, 0.0'f32, 0.0'f32]), |
| 28 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), | 27 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), |
| 29 Vec3([0.0'f32, 0.0'f32, 1.0'f32]), | 28 Vec3([0.0'f32, 0.0'f32, 1.0'f32]), |
| 35 # build a mesh | 34 # build a mesh |
| 36 var trianglemesh = new Mesh[VertexDataA] | 35 var trianglemesh = new Mesh[VertexDataA] |
| 37 trianglemesh.vertexData = VertexDataA( | 36 trianglemesh.vertexData = VertexDataA( |
| 38 position: PositionAttribute[Vec2](data: triangle_pos), | 37 position: PositionAttribute[Vec2](data: triangle_pos), |
| 39 color: ColorAttribute[Vec3](data: triangle_color), | 38 color: ColorAttribute[Vec3](data: triangle_color), |
| 40 id: InstanceAttribute[Vec3](data: @[Vec3([0.5'f32, 0.5'f32, 0.5'f32])]), | |
| 41 ) | 39 ) |
| 42 # build a single-object scene graph | 40 # build a single-object scene graph |
| 43 var triangle = new Thing | 41 var triangle = newThing("triangle", trianglemesh) |
| 44 # add the triangle mesh to the object | |
| 45 triangle.parts.add trianglemesh | |
| 46 | 42 |
| 47 # upload data, prepare shaders, etc | 43 # upload data, prepare shaders, etc |
| 48 const vertexShader = generateVertexShaderCode[VertexDataA, void]() | 44 const vertexShader = generateVertexShaderCode[VertexDataA, void]() |
| 49 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 45 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
| 50 pipeline = setupPipeline[VertexDataA, void, void]( | 46 pipeline = setupPipeline[VertexDataA, void, void]( |
