Skip to content

Commit

Permalink
Updated code to match the book text
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Dec 16, 2024
1 parent 4147bf9 commit b2d422c
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 73 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions Chapter09/05_ImportLights/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
9 changes: 9 additions & 0 deletions Chapter09/06_ImportCameras/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
9 changes: 0 additions & 9 deletions Chapter09/08_ImportLights/CMakeLists.txt

This file was deleted.

9 changes: 0 additions & 9 deletions Chapter09/09_ImportCameras/CMakeLists.txt

This file was deleted.

39 changes: 16 additions & 23 deletions Chapter11/06_FinalDemo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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({});
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
28 changes: 14 additions & 14 deletions shared/UtilsAnim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AnimationChannel initChannel(const aiNodeAnim* anim)
return channel;
}

template <typename T> int getTimeIndex(const std::vector<T>& t, float time)
template <typename T> uint32_t getTimeIndex(const std::vector<T>& t, float time)
{
return std::max(
0,
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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),
};
Expand Down Expand Up @@ -200,7 +200,7 @@ void updateAnimation(GLTFContext& glTF, AnimationState& anim, float dt)
// Apply animations
std::function<void(GLTFNodeRef gltfNode, const mat4& parentTransform)> 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) {
Expand All @@ -226,7 +226,7 @@ void updateAnimation(GLTFContext& glTF, AnimationState& anim, float dt)

traverseTree(glTF.root, mat4(1.0f));

for (const std::pair<std::string, GLTFBone>& b : glTF.bonesStorage) {
for (const std::pair<std::string, GLTFBone>& b : glTF.bonesByName) {
if (b.second.boneId != ~0u) {
glTF.matrices[b.second.boneId] = glTF.matrices[b.second.boneId] * b.second.transform;
}
Expand Down Expand Up @@ -275,7 +275,7 @@ void updateAnimationBlending(GLTFContext& glTF, AnimationState& anim1, Animation
// Update skinning
std::function<void(GLTFNodeRef gltfNode, const mat4& parentTransform)> 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);
Expand Down Expand Up @@ -304,7 +304,7 @@ void updateAnimationBlending(GLTFContext& glTF, AnimationState& anim1, Animation

traverseTree(glTF.root, mat4(1.0f));

for (const std::pair<std::string, GLTFBone>& b : glTF.bonesStorage) {
for (const std::pair<std::string, GLTFBone>& b : glTF.bonesByName) {
if (b.second.boneId != ~0u) {
glTF.matrices[b.second.boneId] = glTF.matrices[b.second.boneId] * b.second.transform;
}
Expand Down
2 changes: 1 addition & 1 deletion shared/UtilsAnim.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct MorphingChannel {
struct Animation {
std::unordered_map<int, AnimationChannel> channels;
std::vector<MorphingChannel> morphChannels;
float duration;
float duration; // In seconds
float ticksPerSecond;
std::string name;
};
Expand Down
25 changes: 12 additions & 13 deletions shared/UtilsGLTF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion shared/UtilsGLTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ struct GLTFContext {

std::vector<GLTFNode> nodesStorage;
std::vector<GLTFMesh> meshesStorage;
std::unordered_map<std::string, GLTFBone> bonesStorage;
std::unordered_map<std::string, GLTFBone> bonesByName;

std::vector<MorphTarget> morphTargets;
std::unordered_map<std::string, uint32_t> meshesRemap;
Expand Down

0 comments on commit b2d422c

Please sign in to comment.