annotate examples/E01_hello_triangle.nim @ 594:512d33d314c4

add: correct swapchain destruction, update 1st example to be working
author Sam <sam@basx.dev>
date Thu, 20 Apr 2023 21:59:52 +0700
parents 21c276c0a968
children fdae5f50fa00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/strutils
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/enumerate
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
3
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
5
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
6
594
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
7 const
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
8 vertexInput = @[
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
9 attr[Vec3f]("position", memoryLocation=VRAM),
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
10 attr[Vec3f]("color", memoryLocation=VRAM),
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
11 ]
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
12 vertexOutput = @[attr[Vec3f]("outcolor")]
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
13 fragOutput = @[attr[Vec4f]("color")]
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
14 vertexCode = compileGlslShader(
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
15 stage=VK_SHADER_STAGE_VERTEX_BIT,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
16 inputs=vertexInput,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
17 outputs=vertexOutput,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
18 main="""gl_Position = vec4(position, 1.0); outcolor = color;"""
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
19 )
594
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
20 fragmentCode = compileGlslShader(
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
21 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
22 inputs=vertexOutput,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
23 outputs=fragOutput,
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
24 main="color = vec4(outcolor, 1);"
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
25 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
26
594
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
27 var
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
28 triangle = newEntity(
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
29 "triangle",
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
30 newMesh(
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
31 [newVec3f(-0.5, 0.5), newVec3f(0, -0.5), newVec3f(0.5, 0.5)],
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
32 [X, Y, Z],
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
33 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
34 )
594
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
35 myengine = initEngine("Hello triangle")
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
36 renderPass = myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
37
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
38 myengine.setRenderer(renderPass)
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
39 myengine.addScene(triangle, vertexInput)
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
40
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
41 while myengine.running and not myengine.keyWasPressed(Escape):
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
42 myengine.updateInputs()
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
43 myengine.renderScene(triangle)
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
44
512d33d314c4 add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents: 535
diff changeset
45 myengine.destroy()