| 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 "OgreColladaTexture.h" | 
|---|
| 25 | #include "OgreColladaDocument.h" | 
|---|
| 26 | #include "OgreColladaLibrary.h" | 
|---|
| 27 | #include "OgreColladaSyntax.h" | 
|---|
| 28 | #include "OgreColladaUtils.h" | 
|---|
| 29 |  | 
|---|
| 30 | namespace Ogre | 
|---|
| 31 | { | 
|---|
| 32 | //------------------------------------------------------------------------- | 
|---|
| 33 | ColladaImage::ColladaImage(ColladaDocument *d, xmlNode *n) : ColladaEntity(d, n) { } | 
|---|
| 34 |  | 
|---|
| 35 | //------------------------------------------------------------------------- | 
|---|
| 36 | ColladaImage::~ColladaImage(void) { } | 
|---|
| 37 |  | 
|---|
| 38 | //------------------------------------------------------------------------- | 
|---|
| 39 | bool ColladaImage::doImport(void) | 
|---|
| 40 | { | 
|---|
| 41 | if (mLoaded) return mStatus; | 
|---|
| 42 | else mLoaded = true; | 
|---|
| 43 |  | 
|---|
| 44 | // get attributes of <image> tag | 
|---|
| 45 | // source is a must, watch out for reference url (local, extern)! | 
|---|
| 46 | mSource = ColladaUtils::getProperty(mNode, CS_ATR_IMAGE_SOURCE); | 
|---|
| 47 |  | 
|---|
| 48 | String tmp = ColladaUtils::getProperty(mNode, CS_ATR_IMAGE_FORMAT); | 
|---|
| 49 | mFormat = (!tmp.empty()) ? tmp : ""; | 
|---|
| 50 |  | 
|---|
| 51 | tmp = ColladaUtils::getProperty(mNode, CS_ATR_IMAGE_HEIGHT); | 
|---|
| 52 | mHeight = (!tmp.empty()) ? static_cast<uint>(atoi(tmp.c_str())) : 0; | 
|---|
| 53 |  | 
|---|
| 54 | tmp = ColladaUtils::getProperty(mNode, CS_ATR_IMAGE_WIDTH); | 
|---|
| 55 | mWidth = (!tmp.empty()) ? static_cast<uint>(atoi(tmp.c_str())) : 0; | 
|---|
| 56 |  | 
|---|
| 57 | tmp = ColladaUtils::getProperty(mNode, CS_ATR_IMAGE_DEPTH); | 
|---|
| 58 | mDepth = (!tmp.empty()) ? static_cast<uint>(atoi(tmp.c_str())) : 1; | 
|---|
| 59 |  | 
|---|
| 60 | mStatus = true; | 
|---|
| 61 | return mStatus; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | //------------------------------------------------------------------------- | 
|---|
| 65 | // ColladaTexture | 
|---|
| 66 | //------------------------------------------------------------------------- | 
|---|
| 67 |  | 
|---|
| 68 | //------------------------------------------------------------------------- | 
|---|
| 69 | ColladaTexture::ColladaTexture(ColladaDocument *d, xmlNode *n) : ColladaEntity(d, n) | 
|---|
| 70 | { | 
|---|
| 71 | mImage = NULL; | 
|---|
| 72 | mType  = ColladaTextureSpecific::UNKNOWN; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | //------------------------------------------------------------------------- | 
|---|
| 76 | ColladaTexture::~ColladaTexture(void) | 
|---|
| 77 | { | 
|---|
| 78 | mImage = NULL;  // attention, the image instance will be deleted by the library container! | 
|---|
| 79 | mType  = ColladaTextureSpecific::UNKNOWN; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | //------------------------------------------------------------------------- | 
|---|
| 83 | bool ColladaTexture::doImport(void) | 
|---|
| 84 | { | 
|---|
| 85 | if (mLoaded) return mStatus; | 
|---|
| 86 | else mLoaded = true; | 
|---|
| 87 |  | 
|---|
| 88 | // <asset> child | 
|---|
| 89 | // xmlNode *asset = ColladaUtils::getChildByTagName(mNode, CS_ELM_ASSET); | 
|---|
| 90 |  | 
|---|
| 91 | // <param> childs, describes the texture dataflow | 
|---|
| 92 | xmlNodePtrVector params = ColladaUtils::getChildsByTagName(mNode, CS_ELM_PARAM); | 
|---|
| 93 | xmlNode *param = NULL; | 
|---|
| 94 | if (!params.empty()) param = params.at(0); | 
|---|
| 95 | // get the texture type | 
|---|
| 96 | String texturetype = ColladaUtils::getProperty(param, CS_ATR_NAME); | 
|---|
| 97 | mType  = ColladaTextureSpecific::getType(texturetype); | 
|---|
| 98 | if (mType == ColladaTextureSpecific::UNKNOWN) | 
|---|
| 99 | { | 
|---|
| 100 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - <param name=" + texturetype + "> unknown! " + mId); | 
|---|
| 101 | return false; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // <technique> nodes | 
|---|
| 105 | xmlNodePtrVector techniques = ColladaUtils::getChildsByTagName(mNode, CS_ELM_TECHNIQUE); | 
|---|
| 106 | // currently we are only interested in "COMMON" profile | 
|---|
| 107 | xmlNode *technique = NULL; | 
|---|
| 108 | for (xmlNodePtrVector::iterator it = techniques.begin(); it != techniques.end(); ++it) | 
|---|
| 109 | { | 
|---|
| 110 | String profile = ColladaUtils::getProperty(*it, CS_ATR_PROFILE); | 
|---|
| 111 | if (profile == CS_VAL_TECHNIQUE_PROFILE_COMMON) | 
|---|
| 112 | { | 
|---|
| 113 | technique = *it; | 
|---|
| 114 | break; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 | if (technique == NULL) return false; | 
|---|
| 118 |  | 
|---|
| 119 | // the <input> child, there are more than one possible, but only one makes sense | 
|---|
| 120 | xmlNode *input = ColladaUtils::getChildByTagName(technique, CS_ELM_INPUT); | 
|---|
| 121 | // the semantic property should be "IMAGE" | 
|---|
| 122 | String semantic = ColladaUtils::getProperty(input, CS_ATR_INPUT_SEMANTIC); | 
|---|
| 123 | if (semantic != CS_VAL_INPUT_SEMANTIC_IMAGE) | 
|---|
| 124 | { | 
|---|
| 125 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - <input semantic=" + semantic + "> unknown"); | 
|---|
| 126 | return false; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // the source url | 
|---|
| 130 | String source = ColladaUtils::getProperty(input, CS_ATR_INPUT_SOURCE); | 
|---|
| 131 | if (source.empty()) | 
|---|
| 132 | { | 
|---|
| 133 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - no source url for texture is given! " + mId); | 
|---|
| 134 | return false; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | // local reference of image file | 
|---|
| 138 | if (source.find("#") == 0) | 
|---|
| 139 | { | 
|---|
| 140 | source.erase(0,1); | 
|---|
| 141 |  | 
|---|
| 142 | // try to find the instance and load it, if it is not already loaded | 
|---|
| 143 | mImage = mDoc->getLibrary()->getImage(source); | 
|---|
| 144 | if (mImage == NULL) | 
|---|
| 145 | { | 
|---|
| 146 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - can't find the image reference " + source + "! " + mId); | 
|---|
| 147 | return false; | 
|---|
| 148 | } | 
|---|
| 149 | // try to import the entity | 
|---|
| 150 | if (!mImage->doImport()) | 
|---|
| 151 | { | 
|---|
| 152 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - loading of image " + source + " failed! " + mId); | 
|---|
| 153 | return false; | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 | // external | 
|---|
| 157 | else | 
|---|
| 158 | { | 
|---|
| 159 | LogManager::getSingleton().logMessage("ColladaTexture::doImport - external refernces are currently not implemented! " + mId); | 
|---|
| 160 | return false; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | mStatus = true; | 
|---|
| 164 | return mStatus; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | //------------------------------------------------------------------------- | 
|---|
| 168 | namespace ColladaTextureSpecific | 
|---|
| 169 | { | 
|---|
| 170 | Type getType(const String &s) | 
|---|
| 171 | { | 
|---|
| 172 | if (s == CS_VAL_TEXTURE_PARAM_AMBIENT) return AMBIENT; | 
|---|
| 173 | else if (s == CS_VAL_TEXTURE_PARAM_DIFFUSE) return DIFFUSE; | 
|---|
| 174 | else if (s == CS_VAL_TEXTURE_PARAM_EMISSION) return EMISSION; | 
|---|
| 175 | else if (s == CS_VAL_TEXTURE_PARAM_SHININESS) return SHININESS; | 
|---|
| 176 | else if (s == CS_VAL_TEXTURE_PARAM_SPECULAR) return SPECULAR; | 
|---|
| 177 | else return UNKNOWN; | 
|---|
| 178 | }; | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|