Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lightmaps in ogre2 #182

Merged
merged 5 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR})

#--------------------------------------
# Find ignition-common
ign_find_package(ignition-common3 REQUIRED COMPONENTS graphics events)
ign_find_package(ignition-common3 REQUIRED
COMPONENTS graphics events
VERSION 3.9)
set(IGN_COMMON_VER ${ignition-common3_VERSION_MAJOR})

#--------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions include/ignition/rendering/Material.hh
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ namespace ignition
/// \brief Removes any emissive map mapped to this material
public: virtual void ClearEmissiveMap() = 0;

/// \brief Determine if this material has a light map
/// \return True if this material has a light map
public: virtual bool HasLightMap() const = 0;

/// \brief Get the URI of the light map file
/// \return URI of the light map file
public: virtual std::string LightMap() const = 0;

/// \brief Get the texture coordinate set used by lightmap
/// \return texture coordinate set of the light map
public: virtual unsigned int LightMapTexCoordSet() const = 0;

/// \brief Set the material light map
/// \param[in] _name URI of the new light map file
/// \param[in] _uvSet Texture coordinate set to use
public: virtual void SetLightMap(const std::string &_name,
unsigned int _uvSet = 0u) = 0;

/// \brief Removes any light map mapped to this material
public: virtual void ClearLightMap() = 0;

/// \brief Set the roughness value. Only affects material of type MT_PBS
/// \param[in] _roughness Roughness to set to
public: virtual void SetRoughness(const float _roughness) = 0;
Expand Down
54 changes: 54 additions & 0 deletions include/ignition/rendering/base/BaseMaterial.hh
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ namespace ignition
// Documentation inherited
public: virtual void ClearEmissiveMap() override;

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

// Documentation inherited
public: virtual std::string LightMap() const override;

// Documentation inherited
public: virtual unsigned int LightMapTexCoordSet() const override;

// Documentation inherited
public: virtual void SetLightMap(const std::string &_lightMap,
unsigned int uvSet = 0u) override;

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

// Documentation inherited
public: virtual void SetRoughness(const float _roughness) override;

Expand Down Expand Up @@ -821,6 +837,41 @@ namespace ignition
// no op
}

//////////////////////////////////////////////////
template <class T>
bool BaseMaterial<T>::HasLightMap() const
{
return false;
}

//////////////////////////////////////////////////
template <class T>
std::string BaseMaterial<T>::LightMap() const
{
return std::string();
}

//////////////////////////////////////////////////
template <class T>
unsigned int BaseMaterial<T>::LightMapTexCoordSet() const
{
return 0u;
}

//////////////////////////////////////////////////
template <class T>
void BaseMaterial<T>::SetLightMap(const std::string &, unsigned int)
{
// no op
}

//////////////////////////////////////////////////
template <class T>
void BaseMaterial<T>::ClearLightMap()
{
// no op
}

//////////////////////////////////////////////////
template <class T>
void BaseMaterial<T>::SetRoughness(const float)
Expand Down Expand Up @@ -891,6 +942,8 @@ namespace ignition
this->SetMetalness(_material->Metalness());
this->SetEnvironmentMap(_material->EnvironmentMap());
this->SetEmissiveMap(_material->EmissiveMap());
this->SetLightMap(_material->LightMap(),
_material->LightMapTexCoordSet());
this->SetShaderType(_material->ShaderType());
this->SetVertexShader(_material->VertexShader());
this->SetFragmentShader(_material->FragmentShader());
Expand Down Expand Up @@ -933,6 +986,7 @@ namespace ignition
this->SetMetalness(pbrMat->Metalness());
this->SetEnvironmentMap(pbrMat->EnvironmentMap());
this->SetEmissiveMap(pbrMat->EmissiveMap());
this->SetLightMap(pbrMat->LightMap(), pbrMat->LightMapTexCoordSet());
}

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

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

// Documentation inherited
public: virtual std::string LightMap() const override;

// Documentation inherited
public: virtual unsigned int LightMapTexCoordSet() const override;

// Documentation inherited
public: virtual void SetLightMap(const std::string &_name,
unsigned int _uvSet = 0u) override;

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

// Documentation inherited
public: virtual float Roughness() const override;

Expand Down Expand Up @@ -247,6 +263,12 @@ namespace ignition
/// \brief Name of the emissive map
protected: std::string emissiveMapName;

/// \brief Name of the light map
protected: std::string lightMapName;

/// \brief Texture coorindate set used by the light map
protected: unsigned int lightMapUvSet = 0u;

/// \brief Unique id assigned to ogre hlms datablock
protected: std::string ogreDatablockId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef IGNITION_RENDERING_OGRE2_OGRE2PARTICLEEMITTER_HH_
#define IGNITION_RENDERING_OGRE2_OGRE2PARTICLEEMITTER_HH_

#include <memory>
#include <string>
#include "ignition/rendering/base/BaseParticleEmitter.hh"
#include "ignition/rendering/ogre2/Ogre2Visual.hh"
Expand Down
65 changes: 63 additions & 2 deletions ogre2/src/Ogre2Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ void Ogre2Material::SetAlphaFromTexture(bool _enabled,
double _alpha, bool _twoSided)
{
BaseMaterial::SetAlphaFromTexture(_enabled, _alpha, _twoSided);
Ogre::HlmsBlendblock block;
if (_enabled)
{
Ogre::HlmsBlendblock block;
block.setBlendType(Ogre::SBT_TRANSPARENT_ALPHA);
this->ogreDatablock->setAlphaTest(Ogre::CMPF_GREATER_EQUAL);
this->ogreDatablock->setBlendblock(block);
this->ogreDatablock->setTwoSidedLighting(_twoSided);
}
else
{
this->ogreDatablock->setAlphaTest(Ogre::CMPF_ALWAYS_PASS);
this->ogreDatablock->setBlendblock(block);
}
this->ogreDatablock->setAlphaTestThreshold(_alpha);
this->ogreDatablock->setTwoSidedLighting(_twoSided);
Expand Down Expand Up @@ -371,6 +371,56 @@ void Ogre2Material::ClearEmissiveMap()
this->ogreDatablock->setTexture(Ogre::PBSM_EMISSIVE, 0, Ogre::TexturePtr());
}

//////////////////////////////////////////////////
bool Ogre2Material::HasLightMap() const
{
return !this->lightMapName.empty();
}

//////////////////////////////////////////////////
std::string Ogre2Material::LightMap() const
{
return this->lightMapName;
}

//////////////////////////////////////////////////
unsigned int Ogre2Material::LightMapTexCoordSet() const
{
return this->lightMapUvSet;
}

//////////////////////////////////////////////////
void Ogre2Material::SetLightMap(const std::string &_name, unsigned int _uvSet)
{
if (_name.empty())
{
this->ClearLightMap();
return;
}

this->lightMapName = _name;
this->lightMapUvSet = _uvSet;

// reserve detail map 0 for light map
Ogre::PbsTextureTypes type = Ogre::PBSM_DETAIL0;

// lightmap usually uses a different tex coord set
this->SetTextureMapImpl(this->lightMapName, type);

this->ogreDatablock->setTextureUvSource(type, this->lightMapUvSet);

// PBSM_BLEND_OVERLAY and PBSM_BLEND_MULTIPLY2X produces better results
this->ogreDatablock->setDetailMapBlendMode(0, Ogre::PBSM_BLEND_OVERLAY);
}

//////////////////////////////////////////////////
void Ogre2Material::ClearLightMap()
{
this->lightMapName = "";
this->lightMapUvSet = 0u;
this->ogreDatablock->setTexture(Ogre::PBSM_DETAIL0, 0, Ogre::TexturePtr());
}

//////////////////////////////////////////////////
void Ogre2Material::SetRoughness(const float _roughness)
{
Expand Down Expand Up @@ -455,6 +505,17 @@ void Ogre2Material::SetTextureMapImpl(const std::string &_texture,

this->ogreDatablock->setTexture(_type, texLocation.xIdx, texLocation.texture,
&samplerBlockRef);

// disable alpha from texture if texture does not have an alpha channel
// otherwise this becomes a transparent material
if (_type == Ogre::PBSM_DIFFUSE)
{
if (this->TextureAlphaEnabled() && !texLocation.texture->hasAlpha())
{
this->SetAlphaFromTexture(false, this->AlphaThreshold(),
this->TwoSidedEnabled());
}
}
}

//////////////////////////////////////////////////
Expand Down
23 changes: 16 additions & 7 deletions ogre2/src/Ogre2MeshFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,15 @@ bool Ogre2MeshFactory::LoadImpl(const MeshDescriptor &_desc)
// TODO(anyone): specular colors

// two dimensional texture coordinates
if (subMesh.TexCoordCount() > 0)
// add all texture coordinate sets
for (unsigned int k = 0u; k < subMesh.TexCoordSetCount(); ++k)
{
vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2,
Ogre::VES_TEXTURE_COORDINATES, 0);
currOffset += Ogre::v1::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
if (subMesh.TexCoordCountBySet(k) > 0)
{
vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2,
Ogre::VES_TEXTURE_COORDINATES, k);
currOffset += Ogre::v1::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
}
}

// allocate the vertex buffer
Expand Down Expand Up @@ -375,17 +379,22 @@ bool Ogre2MeshFactory::LoadImpl(const MeshDescriptor &_desc)
*vertices++ = subMesh.Vertex(j).Y();
*vertices++ = subMesh.Vertex(j).Z();

// Add all normals
if (subMesh.NormalCount() > 0)
{
*vertices++ = subMesh.Normal(j).X();
*vertices++ = subMesh.Normal(j).Y();
*vertices++ = subMesh.Normal(j).Z();
}

if (subMesh.TexCoordCount() > 0)
// Add all texture coordinate sets
for (unsigned int k = 0u; k < subMesh.TexCoordSetCount(); ++k)
{
*vertices++ = subMesh.TexCoord(j).X();
*vertices++ = subMesh.TexCoord(j).Y();
if (subMesh.TexCoordCountBySet(k) > 0u)
{
*vertices++ = subMesh.TexCoordBySet(j, k).X();
*vertices++ = subMesh.TexCoordBySet(j, k).Y();
}
}
}

Expand Down
Loading