EntityUtil.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "O3dPre.hpp"
00023 #include "EntityUtil.hpp"
00024 #include <OgreEntity.h>
00025 #include <OgreSubEntity.h>
00026 #include <OgreMaterialManager.h>
00027 #include <OgreTechnique.h>
00028 #include "util/error/Log.hpp"
00029 
00030 namespace se_ogre {
00031 
00032     EntityMaterial
00033     ::EntityMaterial(Ogre::Entity* e, const char* name) {
00034         entity_ = e;
00035         subEntityCount_ = entity_->getNumSubEntities();
00036         originals_ = new Ogre::MaterialPtr[ subEntityCount_ ];
00037         temporaries_ = new Ogre::MaterialPtr[ subEntityCount_ ];
00038 
00039         // Assign a temprary material
00040         cloneEntityMaterials(name);
00041     }
00042 
00043 
00044     EntityMaterial
00045     ::~EntityMaterial() {
00046         reset();
00047         delete[] temporaries_;
00048         delete[] originals_;
00049     }
00050 
00051 
00052     void EntityMaterial
00053     ::reset() {
00054         // Iterate through subentities
00055         for(int i = 0; i < subEntityCount_; ++i) {
00056             // Reset to original material
00057             Ogre::SubEntity* se = entity_->getSubEntity(i);
00058             se->setMaterialName(originals_[i]->getName());
00059             // Delete temproary material
00060             if (!temporaries_[i].isNull ()) {
00061                 Ogre::MaterialManager::getSingleton().remove(temporaries_[i]->getName ());
00062                 temporaries_[i].setNull();
00063             }
00064         }
00065     }
00066 
00067 
00068     void EntityMaterial
00069     ::setSubmeshTransparency (Ogre::MaterialPtr original
00070                        , Ogre::MaterialPtr temporary
00071                        , Ogre::Real transparency) {
00072         if (transparency > 1.0f)
00073             transparency = 1.0f;
00074 
00075         unsigned short i = 0, j;
00076 
00077         Ogre::ColourValue sc, dc; // Source colour, destination colour
00078         Ogre::Material::TechniqueIterator techniqueIt = temporary->getTechniqueIterator ();
00079         while (techniqueIt.hasMoreElements ()) {
00080             Ogre::Technique *t = techniqueIt.getNext ();
00081             Ogre::Technique::PassIterator passIt = t->getPassIterator ();
00082             j = 0;
00083             while (passIt.hasMoreElements ()) {
00084                 //sc = original->getTechnique(i)->getPass(j)->getDiffuse();
00085                 //dc = sc;
00086                 //dc.a = sc.a * (1.0f - transparency);
00087                 dc = temporary->getTechnique(i)->getPass(j)->getDiffuse();
00088                 dc.a = (1.0f - transparency);
00089                 passIt.peekNext()->setAmbient(original->getTechnique(i)->getPass(j)->getAmbient());
00090                 passIt.peekNext()->setDiffuse(dc);
00091                 passIt.moveNext();
00092 
00093                 ++j;
00094             }
00095             ++i;
00096         }
00097     }
00098 
00099 
00100 
00101     void EntityMaterial
00102     ::setTransparency (Ogre::Real transparency) {
00103         for (int i = 0; i < subEntityCount_; i++) {
00104             Ogre::SubEntity* se = entity_->getSubEntity(i);
00105             Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().getByName(se->getMaterialName());
00106             setSubmeshTransparency(originals_[i], temporaries_[i], transparency);
00107         }
00108     }
00109 
00110 
00111     Ogre::MaterialPtr EntityMaterial
00112     ::cloneMaterial(Ogre::MaterialPtr original, const char* cloneName) {
00113         // Clone
00114         Ogre::MaterialPtr material = original->clone(cloneName);
00115 
00116         // Return material
00117         return material;
00118     }
00119 
00120 
00121     void EntityMaterial
00122     ::cloneEntityMaterials(const char* name) {
00123         char cloneName[256];
00124         for (int i = 0; i < subEntityCount_; i++) {
00125             Ogre::SubEntity* se = entity_->getSubEntity(i);
00126             originals_[i] = Ogre::MaterialManager::getSingleton().getByName(se->getMaterialName());
00127 
00128             sprintf(cloneName, "%s.%d", name, i);
00129             LogDetail(cloneName);
00130             temporaries_[i] = cloneMaterial(originals_[i], cloneName);
00131             se->setMaterialName(cloneName);
00132         }
00133     }
00134 
00135 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:07 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo