comparison examples/E01_hello_triangle.nim @ 522:f2c97bdbb0b3

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
children eb92723db7b2
comparison
equal deleted inserted replaced
521:a25325bec7f2 522:f2c97bdbb0b3
1 import std/times
2 import std/strutils
3 import std/enumerate
4
5 import semicongine
6
7 type
8 # define type of vertex
9 VertexDataA = object
10 position: PositionAttribute[Vec2]
11 color: ColorAttribute[Vec3]
12
13 var pipeline: RenderPipeline[VertexDataA, void]
14
15 proc globalUpdate(engine: var Engine, dt: float32) =
16 discard
17
18 # vertex data (types must match the above VertexAttributes)
19 const
20 triangle_pos = @[
21 Vec2([0.0'f32, -0.5'f32]),
22 Vec2([0.5'f32, 0.5'f32]),
23 Vec2([-0.5'f32, 0.5'f32]),
24 ]
25 triangle_color = @[
26 Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
27 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
28 Vec3([0.0'f32, 0.0'f32, 1.0'f32]),
29 ]
30
31 when isMainModule:
32 var myengine = igniteEngine("Hello triangle")
33
34 # build a mesh
35 var trianglemesh = new Mesh[VertexDataA]
36 trianglemesh.vertexData = VertexDataA(
37 position: PositionAttribute[Vec2](data: triangle_pos),
38 color: ColorAttribute[Vec3](data: triangle_color),
39 )
40 # build a single-object scene graph
41 var triangle = newThing("triangle", trianglemesh)
42
43 # upload data, prepare shaders, etc
44 const vertexShader = generateVertexShaderCode[VertexDataA, void]()
45 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
46 pipeline = setupPipeline[VertexDataA, void, void](
47 myengine,
48 triangle,
49 vertexShader,
50 fragmentShader
51 )
52 # show something
53 myengine.run(pipeline, globalUpdate)
54 pipeline.trash()
55 myengine.trash()