Skip to content

Commit

Permalink
Handle null mesh pointers in examples (#982)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Mar 22, 2024
1 parent 9f97f42 commit e187f52
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 92 deletions.
6 changes: 6 additions & 0 deletions examples/actor_animation/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ void buildScene(ScenePtr _scene, std::vector<VisualPtr> &_visuals,
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
//! [load mesh]
if (!descriptor.mesh)
{
gzerr << "Failed to load mesh: " << descriptor.meshName << std::endl;
// Do not launch example if actor mesh is not found.
return;
}

// add bvh animation
//! [add animation]
Expand Down
27 changes: 18 additions & 9 deletions examples/boundingbox_camera/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ VisualPtr createDuck(ScenePtr _scene,
n_ducks++;

// create a mesh
VisualPtr mesh = _scene->CreateVisual(
"duck" + std::to_string(n_ducks));
mesh->SetLocalPosition(_position);
mesh->SetLocalRotation(_rotation);
VisualPtr mesh;
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetMaterial(_material);
mesh->SetUserData("label", 5);
if (descriptor.mesh)
{
mesh = _scene->CreateVisual(
"duck" + std::to_string(n_ducks));
mesh->SetLocalPosition(_position);
mesh->SetLocalRotation(_rotation);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetMaterial(_material);
mesh->SetUserData("label", 5);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

return mesh;
}
Expand Down Expand Up @@ -168,7 +176,8 @@ void buildScene(ScenePtr _scene, BoundingBoxType _type)

// create a mesh
auto duck = createDuck(_scene, math::Vector3d(5, 0, 0), skyBlue);
root->AddChild(duck);
if (duck)
root->AddChild(duck);

// create a sphere1
auto sphere1 = createSphere(_scene, math::Vector3d(3, -1.5, 0), green);
Expand Down
21 changes: 14 additions & 7 deletions examples/depth_camera/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,24 @@ void buildScene(ScenePtr _scene)
root->AddChild(plane);

// create a mesh
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetUserData("label", 5);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetUserData("label", 5);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

// create a box
VisualPtr box = _scene->CreateVisual("box");
Expand Down
23 changes: 15 additions & 8 deletions examples/global_illumination/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,25 @@ void buildScene(ScenePtr _scene)
matPBR->SetEnvironmentMap(environmentMap);

// create mesh for PBR
VisualPtr meshPBR = _scene->CreateVisual("pump");
meshPBR->SetLocalPosition(2, 0.0, -0.3);
meshPBR->SetLocalRotation(0, 0, 0);
MeshDescriptor descriptorPBR;
descriptorPBR.meshName = common::joinPaths(RESOURCE_PATH, "pump.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptorPBR.mesh = meshManager->Load(descriptorPBR.meshName);
MeshPtr meshPBRGeom = _scene->CreateMesh(descriptorPBR);
meshPBRGeom->SetMaterial(matPBR);
meshPBR->AddGeometry(meshPBRGeom);
meshPBR->SetStatic(true);
root->AddChild(meshPBR);
if (descriptorPBR.mesh)
{
VisualPtr meshPBR = _scene->CreateVisual("pump");
meshPBR->SetLocalPosition(2, 0.0, -0.3);
meshPBR->SetLocalRotation(0, 0, 0);
MeshPtr meshPBRGeom = _scene->CreateMesh(descriptorPBR);
meshPBRGeom->SetMaterial(matPBR);
meshPBR->AddGeometry(meshPBRGeom);
meshPBR->SetStatic(true);
root->AddChild(meshPBR);
}
else
{
gzerr << "Failed load mesh: " << descriptorPBR.meshName << std::endl;
}

// create green material
MaterialPtr green = _scene->CreateMaterial();
Expand Down
24 changes: 15 additions & 9 deletions examples/lux_core_engine/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,25 @@ void buildScene(ScenePtr _scene)
box2->SetMaterial(boxMaterial2);

// Duck Scene
MaterialPtr matte = _scene->CreateMaterial();
matte->SetDiffuse(1.0, 0.0, 0.0);

VisualPtr mesh = _scene->CreateVisual();
MeshDescriptor descriptor;
descriptor.meshName = "media/duck.dae";
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetLocalRotation(GZ_PI / 2, 0, -GZ_PI / 4);
mesh->SetLocalPosition(-0.25, -1.25, 1.25);
mesh->SetMaterial(matte);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetLocalRotation(GZ_PI / 2, 0, -GZ_PI / 4);
mesh->SetLocalPosition(-0.25, -1.25, 1.25);
MaterialPtr matte = _scene->CreateMaterial();
matte->SetDiffuse(1.0, 0.0, 0.0);
mesh->SetMaterial(matte);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}
}

//////////////////////////////////////////////////
Expand Down
38 changes: 26 additions & 12 deletions examples/mesh_viewer/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,41 @@ void buildScene(ScenePtr _scene)
root->AddChild(light0);

//! [create a mesh]
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}
//! [create a mesh]

// create a pbr glb mesh
mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 2, 0);
mesh->SetLocalRotation(0, 0, 0);
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "AmbulanceStretcher.glb");
descriptor.mesh = meshManager->Load(descriptor.meshName);
meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 2, 0);
mesh->SetLocalRotation(0, 0, 0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

// create gray material
MaterialPtr gray = _scene->CreateMaterial();
Expand Down
21 changes: 14 additions & 7 deletions examples/ogre2_demo/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,24 @@ void buildScene(ScenePtr _scene)
matPBR->SetEnvironmentMap(environmentMap);

// create mesh for PBR
VisualPtr meshPBR = _scene->CreateVisual("pump");
meshPBR->SetLocalPosition(2, 0.0, -0.3);
meshPBR->SetLocalRotation(0, 0, 0);
MeshDescriptor descriptorPBR;
descriptorPBR.meshName = common::joinPaths(RESOURCE_PATH, "pump.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptorPBR.mesh = meshManager->Load(descriptorPBR.meshName);
MeshPtr meshPBRGeom = _scene->CreateMesh(descriptorPBR);
meshPBRGeom->SetMaterial(matPBR);
meshPBR->AddGeometry(meshPBRGeom);
root->AddChild(meshPBR);
if (descriptorPBR.mesh)
{
VisualPtr meshPBR = _scene->CreateVisual("pump");
meshPBR->SetLocalPosition(2, 0.0, -0.3);
meshPBR->SetLocalRotation(0, 0, 0);
MeshPtr meshPBRGeom = _scene->CreateMesh(descriptorPBR);
meshPBRGeom->SetMaterial(matPBR);
meshPBR->AddGeometry(meshPBRGeom);
root->AddChild(meshPBR);
}
else
{
gzerr << "Failed load mesh: " << descriptorPBR.meshName << std::endl;
}

// create green material
MaterialPtr green = _scene->CreateMaterial();
Expand Down
19 changes: 13 additions & 6 deletions examples/particles_demo/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,23 @@ void buildScene(ScenePtr _scene)
root->AddChild(light0);

//! [create a mesh]
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}
//! [create a mesh]

// create gray material
Expand Down
21 changes: 14 additions & 7 deletions examples/segmentation_camera/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,24 @@ void buildScene(ScenePtr _scene)
root->AddChild(plane);

// create a mesh
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetUserData("label", 5);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetUserData("label", 5);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

// create a box
VisualPtr box = _scene->CreateVisual("box");
Expand Down
23 changes: 15 additions & 8 deletions examples/thermal_camera/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ void buildScene(ScenePtr _scene)
root->AddChild(plane);

// create a mesh
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
float temperature = 315.0;
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
float temperature = 315.0;
mesh->SetUserData("temperature", temperature);
root->AddChild(mesh);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
mesh->SetUserData("temperature", temperature);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

// create a box
VisualPtr box = _scene->CreateVisual("box");
Expand Down
31 changes: 19 additions & 12 deletions examples/waves/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ void buildScene(ScenePtr _scene,
shader->SetVertexShader(vertexShaderPath);
shader->SetFragmentShader(fragmentShaderPath);

// create waves visual
VisualPtr waves = _scene->CreateVisual("waves");
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "mesh.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
waves->AddGeometry(meshGeom);
waves->SetLocalPosition(3, 0, 0);
waves->SetLocalScale(1, 1, 1);
waves->SetMaterial(shader);
root->AddChild(waves);
// create waves visual
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "mesh.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
if (descriptor.mesh)
{
VisualPtr waves = _scene->CreateVisual("waves");
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
waves->AddGeometry(meshGeom);
waves->SetLocalPosition(3, 0, 0);
waves->SetLocalScale(1, 1, 1);
waves->SetMaterial(shader);
root->AddChild(waves);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

// create camera
CameraPtr camera = _scene->CreateCamera("camera");
Expand Down
1 change: 0 additions & 1 deletion ogre/src/OgreMeshFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <gz/common/Console.hh>
#include <gz/common/Material.hh>
#include <gz/common/MeshManager.hh>
#include <gz/common/Skeleton.hh>
#include <gz/common/SkeletonAnimation.hh>
#include <gz/common/SubMesh.hh>
Expand Down
3 changes: 0 additions & 3 deletions ogre2/src/Ogre2Marker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@

#include <gz/common/Console.hh>

#include <gz/common/Mesh.hh>
#include <gz/common/MeshManager.hh>

#include "gz/rendering/ogre2/Ogre2Capsule.hh"
#include "gz/rendering/ogre2/Ogre2Conversions.hh"
#include "gz/rendering/ogre2/Ogre2DynamicRenderable.hh"
Expand Down
Loading

0 comments on commit e187f52

Please sign in to comment.