comparison tests/test_gltf.nim @ 1247:c15770761865

add: gltf loading test, gltf loading for materials
author sam <sam@basx.dev>
date Wed, 24 Jul 2024 23:26:34 +0700
parents
children 317bb5a73606
comparison
equal deleted inserted replaced
1246:356089365076 1247:c15770761865
1 import std/os
2 import std/sequtils
3 import std/monotimes
4 import std/times
5 import std/options
6 import std/random
7
8 import ../semiconginev2
9
10 proc test_gltf(time: float32) =
11 var renderdata = InitRenderData()
12
13 type
14 Material = object
15 color: Vec4f
16 colorTexture: int32 = -1
17 metallic: float32 = -1
18 roughness: float32 = -1
19 metallicRoughnessTexture: int32 = -1
20
21 normalTexture: int32 = -1
22 occlusionTexture: int32 = -1
23 emissive: Vec4f = NewVec4f(-1, -1, -1, -1)
24 emissiveTexture: int32 = -1
25 MainDescriptors = object
26 material: GPUValue[Material, UniformBuffer]
27 Shader = object
28 position {.VertexAttribute.}: Vec3f
29 color {.VertexAttribute.}: Vec4f
30 uv {.VertexAttribute.}: Vec2f
31 fragmentColor {.Pass.}: Vec4f
32 fragmentUv {.Pass.}: Vec2f
33 outColor {.ShaderOutput.}: Vec4f
34 descriptors {.DescriptorSets.}: (MainDescriptors, )
35 # code
36 vertexCode: string = """
37 void main() {
38 fragmentColor = color;
39 fragmentUv = uv;
40 gl_Position = vec4(position, 1);
41 }"""
42 fragmentCode: string = """void main() { outColor = fragmentColor;}"""
43 Mesh = object
44 position: GPUArray[Vec3f, VertexBuffer]
45 color: GPUArray[Vec4f, VertexBuffer]
46 uv: GPUArray[Vec2f, VertexBuffer]
47
48 let gltfMesh = LoadMeshes[Mesh, Material](
49 "town.glb",
50 baseColorFactor = "color",
51 baseColorTexture = "colorTexture",
52 metallicFactor = "metallic",
53 roughnessFactor = "roughness",
54 metallicRoughnessTexture = "metallicRoughnessTexture",
55 normalTexture = "normalTexture",
56 occlusionTexture = "occlusionTexture",
57 emissiveTexture = "emissiveTexture",
58 emissiveFactor = "emissive",
59 )
60 var mesh = gltfMesh.meshes[0]
61 renderdata.AssignBuffers(mesh)
62 renderdata.FlushAllMemory()
63
64 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
65
66 var start = getMonoTime()
67 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
68
69 WithNextFrame(framebuffer, commandbuffer):
70
71 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
72
73 WithPipeline(commandbuffer, pipeline):
74
75 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
76
77 # cleanup
78 checkVkResult vkDeviceWaitIdle(vulkan.device)
79 DestroyPipeline(pipeline)
80 DestroyRenderData(renderdata)
81 when isMainModule:
82 var time = 1'f32
83 InitVulkan()
84
85 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT)
86 SetupSwapchain(renderpass = renderpass)
87
88 # tests a simple triangle with minimalistic shader and vertex format
89 test_gltf(time)
90
91 checkVkResult vkDeviceWaitIdle(vulkan.device)
92 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
93 ClearSwapchain()
94
95 DestroyVulkan()