From b2d422cb32477820aa6ff1b38446b6d4485ad4c0 Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Mon, 16 Dec 2024 01:57:25 -0800 Subject: [PATCH] Updated code to match the book text --- CMakeLists.txt | 4 +- Chapter09/05_ImportLights/CMakeLists.txt | 9 +++++ .../src/main.cpp | 0 Chapter09/06_ImportCameras/CMakeLists.txt | 9 +++++ .../src/main.cpp | 0 Chapter09/08_ImportLights/CMakeLists.txt | 9 ----- Chapter09/09_ImportCameras/CMakeLists.txt | 9 ----- Chapter11/06_FinalDemo/src/main.cpp | 39 ++++++++----------- shared/CMakeLists.txt | 2 +- shared/UtilsAnim.cpp | 28 ++++++------- shared/UtilsAnim.h | 2 +- shared/UtilsGLTF.cpp | 25 ++++++------ shared/UtilsGLTF.h | 2 +- 13 files changed, 65 insertions(+), 73 deletions(-) create mode 100644 Chapter09/05_ImportLights/CMakeLists.txt rename Chapter09/{08_ImportLights => 05_ImportLights}/src/main.cpp (100%) create mode 100644 Chapter09/06_ImportCameras/CMakeLists.txt rename Chapter09/{09_ImportCameras => 06_ImportCameras}/src/main.cpp (100%) delete mode 100644 Chapter09/08_ImportLights/CMakeLists.txt delete mode 100644 Chapter09/09_ImportCameras/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 969936d..af85e4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,8 +167,8 @@ add_subdirectory(Chapter09/01_AnimationPlayer) add_subdirectory(Chapter09/02_Skinning) add_subdirectory(Chapter09/03_Morphing) add_subdirectory(Chapter09/04_AnimationBlending) -add_subdirectory(Chapter09/08_ImportLights) -add_subdirectory(Chapter09/09_ImportCameras) +add_subdirectory(Chapter09/05_ImportLights) +add_subdirectory(Chapter09/06_ImportCameras) add_subdirectory(Chapter10/01_OffscreenRendering) add_subdirectory(Chapter10/02_ShadowMapping) diff --git a/Chapter09/05_ImportLights/CMakeLists.txt b/Chapter09/05_ImportLights/CMakeLists.txt new file mode 100644 index 0000000..a78d77c --- /dev/null +++ b/Chapter09/05_ImportLights/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.16) + +project(Chapter09) + +include(../../CMake/CommonMacros.txt) + +SETUP_APP(Ch09_Sample05_ImportLights "Chapter 09") + +target_link_libraries(Ch09_Sample05_ImportLights PRIVATE SharedUtils assimp) diff --git a/Chapter09/08_ImportLights/src/main.cpp b/Chapter09/05_ImportLights/src/main.cpp similarity index 100% rename from Chapter09/08_ImportLights/src/main.cpp rename to Chapter09/05_ImportLights/src/main.cpp diff --git a/Chapter09/06_ImportCameras/CMakeLists.txt b/Chapter09/06_ImportCameras/CMakeLists.txt new file mode 100644 index 0000000..0ae22b6 --- /dev/null +++ b/Chapter09/06_ImportCameras/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.16) + +project(Chapter09) + +include(../../CMake/CommonMacros.txt) + +SETUP_APP(Ch09_Sample06_ImportCameras "Chapter 09") + +target_link_libraries(Ch09_Sample06_ImportCameras PRIVATE SharedUtils assimp) diff --git a/Chapter09/09_ImportCameras/src/main.cpp b/Chapter09/06_ImportCameras/src/main.cpp similarity index 100% rename from Chapter09/09_ImportCameras/src/main.cpp rename to Chapter09/06_ImportCameras/src/main.cpp diff --git a/Chapter09/08_ImportLights/CMakeLists.txt b/Chapter09/08_ImportLights/CMakeLists.txt deleted file mode 100644 index 1bb25c7..0000000 --- a/Chapter09/08_ImportLights/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(Chapter09) - -include(../../CMake/CommonMacros.txt) - -SETUP_APP(Ch09_Sample08_ImportLights "Chapter 09") - -target_link_libraries(Ch09_Sample08_ImportLights PRIVATE SharedUtils assimp) \ No newline at end of file diff --git a/Chapter09/09_ImportCameras/CMakeLists.txt b/Chapter09/09_ImportCameras/CMakeLists.txt deleted file mode 100644 index c0762fa..0000000 --- a/Chapter09/09_ImportCameras/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(Chapter09) - -include(../../CMake/CommonMacros.txt) - -SETUP_APP(Ch09_Sample09_ImportCameras "Chapter 09") - -target_link_libraries(Ch09_Sample09_ImportCameras PRIVATE SharedUtils assimp) \ No newline at end of file diff --git a/Chapter11/06_FinalDemo/src/main.cpp b/Chapter11/06_FinalDemo/src/main.cpp index fa6b03f..222d6d6 100644 --- a/Chapter11/06_FinalDemo/src/main.cpp +++ b/Chapter11/06_FinalDemo/src/main.cpp @@ -699,15 +699,13 @@ int main() if (ssaoEnable) { buf.cmdBindComputePipeline(pipelineSSAO); buf.cmdPushConstants(pcSSAO); + // clang-format off buf.cmdDispatchThreadGroups( - { - .width = 1 + (uint32_t)sizeFb.width / 16, - .height = 1 + (uint32_t)sizeFb.height / 16, - }, - { .textures = { - lvk::TextureHandle(texOpaqueDepth), - lvk::TextureHandle(texSSAO), - } }); + { .width = 1 + (uint32_t)sizeFb.width / 16, + .height = 1 + (uint32_t)sizeFb.height / 16 }, + { .textures = { lvk::TextureHandle(texOpaqueDepth), + lvk::TextureHandle(texSSAO) } }); + // clang-format on // 3. Blur SSAO if (ssaoEnableBlur) { @@ -744,22 +742,19 @@ int main() .texOut = p.texOut.index(), .depthThreshold = pcSSAO.zFar * ssaoDepthThreshold, }); - buf.cmdDispatchThreadGroups( - blurDim, { - .textures = {p.texIn, p.texOut, lvk::TextureHandle(texOpaqueDepth)} - }); + // clang-format off + buf.cmdDispatchThreadGroups(blurDim, { .textures = {p.texIn, p.texOut, lvk::TextureHandle(texOpaqueDepth)} }); + // clang-format on } } // combine SSAO + // clang-format off buf.cmdBeginRendering( - { - .color = {{ .loadOp = lvk::LoadOp_Load, .clearColor = { 1.0f, 1.0f, 1.0f, 1.0f } }}, - }, - { - .color = { { .texture = texOpaqueColorWithSSAO } }, - }, + { .color = {{ .loadOp = lvk::LoadOp_Load, .clearColor = { 1.0f, 1.0f, 1.0f, 1.0f } }} }, + { .color = { { .texture = texOpaqueColorWithSSAO } } }, { .textures = { lvk::TextureHandle(texSSAO), lvk::TextureHandle(texOpaqueColor) } }); + // clang-format on buf.cmdBindRenderPipeline(pipelineCombineSSAO); buf.cmdPushConstants(pcCombineSSAO); buf.cmdBindDepthState({}); @@ -771,15 +766,13 @@ int main() const lvk::Framebuffer framebufferOffscreen = { .color = { { .texture = texSceneColor } }, }; - + // clang-format off buf.cmdBeginRendering( - lvk::RenderPass{ - .color = {{ .loadOp = lvk::LoadOp_Load, .storeOp = lvk::StoreOp_Store }}, - }, + lvk::RenderPass{ .color = {{ .loadOp = lvk::LoadOp_Load, .storeOp = lvk::StoreOp_Store }} }, framebufferOffscreen, { .textures = { lvk::TextureHandle(texHeadsOIT), lvk::TextureHandle(texOpaqueColor), lvk::TextureHandle(texOpaqueColorWithSSAO) }, .buffers = { lvk::BufferHandle(bufferListsOIT) } }); - + // clang-format on const struct { uint64_t bufferTransparencyLists; uint32_t texColor; diff --git a/shared/CMakeLists.txt b/shared/CMakeLists.txt index 7bc8643..ef57ccb 100644 --- a/shared/CMakeLists.txt +++ b/shared/CMakeLists.txt @@ -25,6 +25,6 @@ if(WIN32) target_compile_definitions(SharedUtils PUBLIC "NOMINMAX") endif() -if(UNIX) +if(UNIX AND NOT APPLE) target_link_libraries(SharedUtils PUBLIC tbb) endif() diff --git a/shared/UtilsAnim.cpp b/shared/UtilsAnim.cpp index e667d6a..3343c57 100644 --- a/shared/UtilsAnim.cpp +++ b/shared/UtilsAnim.cpp @@ -33,7 +33,7 @@ AnimationChannel initChannel(const aiNodeAnim* anim) return channel; } -template int getTimeIndex(const std::vector& t, float time) +template uint32_t getTimeIndex(const std::vector& t, float time) { return std::max( 0, @@ -51,8 +51,8 @@ vec3 interpolatePosition(const AnimationChannel& channel, float time) if (channel.pos.size() == 1) return channel.pos[0].pos; - int start = getTimeIndex<>(channel.pos, time); - int end = start + 1; + uint32_t start = getTimeIndex<>(channel.pos, time); + uint32_t end = start + 1; float mix = interpolationVal(channel.pos[start].time, channel.pos[end].time, time); return glm::mix(channel.pos[start].pos, channel.pos[end].pos, mix); } @@ -62,8 +62,8 @@ glm::quat interpolateRotation(const AnimationChannel& channel, float time) if (channel.rot.size() == 1) return channel.rot[0].rot; - int start = getTimeIndex<>(channel.rot, time); - int end = start + 1; + uint32_t start = getTimeIndex<>(channel.rot, time); + uint32_t end = start + 1; float mix = interpolationVal(channel.rot[start].time, channel.rot[end].time, time); return glm::slerp(channel.rot[start].rot, channel.rot[end].rot, mix); } @@ -73,8 +73,8 @@ vec3 interpolateScaling(const AnimationChannel& channel, float time) if (channel.scale.size() == 1) return channel.scale[0].scale; - int start = getTimeIndex<>(channel.scale, time); - int end = start + 1; + uint32_t start = getTimeIndex<>(channel.scale, time); + uint32_t end = start + 1; float coef = interpolationVal(channel.scale[start].time, channel.scale[end].time, time); return glm::mix(channel.scale[start].scale, channel.scale[end].scale, coef); } @@ -115,7 +115,7 @@ MorphState morphTransform(const MorphTarget& target, const MorphingChannel& chan int end = 0; if (channel.key.size() > 0) { - start = getTimeIndex<>(channel.key, time); + start = getTimeIndex(channel.key, time); end = start + 1; mix = interpolationVal(channel.key[start].time, channel.key[end].time, time); } @@ -140,13 +140,13 @@ void initAnimations(GLTFContext& glTF, const aiScene* scene) for (uint32_t c = 0; c < scene->mAnimations[i]->mNumChannels; c++) { const aiNodeAnim* channel = scene->mAnimations[i]->mChannels[c]; const char* boneName = channel->mNodeName.data; - uint32_t boneId = glTF.bonesStorage[boneName].boneId; + uint32_t boneId = glTF.bonesByName[boneName].boneId; if (boneId == ~0u) { for (const GLTFNode& node : glTF.nodesStorage) { if (node.name != boneName) continue; boneId = node.modelMtxId; - glTF.bonesStorage[boneName] = { + glTF.bonesByName[boneName] = { .boneId = boneId, .transform = glTF.hasBones ? glm::inverse(node.transform) : mat4(1), }; @@ -200,7 +200,7 @@ void updateAnimation(GLTFContext& glTF, AnimationState& anim, float dt) // Apply animations std::function traverseTree = [&](GLTFNodeRef gltfNode, const mat4& parentTransform) { - const GLTFBone& bone = glTF.bonesStorage[glTF.nodesStorage[gltfNode].name]; + const GLTFBone& bone = glTF.bonesByName[glTF.nodesStorage[gltfNode].name]; const uint32_t boneId = bone.boneId; if (boneId != ~0u) { @@ -226,7 +226,7 @@ void updateAnimation(GLTFContext& glTF, AnimationState& anim, float dt) traverseTree(glTF.root, mat4(1.0f)); - for (const std::pair& b : glTF.bonesStorage) { + for (const std::pair& b : glTF.bonesByName) { if (b.second.boneId != ~0u) { glTF.matrices[b.second.boneId] = glTF.matrices[b.second.boneId] * b.second.transform; } @@ -275,7 +275,7 @@ void updateAnimationBlending(GLTFContext& glTF, AnimationState& anim1, Animation // Update skinning std::function traverseTree = [&](GLTFNodeRef gltfNode, const mat4& parentTransform) { - const GLTFBone& bone = glTF.bonesStorage[glTF.nodesStorage[gltfNode].name]; + const GLTFBone& bone = glTF.bonesByName[glTF.nodesStorage[gltfNode].name]; const uint32_t boneId = bone.boneId; if (boneId != ~0u) { auto channel1 = activeAnim1.channels.find(boneId); @@ -304,7 +304,7 @@ void updateAnimationBlending(GLTFContext& glTF, AnimationState& anim1, Animation traverseTree(glTF.root, mat4(1.0f)); - for (const std::pair& b : glTF.bonesStorage) { + for (const std::pair& b : glTF.bonesByName) { if (b.second.boneId != ~0u) { glTF.matrices[b.second.boneId] = glTF.matrices[b.second.boneId] * b.second.transform; } diff --git a/shared/UtilsAnim.h b/shared/UtilsAnim.h index 8ed4a21..d069c84 100644 --- a/shared/UtilsAnim.h +++ b/shared/UtilsAnim.h @@ -50,7 +50,7 @@ struct MorphingChannel { struct Animation { std::unordered_map channels; std::vector morphChannels; - float duration; + float duration; // In seconds float ticksPerSecond; std::string name; }; diff --git a/shared/UtilsGLTF.cpp b/shared/UtilsGLTF.cpp index b0052a1..6f9243a 100644 --- a/shared/UtilsGLTF.cpp +++ b/shared/UtilsGLTF.cpp @@ -46,9 +46,9 @@ void loadMaterialTexture( uint32_t getNextMtxId(GLTFContext& gltf, const char* name, uint32_t& nextEmptyId, const mat4& mtx) { - const auto it = gltf.bonesStorage.find(name); + const auto it = gltf.bonesByName.find(name); - const uint32_t mtxId = (it == gltf.bonesStorage.end()) ? nextEmptyId++ : it->second.boneId; + const uint32_t mtxId = (it == gltf.bonesByName.end()) ? nextEmptyId++ : it->second.boneId; if (gltf.matrices.size() <= mtxId) { gltf.matrices.resize(mtxId + 1); @@ -503,12 +503,12 @@ void loadGLTF(GLTFContext& gltf, const char* glTFName, const char* glTFDataPath) const aiBone& bone = *mesh->mBones[id]; const char* boneName = bone.mName.C_Str(); - const bool hasBone = gltf.bonesStorage.contains(boneName); + const bool hasBone = gltf.bonesByName.contains(boneName); - const uint32_t boneId = hasBone ? gltf.bonesStorage[boneName].boneId : numBones++; + const uint32_t boneId = hasBone ? gltf.bonesByName[boneName].boneId : numBones++; if (!hasBone) { - gltf.bonesStorage[boneName] = { + gltf.bonesByName[boneName] = { .boneId = boneId, .transform = aiMatrix4x4ToMat4(bone.mOffsetMatrix), }; @@ -845,7 +845,6 @@ void updateCamera(GLTFContext& gltf, const mat4& model, mat4& view, mat4& proj, void buildTransformsList(GLTFContext& gltf) { gltf.transforms.clear(); - // gltf.matrices.clear(); gltf.opaqueNodes.clear(); gltf.transmissionNodes.clear(); gltf.transparentNodes.clear(); @@ -1038,14 +1037,14 @@ void renderGLTF(GLTFContext& gltf, const mat4& model, const mat4& view, const ma }; buf.cmdBindComputePipeline(gltf.pipelineComputeAnimations); buf.cmdPushConstants(pc); + // clang-format off buf.cmdDispatchThreadGroups( - { - .width = gltf.maxVertices / 16, - }, - { .buffers = { { lvk::BufferHandle(gltf.vertexBuffer) }, - { lvk::BufferHandle(gltf.morphStatesBuffer) }, - { lvk::BufferHandle(gltf.matricesBuffer) }, - { lvk::BufferHandle(gltf.vertexSkinningBuffer) } } }); + { .width = gltf.maxVertices / 16 }, + { .buffers = { lvk::BufferHandle(gltf.vertexBuffer), + lvk::BufferHandle(gltf.morphStatesBuffer), + lvk::BufferHandle(gltf.matricesBuffer), + lvk::BufferHandle(gltf.vertexSkinningBuffer) } }); + // clang-format on } } diff --git a/shared/UtilsGLTF.h b/shared/UtilsGLTF.h index 286d37f..473a074 100644 --- a/shared/UtilsGLTF.h +++ b/shared/UtilsGLTF.h @@ -456,7 +456,7 @@ struct GLTFContext { std::vector nodesStorage; std::vector meshesStorage; - std::unordered_map bonesStorage; + std::unordered_map bonesByName; std::vector morphTargets; std::unordered_map meshesRemap;