Skip to content

Commit

Permalink
Add ogre2 skybox support (#168)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Nov 5, 2020
1 parent d18dd1c commit e452c39
Show file tree
Hide file tree
Showing 24 changed files with 890 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Ignition Rendering

### Ignition Rendering 5.X

### Ignition Rendering 5.X.X

1. Add ogre2 skybox support
* [Pull request #168](https://github.com/ignitionrobotics/ign-rendering/pull/168)

### Ignition Rendering 4.X

### Ignition Rendering 4.X.X
Expand Down
9 changes: 9 additions & 0 deletions examples/ogre2_demo/GlutWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ void keyboardCB(unsigned char _key, int, int)
}
}
}
else if (_key == 's')
{
// toggle sky
for (ir::CameraPtr camera : g_cameras)
{
camera->Scene()->SetSkyEnabled(!camera->Scene()->SkyEnabled());
}
}
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -345,6 +353,7 @@ void printUsage()
std::cout << " TAB - Switch render engines " << std::endl;
std::cout << " ESC - Exit " << std::endl;
std::cout << " P - Toggle render pass " << std::endl;
std::cout << " S - Toggle skybox " << std::endl;
std::cout << "===============================" << std::endl;
}

Expand Down
26 changes: 23 additions & 3 deletions examples/ogre2_demo/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ void buildScene(ScenePtr _scene)
{
// initialize _scene
_scene->SetAmbientLight(0.2, 0.2, 0.2);
_scene->SetBackgroundColor(0.0, 0.0, 0.0);
_scene->SetBackgroundColor(0.2, 0.2, 0.2);
VisualPtr root = _scene->RootVisual();

// enable sky
_scene->SetSkyEnabled(true);

// create PBR material
MaterialPtr matPBR = _scene->CreateMaterial();
std::string textureMap = common::joinPaths(RESOURCE_PATH, "pump_albedo.png");
Expand All @@ -64,6 +67,8 @@ void buildScene(ScenePtr _scene)
matPBR->SetNormalMap(normalMap);
matPBR->SetRoughnessMap(roughnessMap);
matPBR->SetMetalnessMap(metalnessMap);
matPBR->SetMetalness(0.7);
matPBR->SetRoughness(0.3);
matPBR->SetEnvironmentMap(environmentMap);

// create mesh for PBR
Expand Down Expand Up @@ -130,8 +135,12 @@ void buildScene(ScenePtr _scene)

// create mirror material
MaterialPtr mirrorMat = _scene->CreateMaterial();
mirrorMat->SetRoughness(0.01);
mirrorMat->SetEnvironmentMap(environmentMap);
mirrorMat->SetDiffuse(1.0, 1.0, 1.0);
mirrorMat->SetRoughness(0.1);
mirrorMat->SetMetalness(0.9);
std::string skyEnvironmentMap =
common::joinPaths(RESOURCE_PATH, "skybox_lowres.dds");
mirrorMat->SetEnvironmentMap(skyEnvironmentMap);

// create box visual
VisualPtr box = _scene->CreateVisual("box");
Expand Down Expand Up @@ -189,6 +198,10 @@ void buildScene(ScenePtr _scene)
light1->SetSpecularColor(0.2, 0.2, 0.2);
light1->SetLocalPosition(0, 3, 3);
light1->SetDirection(1, -1, -1);
light1->SetAttenuationConstant(0.1);
light1->SetAttenuationLinear(0.001);
light1->SetAttenuationQuadratic(0.0001);
light1->SetFalloff(0.8);
light1->SetCastShadows(true);
root->AddChild(light1);

Expand All @@ -197,6 +210,9 @@ void buildScene(ScenePtr _scene)
light2->SetDiffuseColor(0.2, 0.4, 0.8);
light2->SetSpecularColor(0.2, 0.2, 0.2);
light2->SetLocalPosition(3, 0, 2);
light2->SetAttenuationConstant(0.1);
light2->SetAttenuationLinear(0.001);
light2->SetAttenuationQuadratic(0.0001);
light2->SetCastShadows(true);
root->AddChild(light2);

Expand All @@ -206,6 +222,10 @@ void buildScene(ScenePtr _scene)
light3->SetSpecularColor(0.2, 0.2, 0.2);
light3->SetLocalPosition(0, -3, 3);
light3->SetDirection(1, 1, -1);
light3->SetAttenuationConstant(0.1);
light3->SetAttenuationLinear(0.001);
light3->SetAttenuationQuadratic(0.0001);
light3->SetFalloff(0.8);
light3->SetCastShadows(false);
root->AddChild(light3);

Expand Down
Binary file added examples/ogre2_demo/media/skybox_lowres.dds
Binary file not shown.
18 changes: 18 additions & 0 deletions include/ignition/rendering/Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ namespace ignition
/// const std::array<math::Color, 4> &_colors)
public: virtual void RemoveGradientBackgroundColor() = 0;

/// \brief Get the scene background material
/// e.g. a material with skybox cubemap texture
/// \return Material of the background
public: virtual MaterialPtr BackgroundMaterial() const = 0;

/// \brief Set the scene background material
/// e.g. a material with skybox cubemap texture
/// \param[in] _material Material to set the background to
public: virtual void SetBackgroundMaterial(MaterialPtr _material) = 0;

/// \brief Get the number of nodes managed by this scene. Note these
/// nodes may not be directly or indirectly attached to the root node.
/// \return The number of nodes managed by this scene
Expand Down Expand Up @@ -954,6 +964,14 @@ namespace ignition
public: virtual ParticleEmitterPtr CreateParticleEmitter(
unsigned int _id, const std::string &_name) = 0;

/// \brief Enable sky in the scene.
/// \param[in] _enabled True to enable sky
public: virtual void SetSkyEnabled(bool _enabled) = 0;

/// \brief Get whether the sky is enabled in the scene.
/// \return true to sky is enabled, false otherwise
public: virtual bool SkyEnabled() const = 0;

/// \brief Prepare scene for rendering. The scene will flushing any scene
/// changes by traversing scene-graph, calling PreRender on all objects
public: virtual void PreRender() = 0;
Expand Down
16 changes: 16 additions & 0 deletions include/ignition/rendering/base/BaseScene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ namespace ignition
// Documentation inherited.
public: virtual void RemoveGradientBackgroundColor() override;

// Documentation inherited.
public: virtual MaterialPtr BackgroundMaterial() const override;

// Documentation inherited.
public: virtual void SetBackgroundMaterial(MaterialPtr _material)
override;

public: virtual unsigned int NodeCount() const override;

public: virtual bool HasNode(ConstNodePtr _node) const override;
Expand Down Expand Up @@ -432,6 +439,12 @@ namespace ignition
public: virtual ParticleEmitterPtr CreateParticleEmitter(
unsigned int _id, const std::string &_name) override;

// Documentation inherited.
public: virtual void SetSkyEnabled(bool _enabled) override;

// Documentation inherited.
public: virtual bool SkyEnabled() const override;

public: virtual void PreRender() override;

public: virtual void Clear() override;
Expand Down Expand Up @@ -641,6 +654,9 @@ namespace ignition
/// \brief Whether the scene has a gradient background.
protected: bool isGradientBackgroundColor = false;

/// \brief Scene background material.
protected: MaterialPtr backgroundMaterial;

private: unsigned int nextObjectId;

private: NodeStorePtr nodes;
Expand Down
8 changes: 8 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2Camera.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ namespace ignition

public: virtual void SetBackgroundColor(const math::Color &_color);

/// \brief Get the background material of this camera
/// \return Background material of this camera
public: virtual MaterialPtr BackgroundMaterial() const;

/// \brief Set the background material of this camera
/// \param[in] _material Material to set the background to
public: virtual void SetBackgroundMaterial(MaterialPtr _material);

// Documentation inherited.
public: virtual void Render() override;

Expand Down
18 changes: 18 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2RenderTarget.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ namespace ignition
/// \param[in] _color Color to set the background to
public: virtual void SetBackgroundColor(math::Color _color);

/// \brief Set the background material of this camera
/// \param[in] _material Material to set the background to
public: virtual void SetBackgroundMaterial(MaterialPtr _material);

/// \brief Get the background material of this camera
/// \return background material
public: virtual MaterialPtr BackgroundMaterial() const;

// Documentation inherited
public: virtual void PreRender() override;

Expand Down Expand Up @@ -112,6 +120,9 @@ namespace ignition
/// \brief Update the background color
protected: virtual void UpdateBackgroundColor();

/// \brief Update the background material
protected: virtual void UpdateBackgroundMaterial();

/// \brief Update the render pass chain
protected: virtual void UpdateRenderPassChain();

Expand Down Expand Up @@ -171,6 +182,9 @@ namespace ignition
/// \brief Stores the background color of the render target
protected: Ogre::ColourValue ogreBackgroundColor;

/// \brief Background material of the render target
protected: MaterialPtr backgroundMaterial;

/// \brief a material used by for the render target
protected: MaterialPtr material;

Expand All @@ -180,6 +194,10 @@ namespace ignition
/// \brief Flag to indicate if the render target color has changed
protected: bool colorDirty = true;

/// \brief Flag to indicate if the render target background material has
/// changed
protected: bool backgroundMaterialDirty = false;

/// \brief Anti-aliasing level
protected: unsigned int antiAliasing = 4;

Expand Down
6 changes: 6 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ namespace ignition
// Documentation inherited
public: virtual void Destroy() override;

// Documentation inherited
public: virtual void SetSkyEnabled(bool _enabled) override;

// Documentation inherited
public: virtual bool SkyEnabled() const override;

/// \brief Get a pointer to the ogre scene manager
/// \return Pointer to the ogre scene manager
public: virtual Ogre::SceneManager *OgreSceneManager() const;
Expand Down
12 changes: 12 additions & 0 deletions ogre2/src/Ogre2Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ void Ogre2Camera::SetBackgroundColor(const math::Color &_color)
this->renderTexture->SetBackgroundColor(_color);
}

//////////////////////////////////////////////////
MaterialPtr Ogre2Camera::BackgroundMaterial() const
{
return this->renderTexture->BackgroundMaterial();
}

//////////////////////////////////////////////////
void Ogre2Camera::SetBackgroundMaterial(MaterialPtr _material)
{
this->renderTexture->SetBackgroundMaterial(_material);
}

//////////////////////////////////////////////////
void Ogre2Camera::SetMaterial(const MaterialPtr &_material)
{
Expand Down
49 changes: 48 additions & 1 deletion ogre2/src/Ogre2DepthCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class ignition::rendering::Ogre2DepthCameraPrivate
public: ignition::common::EventT<void(const float *,
unsigned int, unsigned int, unsigned int,
const std::string &)> newDepthFrame;

/// \brief Name of sky box material
public: const std::string kSkyboxMaterialName = "SkyBox";
};

using namespace ignition;
Expand Down Expand Up @@ -283,6 +286,33 @@ void Ogre2DepthCamera::CreateDepthTexture()
psParams->setNamedConstant("tolerance",
static_cast<float>(1e-6));

// create background material is specified
MaterialPtr backgroundMaterial = this->Scene()->BackgroundMaterial();
bool validBackground = backgroundMaterial &&
!backgroundMaterial->EnvironmentMap().empty();

if (validBackground)
{
Ogre::MaterialManager &matManager = Ogre::MaterialManager::getSingleton();
std::string skyMatName = this->dataPtr->kSkyboxMaterialName + "_"
+ this->Name();
auto mat = matManager.getByName(skyMatName);
if (!mat)
{
auto skyboxMat = matManager.getByName(this->dataPtr->kSkyboxMaterialName);
if (!skyboxMat)
{
ignerr << "Unable to find skybox material" << std::endl;
return;
}
mat = skyboxMat->clone(skyMatName);
}
Ogre::TextureUnitState *texUnit =
mat->getTechnique(0u)->getPass(0u)->getTextureUnitState(0u);
texUnit->setTextureName(backgroundMaterial->EnvironmentMap(),
Ogre::TEX_TYPE_CUBE_MAP);
}

// Create depth camera compositor
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Expand Down Expand Up @@ -379,14 +409,31 @@ void Ogre2DepthCamera::CreateDepthTexture()
nodeDef->setNumTargetPass(2);
Ogre::CompositorTargetDef *colorTargetDef =
nodeDef->addTargetPass("colorTexture");
colorTargetDef->setNumPasses(2);

if (validBackground)
colorTargetDef->setNumPasses(3);
else
colorTargetDef->setNumPasses(2);
{
// clear pass
Ogre::CompositorPassClearDef *passClear =
static_cast<Ogre::CompositorPassClearDef *>(
colorTargetDef->addPass(Ogre::PASS_CLEAR));
passClear->mColourValue = Ogre::ColourValue(
Ogre2Conversions::Convert(this->Scene()->BackgroundColor()));

if (validBackground)
{
// quad pass
Ogre::CompositorPassQuadDef *passQuad =
static_cast<Ogre::CompositorPassQuadDef *>(
colorTargetDef->addPass(Ogre::PASS_QUAD));
passQuad->mMaterialName = this->dataPtr->kSkyboxMaterialName + "_"
+ this->Name();
passQuad->mFrustumCorners =
Ogre::CompositorPassQuadDef::CAMERA_DIRECTION;
}

// scene pass
Ogre::CompositorPassSceneDef *passScene =
static_cast<Ogre::CompositorPassSceneDef *>(
Expand Down
2 changes: 2 additions & 0 deletions ogre2/src/Ogre2RenderEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ void Ogre2RenderEngine::CreateResources()
std::make_pair(p + "/materials/programs", "General"));
archNames.push_back(
std::make_pair(p + "/materials/scripts", "General"));
archNames.push_back(
std::make_pair(p + "/materials/textures", "General"));

for (auto aiter = archNames.begin(); aiter != archNames.end(); ++aiter)
{
Expand Down
Loading

0 comments on commit e452c39

Please sign in to comment.