[21] | 1 | /** |
---|
| 2 | * This source file is part of OgreColladaPlugin |
---|
| 3 | * an addon for OGRE (Object-oriented Graphics Rendering Engine) |
---|
| 4 | * For the latest info, see http://www.ogre3d.org/ |
---|
| 5 | * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify it under |
---|
| 7 | * the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 8 | * Foundation; either version 2 of the License, or (at your option) any later |
---|
| 9 | * version. |
---|
| 10 | |
---|
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 13 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 14 | |
---|
| 15 | * You should have received a copy of the GNU Lesser General Public License along with |
---|
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 17 | * Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 18 | * http://www.gnu.org/copyleft/lesser.txt. |
---|
| 19 | * |
---|
| 20 | * @author Philipp Hartl |
---|
| 21 | * @see README |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include "OgreColladaLibrary.h" |
---|
| 25 | #include "OgreColladaCamera.h" |
---|
| 26 | #include "OgreColladaGeometry.h" |
---|
| 27 | #include "OgreColladaLight.h" |
---|
| 28 | #include "OgreColladaMaterial.h" |
---|
| 29 | #include "OgreColladaTexture.h" |
---|
| 30 | #include "OgreColladaSyntax.h" |
---|
| 31 | #include "OgreColladaUtils.h" |
---|
| 32 | |
---|
| 33 | #include "OgreStringConverter.h" |
---|
| 34 | |
---|
| 35 | namespace Ogre |
---|
| 36 | { |
---|
| 37 | //------------------------------------------------------------------------- |
---|
| 38 | template <class T> |
---|
| 39 | ColladaLibrary<T>::ColladaLibrary() { } |
---|
| 40 | |
---|
| 41 | //------------------------------------------------------------------------- |
---|
| 42 | template <class T> |
---|
| 43 | ColladaLibrary<T>::~ColladaLibrary() |
---|
| 44 | { |
---|
| 45 | if (!mEntities.empty()) |
---|
| 46 | { |
---|
| 47 | for (typename ColladaLibraryVector::iterator i = mEntities.begin(); |
---|
| 48 | i != mEntities.end(); |
---|
| 49 | ++i) |
---|
| 50 | { |
---|
| 51 | OGRE_DELETE(*i); |
---|
| 52 | } |
---|
| 53 | mEntities.clear(); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | //------------------------------------------------------------------------- |
---|
| 58 | template <class T> |
---|
| 59 | void ColladaLibrary<T>::initialise(ColladaDocument *doc, xmlNode *node) |
---|
| 60 | { |
---|
| 61 | for (xmlNode *child = node->children; child != NULL; child = child->next) |
---|
| 62 | { |
---|
| 63 | if (child->type != XML_ELEMENT_NODE) continue; |
---|
| 64 | |
---|
| 65 | // create new library instance |
---|
| 66 | T *entity = new T(doc, child); |
---|
| 67 | entity->initialise(); |
---|
| 68 | |
---|
| 69 | mEntities.push_back(entity); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | //------------------------------------------------------------------------- |
---|
| 74 | template <class T> |
---|
| 75 | T *ColladaLibrary<T>::getEntity(const String &id) const |
---|
| 76 | { |
---|
| 77 | for (typename ColladaLibraryVector::const_iterator i = mEntities.begin(); i != mEntities.end(); ++i) |
---|
| 78 | { |
---|
| 79 | if (id == (*i)->getId()) return (*i); |
---|
| 80 | } |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | //------------------------------------------------------------------------- |
---|
| 85 | ColladaLibraryContainer::ColladaLibraryContainer(ColladaDocument *doc) : mDoc(doc) |
---|
| 86 | { |
---|
| 87 | mCameras = new ColladaLibrary<ColladaCamera>(); |
---|
| 88 | mImages = new ColladaLibrary<ColladaImage>(); |
---|
| 89 | mLights = new ColladaLibrary<ColladaLight>(); |
---|
| 90 | mTextures = new ColladaLibrary<ColladaTexture>(); |
---|
| 91 | mMaterials = new ColladaLibrary<ColladaMaterial>(); |
---|
| 92 | mGeometries = new ColladaLibrary<ColladaGeometry>(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | //------------------------------------------------------------------------- |
---|
| 96 | ColladaLibraryContainer::~ColladaLibraryContainer(void) |
---|
| 97 | { |
---|
| 98 | OGRE_DELETE(mGeometries); |
---|
| 99 | OGRE_DELETE(mMaterials); |
---|
| 100 | OGRE_DELETE(mTextures); |
---|
| 101 | OGRE_DELETE(mLights); |
---|
| 102 | OGRE_DELETE(mImages); |
---|
| 103 | OGRE_DELETE(mCameras); |
---|
| 104 | |
---|
| 105 | mDoc = NULL; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | //------------------------------------------------------------------------- |
---|
| 109 | void ColladaLibraryContainer::doImport(xmlNode *node) |
---|
| 110 | { |
---|
| 111 | // get the library type property |
---|
| 112 | String type = ColladaUtils::getProperty(node, CS_ATR_TYPE); |
---|
| 113 | // the corresponding enumeration type |
---|
| 114 | ColladaLibrarySpecific::Type libtype = ColladaLibrarySpecific::getType(type); |
---|
| 115 | |
---|
| 116 | if (libtype != ColladaLibrarySpecific::UNKNOWN) |
---|
| 117 | { |
---|
| 118 | if (libtype == ColladaLibrarySpecific::CAMERA) mCameras->initialise(mDoc, node); |
---|
| 119 | else if (libtype == ColladaLibrarySpecific::GEOMETRY) mGeometries->initialise(mDoc, node); |
---|
| 120 | else if (libtype == ColladaLibrarySpecific::IMAGE) mImages->initialise(mDoc, node); |
---|
| 121 | else if (libtype == ColladaLibrarySpecific::LIGHT) mLights->initialise(mDoc, node); |
---|
| 122 | else if (libtype == ColladaLibrarySpecific::MATERIAL) mMaterials->initialise(mDoc, node); |
---|
| 123 | else if (libtype == ColladaLibrarySpecific::TEXTURE) mTextures->initialise(mDoc, node); |
---|
| 124 | else LogManager::getSingleton().logMessage("ColladaLibraryContainer::doImport - <library type=" + type + "> unsupported"); |
---|
| 125 | } |
---|
| 126 | // unknwon |
---|
| 127 | else |
---|
| 128 | { |
---|
| 129 | LogManager::getSingleton().logMessage("ColladaLibraryContainer::doImport - <library type=" + type + "> unknown"); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | //------------------------------------------------------------------------- |
---|
| 134 | ColladaCamera *ColladaLibraryContainer::getCamera(const String &id) const |
---|
| 135 | { |
---|
| 136 | return mCameras->getEntity(id); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | //------------------------------------------------------------------------- |
---|
| 140 | ColladaImage *ColladaLibraryContainer::getImage(const String &id) const |
---|
| 141 | { |
---|
| 142 | return mImages->getEntity(id); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | //------------------------------------------------------------------------- |
---|
| 146 | ColladaLight *ColladaLibraryContainer::getLight(const String &id) const |
---|
| 147 | { |
---|
| 148 | return mLights->getEntity(id); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | //------------------------------------------------------------------------- |
---|
| 152 | ColladaMaterial *ColladaLibraryContainer::getMaterial(const String &id) const |
---|
| 153 | { |
---|
| 154 | return mMaterials->getEntity(id); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | //------------------------------------------------------------------------- |
---|
| 158 | ColladaTexture *ColladaLibraryContainer::getTexture(const String &id) const |
---|
| 159 | { |
---|
| 160 | return mTextures->getEntity(id); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | //------------------------------------------------------------------------- |
---|
| 164 | ColladaEntity *ColladaLibraryContainer::importInstance(const String &id) |
---|
| 165 | { |
---|
| 166 | // look in the library entities for the identifier |
---|
| 167 | ColladaEntity *entity = NULL; |
---|
| 168 | |
---|
| 169 | // first start with geometries |
---|
| 170 | entity = mGeometries->getEntity(id); |
---|
| 171 | |
---|
| 172 | // camera |
---|
| 173 | if (entity == NULL) entity = mCameras->getEntity(id); |
---|
| 174 | |
---|
| 175 | // light |
---|
| 176 | if (entity == NULL) entity = mLights->getEntity(id); |
---|
| 177 | |
---|
| 178 | if (entity == NULL) return NULL; |
---|
| 179 | |
---|
| 180 | // try to import the instance |
---|
| 181 | if (!entity->doImport()) |
---|
| 182 | { |
---|
| 183 | LogManager::getSingleton().logMessage("ColladaLibraryContainer::importInstance - doImport of instance " + id + " failed! Entity: " + StringConverter::toString(entity->getEntityType())); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | return entity; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | //------------------------------------------------------------------------- |
---|
| 190 | namespace ColladaLibrarySpecific |
---|
| 191 | { |
---|
| 192 | Type getType(const String &s) |
---|
| 193 | { |
---|
| 194 | if (s == CS_VAL_LIBRARY_TYPE_ANIMATION) return ANIMATION; |
---|
| 195 | else if (s == CS_VAL_LIBRARY_TYPE_CAMERA) return CAMERA; |
---|
| 196 | else if (s == CS_VAL_LIBRARY_TYPE_CODE) return CODE; |
---|
| 197 | else if (s == CS_VAL_LIBRARY_TYPE_CONTROLLER) return CONTROLLER; |
---|
| 198 | else if (s == CS_VAL_LIBRARY_TYPE_GEOMETRY) return GEOMETRY; |
---|
| 199 | else if (s == CS_VAL_LIBRARY_TYPE_IMAGE) return IMAGE; |
---|
| 200 | else if (s == CS_VAL_LIBRARY_TYPE_LIGHT) return LIGHT; |
---|
| 201 | else if (s == CS_VAL_LIBRARY_TYPE_MATERIAL) return MATERIAL; |
---|
| 202 | else if (s == CS_VAL_LIBRARY_TYPE_PROGRAM) return PROGRAM; |
---|
| 203 | else if (s == CS_VAL_LIBRARY_TYPE_TEXTURE) return TEXTURE; |
---|
| 204 | else return UNKNOWN; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|