comparison examples/E03_hello_cube.nim @ 602:0d8858aa0671

did: migrate to new engine version
author Sam <sam@basx.dev>
date Sat, 22 Apr 2023 17:34:59 +0700
parents 21c276c0a968
children f41c1b78cf5b
comparison
equal deleted inserted replaced
601:7e54bbbcffba 602:0d8858aa0671
11 import std/times 11 import std/times
12 import std/strutils 12 import std/strutils
13 13
14 import semicongine 14 import semicongine
15 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
30
31 proc globalUpdate(engine: var Engine, t, dt: float32) =
32 let ratio = float32(engine.vulkan.frameSize.y) / float32(
33 engine.vulkan.frameSize.x)
34 uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t,
35 Yf32) # * rotate3d(float32(PI), Yf32)
36
37 uniforms.view.value = Unit44f32
38 uniforms.projection.value = Mat44(data: [
39 ratio, 0'f32, 0'f32, 0'f32,
40 0'f32, 1'f32, 0'f32, 0'f32,
41 0'f32, 0'f32, 1'f32, 0'f32,
42 0'f32, 0'f32, 0'f32, 1'f32,
43 ])
44 uniforms.projection.value = perspective(float32(PI / 4), float32(
45 engine.vulkan.frameSize.x) / float32(
46 engine.vulkan.frameSize.y), 0.1'f32, 100'f32)
47 engine.vulkan.device.updateUniformData(pipeline, uniforms)
48
49 const 16 const
50 TopLeftFront = Vec3([-0.5'f32, -0.5'f32, -0.5'f32]) 17 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
51 TopRightFront = Vec3([0.5'f32, -0.5'f32, -0.5'f32]) 18 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32)
52 BottomRightFront = Vec3([0.5'f32, 0.5'f32, -0.5'f32]) 19 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32)
53 BottomLeftFront = Vec3([-0.5'f32, 0.5'f32, -0.5'f32]) 20 BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32)
54 TopLeftBack = Vec3([0.5'f32, -0.5'f32, 0.5'f32]) 21 TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32)
55 TopRightBack = Vec3([-0.5'f32, -0.5'f32, 0.5'f32]) 22 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32)
56 BottomRightBack = Vec3([-0.5'f32, 0.5'f32, 0.5'f32]) 23 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32)
57 BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32]) 24 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32)
58 const 25 const
59 cube_pos = @[ 26 cube_pos = @[
60 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front 27 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
61 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back 28 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
62 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left 29 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
78 let off = i * 4 45 let off = i * 4
79 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] 46 tris.add [off + 0'u16, off + 1'u16, off + 2'u16]
80 tris.add [off + 2'u16, off + 3'u16, off + 0'u16] 47 tris.add [off + 2'u16, off + 3'u16, off + 0'u16]
81 48
82 when isMainModule: 49 when isMainModule:
83 var myengine = igniteEngine("Hello cube") 50 var myengine = initEngine("Hello cube")
84 51
85 # build a mesh 52 const
86 var trianglemesh = new Mesh[VertexDataA, uint16] 53 vertexInput = @[
87 trianglemesh.indexed = true 54 attr[Vec3f]("position", memoryLocation=VRAM),
88 trianglemesh.vertexData = VertexDataA( 55 attr[Vec3f]("color", memoryLocation=VRAMVisible),
89 position: PositionAttribute[Vec3](data: cube_pos), 56 ]
90 color: ColorAttribute[Vec3](data: cube_color), 57 vertexOutput = @[attr[Vec3f]("outcolor")]
91 ) 58 uniforms = @[
92 trianglemesh.indices = tris 59 attr[Mat4]("projection"),
93 var cube = newThing("cube", trianglemesh) 60 attr[Mat4]("view"),
61 attr[Mat4]("model"),
62 ]
63 fragOutput = @[attr[Vec4f]("color")]
64 vertexCode = compileGlslShader(
65 stage=VK_SHADER_STAGE_VERTEX_BIT,
66 inputs=vertexInput,
67 uniforms=uniforms,
68 outputs=vertexOutput,
69 main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);"""
70 )
71 fragmentCode = compileGlslShader(
72 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
73 inputs=vertexOutput,
74 uniforms=uniforms,
75 outputs=fragOutput,
76 main="color = vec4(outcolor, 1);"
77 )
78 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
79 var
80 projection = initShaderGlobal("projection", Unit4f32)
81 view = initShaderGlobal("view", Unit4f32)
82 model = initShaderGlobal("model", Unit4f32)
83 cube = newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color))
84 cube.components.add projection
85 cube.components.add view
86 cube.components.add model
87 myengine.addScene(cube, vertexInput)
94 88
95 # upload data, prepare shaders, etc 89 var t: float32 = cpuTime()
96 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" 90 while myengine.running and not myengine.keyWasPressed(Escape):
97 out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1); 91 myengine.updateInputs()
98 """) 92 setValue[Mat4](model.value, translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32))
99 const fragmentShader = generateFragmentShaderCode[VertexDataA]() 93 setValue[Mat4](
100 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( 94 projection.value,
101 myengine, 95 perspective(
102 cube, 96 float32(PI / 4),
103 vertexShader, 97 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
104 fragmentShader 98 0.1'f32,
105 ) 99 100'f32
106 # show something 100 )
107 myengine.run(pipeline, globalUpdate) 101 )
108 pipeline.trash() 102 t = cpuTime()
109 myengine.trash() 103
104 myengine.renderScene(cube)
105
106 myengine.destroy()