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 25, 2024
1 parent c1b4d47 commit 7faf3f2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Chapter08/02_SceneGraph/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool editTransformUI(const mat4& view, const mat4& projection, mat4& matrix)

int* textureToEdit = nullptr;

bool editMaterialUI(Scene& scene, MeshData& meshData, int node, int& outUpdateMaterialIndex, TextureCache& textureCache)
bool editMaterialUI(Scene& scene, MeshData& meshData, int node, int& outUpdateMaterialIndex, const TextureCache& textureCache)
{
if (!scene.materialForNode.contains(node))
return false;
Expand Down Expand Up @@ -141,7 +141,7 @@ bool editMaterialUI(Scene& scene, MeshData& meshData, int node, int& outUpdateMa
}

void editNodeUI(
Scene& scene, MeshData& meshData, const mat4& view, const mat4 proj, int node, int& outUpdateMaterialIndex, TextureCache& textureCache)
Scene& scene, MeshData& meshData, const mat4& view, const mat4 proj, int node, int& outUpdateMaterialIndex, const TextureCache& textureCache)
{
ImGuizmo::SetOrthographic(false);
ImGuizmo::BeginFrame();
Expand Down
14 changes: 8 additions & 6 deletions Chapter08/SceneUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ std::string convertTexture(
const int maxNewWidth = DEMO_TEXTURE_MAX_SIZE;
const int maxNewHeight = DEMO_TEXTURE_MAX_SIZE;

if (!std::filesystem::exists(DEMO_TEXTURE_CACHE_FOLDER)) {
std::filesystem::create_directories(DEMO_TEXTURE_CACHE_FOLDER);
namespace fs = std::filesystem;

if (!fs::exists(DEMO_TEXTURE_CACHE_FOLDER)) {
fs::create_directories(DEMO_TEXTURE_CACHE_FOLDER);
}

const std::string srcFile = replaceAll(basePath + file, "\\", "/");
Expand Down Expand Up @@ -210,16 +212,16 @@ void traverse(const aiScene* sourceScene, Scene& scene, aiNode* N, int parent, i
printPrefix(depth);
printf("Node[%d].name = %s\n", newNode, N->mName.C_Str());

const uint32_t stringID = (uint32_t)scene.names.size();
scene.names.push_back(std::string(N->mName.C_Str()));
const uint32_t stringID = (uint32_t)scene.nodeNames.size();
scene.nodeNames.push_back(std::string(N->mName.C_Str()));
scene.nameForNode[newNode] = stringID;
}

for (size_t i = 0; i < N->mNumMeshes; i++) {
const int newSubNode = addNode(scene, newNode, depth + 1);

const uint32_t stringID = (uint32_t)scene.names.size();
scene.names.push_back(std::string(N->mName.C_Str()) + "_Mesh_" + std::to_string(i));
const uint32_t stringID = (uint32_t)scene.nodeNames.size();
scene.nodeNames.push_back(std::string(N->mName.C_Str()) + "_Mesh_" + std::to_string(i));
scene.nameForNode[newSubNode] = stringID;

const int mesh = (int)N->mMeshes[i];
Expand Down
2 changes: 1 addition & 1 deletion deps/bootstrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"source": {
"type": "git",
"url": "https://github.com/corporateshark/lightweightvk.git",
"revision": "12bbe18d68ac288c02cb871dfae92facb3dc711f"
"revision": "c297911d8daefb26d4e0a9826a667c6ea9807a46"
}
},
{
Expand Down
18 changes: 9 additions & 9 deletions shared/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int findNodeByName(const Scene& scene, const std::string& name)
if (scene.nameForNode.contains(i)) {
int strID = scene.nameForNode.at(i);
if (strID > -1)
if (scene.names[strID] == name)
if (scene.nodeNames[strID] == name)
return (int)i;
}

Expand Down Expand Up @@ -131,7 +131,7 @@ void loadScene(const char* fileName, Scene& scene)

if (!feof(f)) {
loadMap(f, scene.nameForNode);
loadStringList(f, scene.names);
loadStringList(f, scene.nodeNames);
loadStringList(f, scene.materialNames);
}

Expand Down Expand Up @@ -169,9 +169,9 @@ void saveScene(const char* fileName, const Scene& scene)
saveMap(f, scene.materialForNode);
saveMap(f, scene.meshForNode);

if (!scene.names.empty() && !scene.nameForNode.empty()) {
if (!scene.nodeNames.empty() && !scene.nameForNode.empty()) {
saveMap(f, scene.nameForNode);
saveStringList(f, scene.names);
saveStringList(f, scene.nodeNames);
saveStringList(f, scene.materialNames);
}
fclose(f);
Expand Down Expand Up @@ -289,7 +289,7 @@ void mergeScenes(
};

scene.nameForNode[0] = 0;
scene.names = { "NewRoot" };
scene.nodeNames = { "NewRoot" };

scene.localTransform.push_back(glm::mat4(1.f));
scene.globalTransform.push_back(glm::mat4(1.f));
Expand All @@ -299,7 +299,7 @@ void mergeScenes(

int offs = 1;
int meshOffs = 0;
int nameOffs = (int)scene.names.size();
int nameOffs = (int)scene.nodeNames.size();
int materialOfs = 0;
auto meshCount = meshCounts.begin();

Expand All @@ -313,7 +313,7 @@ void mergeScenes(

mergeVectors(scene.hierarchy, s->hierarchy);

mergeVectors(scene.names, s->names);
mergeVectors(scene.nodeNames, s->nodeNames);
if (mergeMaterials)
mergeVectors(scene.materialNames, s->materialNames);

Expand All @@ -328,7 +328,7 @@ void mergeScenes(
offs += nodeCount;

materialOfs += (int)s->materialNames.size();
nameOffs += (int)s->names.size();
nameOffs += (int)s->nodeNames.size();

if (mergeMeshes) {
meshOffs += *meshCount;
Expand Down Expand Up @@ -371,7 +371,7 @@ void dumpSceneToDot(const char* fileName, const Scene& scene, int* visited)
std::string extra = "";
if (scene.nameForNode.contains(i)) {
int strID = scene.nameForNode.at(i);
name = scene.names[strID];
name = scene.nodeNames[strID];
}
if (visited) {
if (visited[i])
Expand Down
8 changes: 4 additions & 4 deletions shared/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Scene {
std::unordered_map<uint32_t, uint32_t> nameForNode;

// List of scene node names
std::vector<std::string> names;
std::vector<std::string> nodeNames;

// Debug list of material names
std::vector<std::string> materialNames;
Expand All @@ -66,13 +66,13 @@ int findNodeByName(const Scene& scene, const std::string& name);
inline std::string getNodeName(const Scene& scene, int node)
{
int strID = scene.nameForNode.contains(node) ? scene.nameForNode.at(node) : -1;
return (strID > -1) ? scene.names[strID] : std::string();
return (strID > -1) ? scene.nodeNames[strID] : std::string();
}

inline void setNodeName(Scene& scene, int node, const std::string& name)
{
uint32_t stringID = (uint32_t)scene.names.size();
scene.names.push_back(name);
uint32_t stringID = (uint32_t)scene.nodeNames.size();
scene.nodeNames.push_back(name);
scene.nameForNode[node] = stringID;
}

Expand Down
2 changes: 1 addition & 1 deletion shared/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename T> inline void mergeVectors(std::vector<T>& v1, const std::ve
// Delete a list of items from std::vector with indices in 'selection'
// e.g., eraseSelected({1, 2, 3, 4, 5}, {1, 3}) -> {1, 3, 5}
// ^ ^ 2 and 4 get deleted
template <class T, class Index = int> inline void eraseSelected(std::vector<T>& v, const std::vector<Index>& selection)
template <typename T, typename Index = int> inline void eraseSelected(std::vector<T>& v, const std::vector<Index>& selection)
{
// cut off the elements moved to the end of the vector by std::stable_partition
v.resize(std::distance(
Expand Down

0 comments on commit 7faf3f2

Please sign in to comment.