comparison examples/E03_hello_cube.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 #
2 # TODO: Needs Depth-Buffer first!
3 #
4 #
5 #
6 #
7 #
8 #
9
10
11 import std/times
12 import std/strutils
13
14 import semicongine
15
16 type
17 # define type of vertex
18 VertexDataA = object
19 position: PositionAttribute[Vec3]
20 color: ColorAttribute[Vec3]
21 Uniforms = object
22 model: Descriptor[Mat44]
23 view: Descriptor[Mat44]
24 projection: Descriptor[Mat44]
25
26 var
27 pipeline: RenderPipeline[VertexDataA, Uniforms]
28 uniforms: Uniforms
29 t: float32
30
31
32 proc globalUpdate(engine: var Engine, dt: float32) =
33 let ratio = float32(engine.vulkan.frameSize.y) / float32(
34 engine.vulkan.frameSize.x)
35 t += dt
36 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t,
37 Yf32) # * rotate3d(float32(PI), Yf32)
38
39 uniforms.view.value = Unit44f32
40 uniforms.projection.value = Mat44(data: [
41 ratio, 0'f32, 0'f32, 0'f32,
42 0'f32, 1'f32, 0'f32, 0'f32,
43 0'f32, 0'f32, 1'f32, 0'f32,
44 0'f32, 0'f32, 0'f32, 1'f32,
45 ])
46 uniforms.projection.value = perspective(float32(PI / 4), float32(
47 engine.vulkan.frameSize.x) / float32(
48 engine.vulkan.frameSize.y), 0.1'f32, 100'f32)
49 engine.vulkan.device.updateUniformData(pipeline, uniforms)
50
51 const
52 TopLeftFront = Vec3([-0.5'f32, -0.5'f32, -0.5'f32])
53 TopRightFront = Vec3([0.5'f32, -0.5'f32, -0.5'f32])
54 BottomRightFront = Vec3([0.5'f32, 0.5'f32, -0.5'f32])
55 BottomLeftFront = Vec3([-0.5'f32, 0.5'f32, -0.5'f32])
56 TopLeftBack = Vec3([0.5'f32, -0.5'f32, 0.5'f32])
57 TopRightBack = Vec3([-0.5'f32, -0.5'f32, 0.5'f32])
58 BottomRightBack = Vec3([-0.5'f32, 0.5'f32, 0.5'f32])
59 BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32])
60 const
61 cube_pos = @[
62 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
63 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
64 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
65 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
66 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
67 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
68 ]
69 cube_color = @[
70 Rf32, Rf32, Rf32, Rf32,
71 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32,
72 Gf32, Gf32, Gf32, Gf32,
73 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32,
74 Bf32, Bf32, Bf32, Bf32,
75 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32,
76 ]
77 var
78 tris: seq[array[3, uint16]]
79 for i in 0'u16 ..< 6'u16:
80 let off = i * 4
81 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
82 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
83
84 when isMainModule:
85 var myengine = igniteEngine("Hello cube")
86
87 # build a mesh
88 var trianglemesh = new IndexedMesh[VertexDataA, uint16]
89 trianglemesh.vertexData = VertexDataA(
90 position: PositionAttribute[Vec3](data: cube_pos),
91 color: ColorAttribute[Vec3](data: cube_color),
92 )
93 trianglemesh.indices = tris
94 var cube = newThing("cube", trianglemesh)
95
96 # upload data, prepare shaders, etc
97 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
98 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1);
99 """)
100 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
101 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
102 myengine,
103 cube,
104 vertexShader,
105 fragmentShader
106 )
107 # show something
108 myengine.run(pipeline, globalUpdate)
109 pipeline.trash()
110 myengine.trash()