/** * This source file is part of OgreColladaPlugin * an addon for OGRE (Object-oriented Graphics Rendering Engine) * For the latest info, see http://www.ogre3d.org/ * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free Software * Foundation; either version 2 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 Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307, USA, or go to * http://www.gnu.org/copyleft/lesser.txt. * * @author Philipp Hartl * @see README */ #include "OgreColladaLibrary.h" #include "OgreColladaCamera.h" #include "OgreColladaGeometry.h" #include "OgreColladaLight.h" #include "OgreColladaMaterial.h" #include "OgreColladaTexture.h" #include "OgreColladaSyntax.h" #include "OgreColladaUtils.h" #include "OgreStringConverter.h" namespace Ogre { //------------------------------------------------------------------------- template ColladaLibrary::ColladaLibrary() { } //------------------------------------------------------------------------- template ColladaLibrary::~ColladaLibrary() { if (!mEntities.empty()) { for (typename ColladaLibraryVector::iterator i = mEntities.begin(); i != mEntities.end(); ++i) { OGRE_DELETE(*i); } mEntities.clear(); } } //------------------------------------------------------------------------- template void ColladaLibrary::initialise(ColladaDocument *doc, xmlNode *node) { for (xmlNode *child = node->children; child != NULL; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; // create new library instance T *entity = new T(doc, child); entity->initialise(); mEntities.push_back(entity); } } //------------------------------------------------------------------------- template T *ColladaLibrary::getEntity(const String &id) const { for (typename ColladaLibraryVector::const_iterator i = mEntities.begin(); i != mEntities.end(); ++i) { if (id == (*i)->getId()) return (*i); } return NULL; } //------------------------------------------------------------------------- ColladaLibraryContainer::ColladaLibraryContainer(ColladaDocument *doc) : mDoc(doc) { mCameras = new ColladaLibrary(); mImages = new ColladaLibrary(); mLights = new ColladaLibrary(); mTextures = new ColladaLibrary(); mMaterials = new ColladaLibrary(); mGeometries = new ColladaLibrary(); } //------------------------------------------------------------------------- ColladaLibraryContainer::~ColladaLibraryContainer(void) { OGRE_DELETE(mGeometries); OGRE_DELETE(mMaterials); OGRE_DELETE(mTextures); OGRE_DELETE(mLights); OGRE_DELETE(mImages); OGRE_DELETE(mCameras); mDoc = NULL; } //------------------------------------------------------------------------- void ColladaLibraryContainer::doImport(xmlNode *node) { // get the library type property String type = ColladaUtils::getProperty(node, CS_ATR_TYPE); // the corresponding enumeration type ColladaLibrarySpecific::Type libtype = ColladaLibrarySpecific::getType(type); if (libtype != ColladaLibrarySpecific::UNKNOWN) { if (libtype == ColladaLibrarySpecific::CAMERA) mCameras->initialise(mDoc, node); else if (libtype == ColladaLibrarySpecific::GEOMETRY) mGeometries->initialise(mDoc, node); else if (libtype == ColladaLibrarySpecific::IMAGE) mImages->initialise(mDoc, node); else if (libtype == ColladaLibrarySpecific::LIGHT) mLights->initialise(mDoc, node); else if (libtype == ColladaLibrarySpecific::MATERIAL) mMaterials->initialise(mDoc, node); else if (libtype == ColladaLibrarySpecific::TEXTURE) mTextures->initialise(mDoc, node); else LogManager::getSingleton().logMessage("ColladaLibraryContainer::doImport - unsupported"); } // unknwon else { LogManager::getSingleton().logMessage("ColladaLibraryContainer::doImport - unknown"); } } //------------------------------------------------------------------------- ColladaCamera *ColladaLibraryContainer::getCamera(const String &id) const { return mCameras->getEntity(id); } //------------------------------------------------------------------------- ColladaImage *ColladaLibraryContainer::getImage(const String &id) const { return mImages->getEntity(id); } //------------------------------------------------------------------------- ColladaLight *ColladaLibraryContainer::getLight(const String &id) const { return mLights->getEntity(id); } //------------------------------------------------------------------------- ColladaMaterial *ColladaLibraryContainer::getMaterial(const String &id) const { return mMaterials->getEntity(id); } //------------------------------------------------------------------------- ColladaTexture *ColladaLibraryContainer::getTexture(const String &id) const { return mTextures->getEntity(id); } //------------------------------------------------------------------------- ColladaEntity *ColladaLibraryContainer::importInstance(const String &id) { // look in the library entities for the identifier ColladaEntity *entity = NULL; // first start with geometries entity = mGeometries->getEntity(id); // camera if (entity == NULL) entity = mCameras->getEntity(id); // light if (entity == NULL) entity = mLights->getEntity(id); if (entity == NULL) return NULL; // try to import the instance if (!entity->doImport()) { LogManager::getSingleton().logMessage("ColladaLibraryContainer::importInstance - doImport of instance " + id + " failed! Entity: " + StringConverter::toString(entity->getEntityType())); } return entity; } //------------------------------------------------------------------------- namespace ColladaLibrarySpecific { Type getType(const String &s) { if (s == CS_VAL_LIBRARY_TYPE_ANIMATION) return ANIMATION; else if (s == CS_VAL_LIBRARY_TYPE_CAMERA) return CAMERA; else if (s == CS_VAL_LIBRARY_TYPE_CODE) return CODE; else if (s == CS_VAL_LIBRARY_TYPE_CONTROLLER) return CONTROLLER; else if (s == CS_VAL_LIBRARY_TYPE_GEOMETRY) return GEOMETRY; else if (s == CS_VAL_LIBRARY_TYPE_IMAGE) return IMAGE; else if (s == CS_VAL_LIBRARY_TYPE_LIGHT) return LIGHT; else if (s == CS_VAL_LIBRARY_TYPE_MATERIAL) return MATERIAL; else if (s == CS_VAL_LIBRARY_TYPE_PROGRAM) return PROGRAM; else if (s == CS_VAL_LIBRARY_TYPE_TEXTURE) return TEXTURE; else return UNKNOWN; } } }