comparison examples/hello_cube.nim @ 521:a25325bec7f2

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 cd73e429fc99
children
comparison
equal deleted inserted replaced
520:cd73e429fc99 521:a25325bec7f2
4 # 4 #
5 # 5 #
6 # 6 #
7 # 7 #
8 # 8 #
9 # 9
10 # 10
11 import std/times 11 import std/times
12 import std/strutils 12 import std/strutils
13 import std/enumerate
14 13
15 import semicongine 14 import semicongine
16 15
17 type 16 type
18 # define type of vertex 17 # define type of vertex
24 view: Descriptor[Mat44] 23 view: Descriptor[Mat44]
25 projection: Descriptor[Mat44] 24 projection: Descriptor[Mat44]
26 25
27 var 26 var
28 pipeline: RenderPipeline[VertexDataA, Uniforms] 27 pipeline: RenderPipeline[VertexDataA, Uniforms]
29 uniforms:Uniforms 28 uniforms: Uniforms
30 t: float32 29 t: float32
31 30
32 31
33 proc globalUpdate(engine: var Engine, dt: float32) = 32 proc globalUpdate(engine: var Engine, dt: float32) =
34 let ratio = float32(engine.vulkan.frameDimension.height) / float32(engine.vulkan.frameDimension.width) 33 let ratio = float32(engine.vulkan.frameDimension.height) / float32(
34 engine.vulkan.frameDimension.width)
35 t += dt 35 t += dt
36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32) # * rotate3d(float32(PI), Yf32) 36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t,
37 Yf32) # * rotate3d(float32(PI), Yf32)
37 38
38 uniforms.view.value = Unit44f32 39 uniforms.view.value = Unit44f32
39 uniforms.projection.value = Mat44(data:[ 40 uniforms.projection.value = Mat44(data: [
40 ratio, 0'f32, 0'f32, 0'f32, 41 ratio, 0'f32, 0'f32, 0'f32,
41 0'f32, 1'f32, 0'f32, 0'f32, 42 0'f32, 1'f32, 0'f32, 0'f32,
42 0'f32, 0'f32, 1'f32, 0'f32, 43 0'f32, 0'f32, 1'f32, 0'f32,
43 0'f32, 0'f32, 0'f32, 1'f32, 44 0'f32, 0'f32, 0'f32, 1'f32,
44 ]) 45 ])
45 uniforms.projection.value = perspective(float32(PI / 4), float32(engine.vulkan.frameDimension.width) / float32(engine.vulkan.frameDimension.height), 0.1'f32, 100'f32) 46 uniforms.projection.value = perspective(float32(PI / 4), float32(
46 for buffer in pipeline.uniformBuffers: 47 engine.vulkan.frameDimension.width) / float32(
47 buffer.updateData(uniforms) 48 engine.vulkan.frameDimension.height), 0.1'f32, 100'f32)
49 pipeline.updateUniformValues(uniforms)
48 50
49 const 51 const
50 TopLeftFront = Vec3([ -0.5'f32, -0.5'f32, -0.5'f32]) 52 TopLeftFront = Vec3([-0.5'f32, -0.5'f32, -0.5'f32])
51 TopRightFront = Vec3([ 0.5'f32, -0.5'f32, -0.5'f32]) 53 TopRightFront = Vec3([0.5'f32, -0.5'f32, -0.5'f32])
52 BottomRightFront = Vec3([ 0.5'f32, 0.5'f32, -0.5'f32]) 54 BottomRightFront = Vec3([0.5'f32, 0.5'f32, -0.5'f32])
53 BottomLeftFront = Vec3([ -0.5'f32, 0.5'f32, -0.5'f32]) 55 BottomLeftFront = Vec3([-0.5'f32, 0.5'f32, -0.5'f32])
54 TopLeftBack = Vec3([ 0.5'f32, -0.5'f32, 0.5'f32]) 56 TopLeftBack = Vec3([0.5'f32, -0.5'f32, 0.5'f32])
55 TopRightBack = Vec3([ -0.5'f32, -0.5'f32, 0.5'f32]) 57 TopRightBack = Vec3([-0.5'f32, -0.5'f32, 0.5'f32])
56 BottomRightBack = Vec3([ -0.5'f32, 0.5'f32, 0.5'f32]) 58 BottomRightBack = Vec3([-0.5'f32, 0.5'f32, 0.5'f32])
57 BottomLeftBack = Vec3([ 0.5'f32, 0.5'f32, 0.5'f32]) 59 BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32])
58 const 60 const
59 cube_pos = @[ 61 cube_pos = @[
60 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front 62 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
61 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back 63 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
62 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left 64 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
76 tris: seq[array[3, uint16]] 78 tris: seq[array[3, uint16]]
77 for i in 0'u16 ..< 6'u16: 79 for i in 0'u16 ..< 6'u16:
78 let off = i * 4 80 let off = i * 4
79 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] 81 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
80 tris.add [off + 2'u16, off + 3'u16, off + 0'u16] 82 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
81 var off = 0'u16 * 4
82 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
83 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
84 # off = 1'u16 * 4
85 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
86 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
87 # off = 4'u16 * 4
88 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
89 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
90 # off = 3'u16 * 4
91 # tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
92 # tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
93 83
94 when isMainModule: 84 when isMainModule:
95 var myengine = igniteEngine("Hello cube") 85 var myengine = igniteEngine("Hello cube")
96 86
97 # build a mesh 87 # build a mesh
99 trianglemesh.vertexData = VertexDataA( 89 trianglemesh.vertexData = VertexDataA(
100 position: PositionAttribute[Vec3](data: cube_pos), 90 position: PositionAttribute[Vec3](data: cube_pos),
101 color: ColorAttribute[Vec3](data: cube_color), 91 color: ColorAttribute[Vec3](data: cube_color),
102 ) 92 )
103 trianglemesh.indices = tris 93 trianglemesh.indices = tris
104 # build a single-object scene graph 94 var cube = newThing("cube", trianglemesh)
105 var triangle = new Thing
106 # add the triangle mesh to the object
107 triangle.parts.add trianglemesh
108 95
109 # upload data, prepare shaders, etc 96 # upload data, prepare shaders, etc
110 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" 97 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
111 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1); 98 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1);
112 """) 99 """)
113 const fragmentShader = generateFragmentShaderCode[VertexDataA]() 100 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
114 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( 101 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
115 myengine, 102 myengine,
116 triangle, 103 cube,
117 vertexShader, 104 vertexShader,
118 fragmentShader 105 fragmentShader
119 ) 106 )
120 # show something 107 # show something
121 myengine.run(pipeline, globalUpdate) 108 myengine.run(pipeline, globalUpdate)