Skip to content

Commit

Permalink
GRAPHICS: Add a MaterialManager for Ogre
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 21, 2014
1 parent 0bb1e6a commit 7e2e716
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/graphics/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ noinst_HEADERS = \
renderer.h \
yuv_to_rgb.h \
textureman.h \
materialman.h \
cursor.h \
cursorman.h \
$(EMPTY)
Expand All @@ -24,6 +25,7 @@ libgraphics_la_SOURCES = \
renderer.cpp \
yuv_to_rgb.cpp \
textureman.cpp \
materialman.cpp \
cursor.cpp \
cursorman.cpp \
$(EMPTY)
Expand Down
129 changes: 129 additions & 0 deletions src/graphics/materialman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/materialman.cpp
* A material manager.
*/


#include <OgreMaterialManager.h>
#include <OgreTechnique.h>
#include <OgrePass.h>

#include "common/ustring.h"
#include "common/error.h"
#include "common/threads.h"

#include "graphics/textureman.h"
#include "graphics/materialman.h"

#include "events/requests.h"

DECLARE_SINGLETON(Graphics::MaterialManager)

namespace Graphics {

MaterialManager::MaterialManager() {
}

MaterialManager::~MaterialManager() {
}

Ogre::MaterialPtr MaterialManager::get(const Common::UString &name) {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<Ogre::MaterialPtr> functor(boost::bind(&MaterialManager::get, this, name));

return RequestMan.callInMainThread(functor);
}

Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName(name.c_str());
if (!material.isNull())
return material;

try {
material = create(name);
} catch (Common::Exception &e) {
e.add("Failed to load material \"%s\"", name.c_str());
throw;
}

return material;
}

Ogre::MaterialPtr MaterialManager::create(const Common::UString &name) {
Ogre::MaterialPtr material((Ogre::Material *) 0);

try {
Ogre::TexturePtr texture = TextureMan.get(name);

material = Ogre::MaterialManager::getSingleton().create(name.c_str(), "General");

Ogre::TextureUnitState *texState = material->getTechnique(0)->getPass(0)->createTextureUnitState();

texState->setTexture(texture);
texState->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);

if (texture->hasAlpha())
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
else
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_REPLACE);

} catch (std::exception &e) {
throw Common::Exception("%s", e.what());
}

return material;
}

Ogre::MaterialPtr MaterialManager::getInvisible() {
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("xoreos-invisible");
if (!material.isNull())
return material;

material = Ogre::MaterialManager::getSingleton().create("xoreos/invisible", "General");

material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
material->getTechnique(0)->getPass(0)->setColourWriteEnabled(false);

return material;
}

Ogre::MaterialPtr MaterialManager::getBlack() {
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("xoreos-invisible");
if (!material.isNull())
return material;

material = Ogre::MaterialManager::getSingleton().create("xoreos/invisible", "General");

material->getTechnique(0)->getPass(0)->setAmbient(0.0, 0.0, 0.0);
material->getTechnique(0)->getPass(0)->setDiffuse(0.0, 0.0, 0.0, 1.0);
material->getTechnique(0)->getPass(0)->setSpecular(0.0, 0.0, 0.0, 1.0);
material->getTechnique(0)->getPass(0)->setShininess(0.0);
material->getTechnique(0)->getPass(0)->setSelfIllumination(0.0, 0.0, 0.0);

return material;
}

} // End of namespace Graphics
61 changes: 61 additions & 0 deletions src/graphics/materialman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/materialman.h
* A material manager.
*/

#ifndef GRAPHICS_MATERIALMAN_H
#define GRAPHICS_MATERIALMAN_H

#include <OgrePrerequisites.h>
#include <OgreMaterial.h>

#include "common/singleton.h"

namespace Graphics {

/** The global material manager. */
class MaterialManager : public Common::Singleton<MaterialManager> {
public:
MaterialManager();
~MaterialManager();

/** Get/Load a material with a single texture. */
Ogre::MaterialPtr get(const Common::UString &name);

Ogre::MaterialPtr getInvisible();
Ogre::MaterialPtr getBlack();

private:
Ogre::MaterialPtr create(const Common::UString &name);
};

} // End of namespace Graphics

/** Shortcut for accessing the material manager. */
#define MaterialMan Graphics::MaterialManager::instance()

#endif // GRAPHICS_MATERIALMAN_H
2 changes: 2 additions & 0 deletions src/xoreos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "graphics/graphics.h"
#include "graphics/cursorman.h"
#include "graphics/textureman.h"
#include "graphics/materialman.h"

#include "sound/sound.h"

Expand Down Expand Up @@ -268,6 +269,7 @@ void deinit() {
// Destroy global singletons
Graphics::CursorManager::destroy();
Graphics::TextureManager::destroy();
Graphics::MaterialManager::destroy();

Aurora::TalkManager::destroy();
Aurora::TwoDARegistry::destroy();
Expand Down

0 comments on commit 7e2e716

Please sign in to comment.