comparison examples/hello_triangle.nim @ 496:b41733878bc6

add: support for instance data
author Sam <sam@basx.dev>
date Mon, 16 Jan 2023 00:35:41 +0700
parents 0c18638c7217
children 3f1111f3b9f8
comparison
equal deleted inserted replaced
495:85f6b1b29c98 496:b41733878bc6
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 id: InstanceAttribute[Vec3[float32]]
20 21
21 var pipeline: RenderPipeline[VertexDataA, void] 22 var pipeline: RenderPipeline[VertexDataA, void]
22 23
23 proc globalUpdate(engine: var Engine, dt: float32) = 24 proc globalUpdate(engine: var Engine, dt: float32) =
24 discard 25 discard
42 # build a mesh 43 # build a mesh
43 var trianglemesh = new Mesh[VertexDataA] 44 var trianglemesh = new Mesh[VertexDataA]
44 trianglemesh.vertexData = VertexDataA( 45 trianglemesh.vertexData = VertexDataA(
45 position: PositionAttribute[Vec2[float32]](data: triangle_pos), 46 position: PositionAttribute[Vec2[float32]](data: triangle_pos),
46 color: ColorAttribute[Vec3[float32]](data: triangle_color), 47 color: ColorAttribute[Vec3[float32]](data: triangle_color),
48 id: InstanceAttribute[Vec3[float32]](data: @[Vec3[float32]([0.5'f32, 0.5'f32, 0.5'f32])]),
47 ) 49 )
48 # build a single-object scene graph 50 # build a single-object scene graph
49 var triangle = new Thing 51 var triangle = new Thing
50 # add the triangle mesh to the object 52 # add the triangle mesh to the object
51 triangle.parts.add trianglemesh 53 triangle.parts.add trianglemesh
52 54
53 # upload data, prepare shaders, etc 55 # upload data, prepare shaders, etc
54 const vertexShader = generateVertexShaderCode[VertexDataA, void]( 56 const vertexShader = generateVertexShaderCode[VertexDataA, void]()
55 # "out_position = uniforms.mat * vec4(in_position, 0, 1);"
56 )
57 const fragmentShader = generateFragmentShaderCode[VertexDataA]() 57 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
58 pipeline = setupPipeline[VertexDataA, void, uint16]( 58 pipeline = setupPipeline[VertexDataA, void, uint16](
59 myengine, 59 myengine,
60 triangle, 60 triangle,
61 vertexShader, 61 vertexShader,