comparison examples/hello_triangle.nim @ 482:1670f8e70964

add: clean examples, update build configs
author Sam <sam@basx.dev>
date Tue, 10 Jan 2023 00:24:37 +0700
parents 14e5151f68d1
children b4a972bd37d5
comparison
equal deleted inserted replaced
481:c472abfcee57 482:1670f8e70964
4 import zamikongine/mesh 4 import zamikongine/mesh
5 import zamikongine/thing 5 import zamikongine/thing
6 import zamikongine/shader 6 import zamikongine/shader
7 7
8 type 8 type
9 # define type of vertex
9 VertexDataA = object 10 VertexDataA = object
10 position11: VertexAttribute[Vec2[float32]] 11 position: VertexAttribute[Vec2[float32]]
11 color22: VertexAttribute[Vec3[float32]] 12 color: VertexAttribute[Vec3[float32]]
13
14 # vertex data (types must match the above VertexAttributes)
15 const
16 triangle_pos = @[
17 Vec2([-0.5'f32, -0.5'f32]),
18 Vec2([ 0.5'f32, 0.5'f32]),
19 Vec2([-0.5'f32, 0.5'f32]),
20 ]
21 triangle_color = @[
22 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
23 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
24 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
25 ]
12 26
13 when isMainModule: 27 when isMainModule:
14 var myengine = igniteEngine() 28 var myengine = igniteEngine()
15 var mymesh1 = new Mesh[VertexDataA] 29
16 mymesh1.vertexData = VertexDataA( 30 # build a mesh
17 position11: VertexAttribute[Vec2[float32]]( 31 var trianglemesh = new Mesh[VertexDataA]
18 data: @[ 32 trianglemesh.vertexData = VertexDataA(
19 Vec2([-0.5'f32, -0.5'f32]), 33 position: VertexAttribute[Vec2[float32]](data: triangle_pos),
20 Vec2([ 0.5'f32, 0.5'f32]), 34 color: VertexAttribute[Vec3[float32]](data: triangle_color),
21 Vec2([-0.5'f32, 0.5'f32]),
22 ]
23 ),
24 color22: VertexAttribute[Vec3[float32]](
25 data: @[
26 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
27 Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
28 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
29 ]
30 )
31 ) 35 )
32 var mymesh2 = new IndexedMesh[VertexDataA, uint16] 36 # build a single-object scene graph
33 mymesh2.vertexData = VertexDataA( 37 var triangle = new Thing
34 position11: VertexAttribute[Vec2[float32]]( 38 # add the triangle mesh to the object
35 data: @[ 39 triangle.parts.add trianglemesh
36 Vec2([ 0.0'f32, -0.7'f32]),
37 Vec2([ 0.6'f32, 0.1'f32]),
38 Vec2([ 0.3'f32, 0.4'f32]),
39 ]
40 ),
41 color22: VertexAttribute[Vec3[float32]](
42 data: @[
43 Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
44 Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
45 Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
46 ]
47 )
48 )
49 mymesh2.indices = @[[0'u16, 1'u16, 2'u16]]
50 var athing = new Thing
51 athing.parts.add mymesh1
52 var childthing = new Thing
53 childthing.parts.add mymesh2
54 athing.children.add childthing
55 40
41 # upload data, prepare shaders, etc
56 setupPipeline[VertexDataA, uint16]( 42 setupPipeline[VertexDataA, uint16](
57 myengine, 43 myengine,
58 athing, 44 triangle,
59 generateVertexShaderCode[VertexDataA]("main", "position11", "color22"), 45 generateVertexShaderCode[VertexDataA]("main", "position", "color"),
60 generateFragmentShaderCode[VertexDataA]("main"), 46 generateFragmentShaderCode[VertexDataA]("main"),
61 ) 47 )
48 # show something
62 myengine.fullThrottle() 49 myengine.fullThrottle()
63 myengine.trash() 50 myengine.trash()