Skip to content

Commit

Permalink
GRAPHICS: Add MaterialManager::setColorModifier()
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 20, 2014
1 parent 0aaf179 commit 7bb4a8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/graphics/materialman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,35 @@ void MaterialManager::create(const MaterialDeclaration &decl, Ogre::MaterialPtr
material->setReceiveShadows(decl.receiveShadows);
}

void MaterialManager::setColorModifier(const Ogre::MaterialPtr &material, float r, float g, float b, float a) {
Ogre::TextureUnitState *texState = getColorModifier(material);
if (!texState)
texState = addColorModifier(material);

const Ogre::ColourValue color(r, g, b);

texState->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_CURRENT, Ogre::LBS_MANUAL, color, color, 1.0);
texState->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_CURRENT, Ogre::LBS_MANUAL, a, a, 1.0);
}

Ogre::TextureUnitState *MaterialManager::getColorModifier(const Ogre::MaterialPtr &material) {
uint count = material->getTechnique(0)->getPass(0)->getNumTextureUnitStates();
for (uint i = 0; i < count; i++) {
Ogre::TextureUnitState *texState = material->getTechnique(0)->getPass(0)->getTextureUnitState(i);
if (texState->getName() == "colormodifier")
return texState;
}

return 0;
}

Ogre::TextureUnitState *MaterialManager::addColorModifier(const Ogre::MaterialPtr &material) {
Ogre::TextureUnitState *texState = material->getTechnique(0)->getPass(0)->createTextureUnitState();
texState->setName("colormodifier");

return texState;
}

static Common::UString concat(const std::vector<Common::UString> &str) {
Common::UString c;

Expand Down
6 changes: 6 additions & 0 deletions src/graphics/materialman.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,18 @@ class MaterialManager : public Common::Singleton<MaterialManager> {
/** Get a default material with a solid color. */
Ogre::MaterialPtr getSolidColor(float r, float g, float b, float a = 1.0, bool dynamic = false);

/** Change the material's color modifier. */
void setColorModifier(const Ogre::MaterialPtr &material, float r, float g, float b, float a = 1.0);

private:
void create(const MaterialDeclaration &decl, Ogre::MaterialPtr material);
void createSolidColor(const MaterialDeclaration &decl, Ogre::MaterialPtr material);

Common::UString canonicalName(const MaterialDeclaration &decl);
Common::UString dynamicName();

Ogre::TextureUnitState *getColorModifier(const Ogre::MaterialPtr &material);
Ogre::TextureUnitState *addColorModifier(const Ogre::MaterialPtr &material);
};

} // End of namespace Graphics
Expand Down

0 comments on commit 7bb4a8a

Please sign in to comment.