Only attempt to load the obj file if the obj_filename is defined in json

Signed-off by: Arthur Brainville (Ybalrid) <ybalrid@ybalrid.info>
This commit is contained in:
Arthur Brainville (Ybalrid) 2018-02-19 13:16:40 +01:00 committed by Arthur Brainville
parent 365e56b779
commit 62da4688af
No known key found for this signature in database
GPG Key ID: BC05C4812A06BCF3
2 changed files with 40 additions and 30 deletions

View File

@ -736,33 +736,35 @@ int main(int argc, char **argv) {
default_material.specular[1] = 0;
default_material.specular[2] = 0;
bool ret = LoadObj(gRenderConfig.obj_filename, gRenderConfig.scene_scale,
&meshes, &materials, &textures);
if (!ret) {
std::cerr << "Failed to load .obj [ " << gRenderConfig.obj_filename
<< " ]" << std::endl;
return -1;
if (!gRenderConfig.obj_filename.empty()) {
bool ret = LoadObj(gRenderConfig.obj_filename, gRenderConfig.scene_scale,
&meshes, &materials, &textures);
if (!ret) {
std::cerr << "Failed to load .obj [ " << gRenderConfig.obj_filename
<< " ]" << std::endl;
return -1;
}
gAsset.materials = materials;
gAsset.default_material = default_material;
gAsset.textures = textures;
for (size_t n = 0; n < meshes.size(); n++) {
size_t mesh_id = gAsset.meshes.size();
gAsset.meshes.push_back(meshes[mesh_id]);
}
for (size_t n = 0; n < gAsset.meshes.size(); n++) {
nanosg::Node<float, example::Mesh<float> > node(&gAsset.meshes[n]);
node.SetName(meshes[n].name);
node.SetLocalXform(
meshes[n].pivot_xform); // Use mesh's pivot transform
// as node's local transform.
gNodes.push_back(node);
gScene.AddNode(node);
}
}
gAsset.materials = materials;
gAsset.default_material = default_material;
gAsset.textures = textures;
for (size_t n = 0; n < meshes.size(); n++) {
size_t mesh_id = gAsset.meshes.size();
gAsset.meshes.push_back(meshes[mesh_id]);
}
for (size_t n = 0; n < gAsset.meshes.size(); n++) {
nanosg::Node<float, example::Mesh<float> > node(&gAsset.meshes[n]);
node.SetName(meshes[n].name);
node.SetLocalXform(meshes[n].pivot_xform); // Use mesh's pivot transform
// as node's local transform.
gNodes.push_back(node);
gScene.AddNode(node);
}
if (!gScene.Commit()) {
std::cerr << "Failed to commit the scene." << std::endl;
return -1;
@ -952,13 +954,15 @@ int main(int argc, char **argv) {
// scene graph tree
glm::mat4 node_matrix;
static int node_selected = 0;
static int node_selected_index = 0;
{
ImGui::Begin("Scene");
ImGui::ListBox("Node list", &node_selected, imgui_node_names.data(),
ImGui::ListBox("Node list", &node_selected_index, imgui_node_names.data(),
imgui_node_names.size(), 16);
node_matrix = glm::make_mat4(node_map[node_selected]->GetLocalXformPtr());
auto node_selected = node_map.at(node_selected_index);
node_matrix = glm::make_mat4(node_selected->GetLocalXformPtr());
ImGui::End();
}
@ -1000,7 +1004,7 @@ int main(int argc, char **argv) {
float mat[4][4];
memcpy(mat, &node_matrix[0][0], sizeof(float) * 16);
node_map[node_selected]->SetLocalXform(mat);
node_map[node_selected_index]->SetLocalXform(mat);
checkErrors("edit_transform");

View File

@ -682,6 +682,12 @@ class Scene {
/// Commit the scene. Must be called before tracing rays into the scene.
///
bool Commit() {
// the scene should contains something
if (nodes_.size() == 0) {
std::cerr << "You are attempting to commit an empty scene!\n";
return false;
}
// Update nodes.
for (size_t i = 0; i < nodes_.size(); i++) {
T ident[4][4];