vrshoot
diff libs/assimp/ColladaParser.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/ColladaParser.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,341 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 +copyright notice, this list of conditions and the 1.17 +following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 +copyright notice, this list of conditions and the 1.21 +following disclaimer in the documentation and/or other 1.22 +materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 +contributors may be used to endorse or promote products 1.26 +derived from this software without specific prior 1.27 +written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file ColladaParser.h 1.45 + * @brief Defines the parser helper class for the collada loader 1.46 + */ 1.47 + 1.48 +#ifndef AI_COLLADAPARSER_H_INC 1.49 +#define AI_COLLADAPARSER_H_INC 1.50 + 1.51 +#include "irrXMLWrapper.h" 1.52 +#include "ColladaHelper.h" 1.53 + 1.54 +namespace Assimp 1.55 +{ 1.56 + 1.57 +// ------------------------------------------------------------------------------------------ 1.58 +/** Parser helper class for the Collada loader. 1.59 + * 1.60 + * Does all the XML reading and builds internal data structures from it, 1.61 + * but leaves the resolving of all the references to the loader. 1.62 +*/ 1.63 +class ColladaParser 1.64 +{ 1.65 + friend class ColladaLoader; 1.66 + 1.67 +protected: 1.68 + /** Constructor from XML file */ 1.69 + ColladaParser( IOSystem* pIOHandler, const std::string& pFile); 1.70 + 1.71 + /** Destructor */ 1.72 + ~ColladaParser(); 1.73 + 1.74 + /** Reads the contents of the file */ 1.75 + void ReadContents(); 1.76 + 1.77 + /** Reads the structure of the file */ 1.78 + void ReadStructure(); 1.79 + 1.80 + /** Reads asset informations such as coordinate system informations and legal blah */ 1.81 + void ReadAssetInfo(); 1.82 + 1.83 + /** Reads the animation library */ 1.84 + void ReadAnimationLibrary(); 1.85 + 1.86 + /** Reads an animation into the given parent structure */ 1.87 + void ReadAnimation( Collada::Animation* pParent); 1.88 + 1.89 + /** Reads an animation sampler into the given anim channel */ 1.90 + void ReadAnimationSampler( Collada::AnimationChannel& pChannel); 1.91 + 1.92 + /** Reads the skeleton controller library */ 1.93 + void ReadControllerLibrary(); 1.94 + 1.95 + /** Reads a controller into the given mesh structure */ 1.96 + void ReadController( Collada::Controller& pController); 1.97 + 1.98 + /** Reads the joint definitions for the given controller */ 1.99 + void ReadControllerJoints( Collada::Controller& pController); 1.100 + 1.101 + /** Reads the joint weights for the given controller */ 1.102 + void ReadControllerWeights( Collada::Controller& pController); 1.103 + 1.104 + /** Reads the image library contents */ 1.105 + void ReadImageLibrary(); 1.106 + 1.107 + /** Reads an image entry into the given image */ 1.108 + void ReadImage( Collada::Image& pImage); 1.109 + 1.110 + /** Reads the material library */ 1.111 + void ReadMaterialLibrary(); 1.112 + 1.113 + /** Reads a material entry into the given material */ 1.114 + void ReadMaterial( Collada::Material& pMaterial); 1.115 + 1.116 + /** Reads the camera library */ 1.117 + void ReadCameraLibrary(); 1.118 + 1.119 + /** Reads a camera entry into the given camera */ 1.120 + void ReadCamera( Collada::Camera& pCamera); 1.121 + 1.122 + /** Reads the light library */ 1.123 + void ReadLightLibrary(); 1.124 + 1.125 + /** Reads a light entry into the given light */ 1.126 + void ReadLight( Collada::Light& pLight); 1.127 + 1.128 + /** Reads the effect library */ 1.129 + void ReadEffectLibrary(); 1.130 + 1.131 + /** Reads an effect entry into the given effect*/ 1.132 + void ReadEffect( Collada::Effect& pEffect); 1.133 + 1.134 + /** Reads an COMMON effect profile */ 1.135 + void ReadEffectProfileCommon( Collada::Effect& pEffect); 1.136 + 1.137 + /** Read sampler properties */ 1.138 + void ReadSamplerProperties( Collada::Sampler& pSampler); 1.139 + 1.140 + /** Reads an effect entry containing a color or a texture defining that color */ 1.141 + void ReadEffectColor( aiColor4D& pColor, Collada::Sampler& pSampler); 1.142 + 1.143 + /** Reads an effect entry containing a float */ 1.144 + void ReadEffectFloat( float& pFloat); 1.145 + 1.146 + /** Reads an effect parameter specification of any kind */ 1.147 + void ReadEffectParam( Collada::EffectParam& pParam); 1.148 + 1.149 + /** Reads the geometry library contents */ 1.150 + void ReadGeometryLibrary(); 1.151 + 1.152 + /** Reads a geometry from the geometry library. */ 1.153 + void ReadGeometry( Collada::Mesh* pMesh); 1.154 + 1.155 + /** Reads a mesh from the geometry library */ 1.156 + void ReadMesh( Collada::Mesh* pMesh); 1.157 + 1.158 + /** Reads a source element - a combination of raw data and an accessor defining 1.159 + * things that should not be redefinable. Yes, that's another rant. 1.160 + */ 1.161 + void ReadSource(); 1.162 + 1.163 + /** Reads a data array holding a number of elements, and stores it in the global library. 1.164 + * Currently supported are array of floats and arrays of strings. 1.165 + */ 1.166 + void ReadDataArray(); 1.167 + 1.168 + /** Reads an accessor and stores it in the global library under the given ID - 1.169 + * accessors use the ID of the parent <source> element 1.170 + */ 1.171 + void ReadAccessor( const std::string& pID); 1.172 + 1.173 + /** Reads input declarations of per-vertex mesh data into the given mesh */ 1.174 + void ReadVertexData( Collada::Mesh* pMesh); 1.175 + 1.176 + /** Reads input declarations of per-index mesh data into the given mesh */ 1.177 + void ReadIndexData( Collada::Mesh* pMesh); 1.178 + 1.179 + /** Reads a single input channel element and stores it in the given array, if valid */ 1.180 + void ReadInputChannel( std::vector<Collada::InputChannel>& poChannels); 1.181 + 1.182 + /** Reads a <p> primitive index list and assembles the mesh data into the given mesh */ 1.183 + void ReadPrimitives( Collada::Mesh* pMesh, std::vector<Collada::InputChannel>& pPerIndexChannels, 1.184 + size_t pNumPrimitives, const std::vector<size_t>& pVCount, Collada::PrimitiveType pPrimType); 1.185 + 1.186 + /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */ 1.187 + void ExtractDataObjectFromChannel( const Collada::InputChannel& pInput, size_t pLocalIndex, Collada::Mesh* pMesh); 1.188 + 1.189 + /** Reads the library of node hierarchies and scene parts */ 1.190 + void ReadSceneLibrary(); 1.191 + 1.192 + /** Reads a scene node's contents including children and stores it in the given node */ 1.193 + void ReadSceneNode( Collada::Node* pNode); 1.194 + 1.195 + /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */ 1.196 + void ReadNodeTransformation( Collada::Node* pNode, Collada::TransformType pType); 1.197 + 1.198 + /** Reads a mesh reference in a node and adds it to the node's mesh list */ 1.199 + void ReadNodeGeometry( Collada::Node* pNode); 1.200 + 1.201 + /** Reads the collada scene */ 1.202 + void ReadScene(); 1.203 + 1.204 + // Processes bind_vertex_input and bind elements 1.205 + void ReadMaterialVertexInputBinding( Collada::SemanticMappingTable& tbl); 1.206 + 1.207 +protected: 1.208 + /** Aborts the file reading with an exception */ 1.209 + void ThrowException( const std::string& pError) const; 1.210 + 1.211 + /** Skips all data until the end node of the current element */ 1.212 + void SkipElement(); 1.213 + 1.214 + /** Skips all data until the end node of the given element */ 1.215 + void SkipElement( const char* pElement); 1.216 + 1.217 + /** Compares the current xml element name to the given string and returns true if equal */ 1.218 + bool IsElement( const char* pName) const; 1.219 + 1.220 + /** Tests for the opening tag of the given element, throws an exception if not found */ 1.221 + void TestOpening( const char* pName); 1.222 + 1.223 + /** Tests for the closing tag of the given element, throws an exception if not found */ 1.224 + void TestClosing( const char* pName); 1.225 + 1.226 + /** Checks the present element for the presence of the attribute, returns its index 1.227 + or throws an exception if not found */ 1.228 + int GetAttribute( const char* pAttr) const; 1.229 + 1.230 + /** Returns the index of the named attribute or -1 if not found. Does not throw, 1.231 + therefore useful for optional attributes */ 1.232 + int TestAttribute( const char* pAttr) const; 1.233 + 1.234 + /** Reads the text contents of an element, throws an exception if not given. 1.235 + Skips leading whitespace. */ 1.236 + const char* GetTextContent(); 1.237 + 1.238 + /** Reads the text contents of an element, returns NULL if not given. 1.239 + Skips leading whitespace. */ 1.240 + const char* TestTextContent(); 1.241 + 1.242 + /** Reads a single bool from current text content */ 1.243 + bool ReadBoolFromTextContent(); 1.244 + 1.245 + /** Reads a single float from current text content */ 1.246 + float ReadFloatFromTextContent(); 1.247 + 1.248 + /** Calculates the resulting transformation from all the given transform steps */ 1.249 + aiMatrix4x4 CalculateResultTransform( const std::vector<Collada::Transform>& pTransforms) const; 1.250 + 1.251 + /** Determines the input data type for the given semantic string */ 1.252 + Collada::InputType GetTypeForSemantic( const std::string& pSemantic); 1.253 + 1.254 + /** Finds the item in the given library by its reference, throws if not found */ 1.255 + template <typename Type> const Type& ResolveLibraryReference( 1.256 + const std::map<std::string, Type>& pLibrary, const std::string& pURL) const; 1.257 + 1.258 +protected: 1.259 + /** Filename, for a verbose error message */ 1.260 + std::string mFileName; 1.261 + 1.262 + /** XML reader, member for everyday use */ 1.263 + irr::io::IrrXMLReader* mReader; 1.264 + 1.265 + /** All data arrays found in the file by ID. Might be referred to by actually 1.266 + everyone. Collada, you are a steaming pile of indirection. */ 1.267 + typedef std::map<std::string, Collada::Data> DataLibrary; 1.268 + DataLibrary mDataLibrary; 1.269 + 1.270 + /** Same for accessors which define how the data in a data array is accessed. */ 1.271 + typedef std::map<std::string, Collada::Accessor> AccessorLibrary; 1.272 + AccessorLibrary mAccessorLibrary; 1.273 + 1.274 + /** Mesh library: mesh by ID */ 1.275 + typedef std::map<std::string, Collada::Mesh*> MeshLibrary; 1.276 + MeshLibrary mMeshLibrary; 1.277 + 1.278 + /** node library: root node of the hierarchy part by ID */ 1.279 + typedef std::map<std::string, Collada::Node*> NodeLibrary; 1.280 + NodeLibrary mNodeLibrary; 1.281 + 1.282 + /** Image library: stores texture properties by ID */ 1.283 + typedef std::map<std::string, Collada::Image> ImageLibrary; 1.284 + ImageLibrary mImageLibrary; 1.285 + 1.286 + /** Effect library: surface attributes by ID */ 1.287 + typedef std::map<std::string, Collada::Effect> EffectLibrary; 1.288 + EffectLibrary mEffectLibrary; 1.289 + 1.290 + /** Material library: surface material by ID */ 1.291 + typedef std::map<std::string, Collada::Material> MaterialLibrary; 1.292 + MaterialLibrary mMaterialLibrary; 1.293 + 1.294 + /** Light library: surface light by ID */ 1.295 + typedef std::map<std::string, Collada::Light> LightLibrary; 1.296 + LightLibrary mLightLibrary; 1.297 + 1.298 + /** Camera library: surface material by ID */ 1.299 + typedef std::map<std::string, Collada::Camera> CameraLibrary; 1.300 + CameraLibrary mCameraLibrary; 1.301 + 1.302 + /** Controller library: joint controllers by ID */ 1.303 + typedef std::map<std::string, Collada::Controller> ControllerLibrary; 1.304 + ControllerLibrary mControllerLibrary; 1.305 + 1.306 + /** Pointer to the root node. Don't delete, it just points to one of 1.307 + the nodes in the node library. */ 1.308 + Collada::Node* mRootNode; 1.309 + 1.310 + /** Root animation container */ 1.311 + Collada::Animation mAnims; 1.312 + 1.313 + /** Size unit: how large compared to a meter */ 1.314 + float mUnitSize; 1.315 + 1.316 + /** Which is the up vector */ 1.317 + enum { UP_X, UP_Y, UP_Z } mUpDirection; 1.318 + 1.319 + /** Collada file format version */ 1.320 + Collada::FormatVersion mFormat; 1.321 +}; 1.322 + 1.323 +// ------------------------------------------------------------------------------------------------ 1.324 +// Check for element match 1.325 +inline bool ColladaParser::IsElement( const char* pName) const 1.326 +{ 1.327 + ai_assert( mReader->getNodeType() == irr::io::EXN_ELEMENT); 1.328 + return ::strcmp( mReader->getNodeName(), pName) == 0; 1.329 +} 1.330 + 1.331 +// ------------------------------------------------------------------------------------------------ 1.332 +// Finds the item in the given library by its reference, throws if not found 1.333 +template <typename Type> 1.334 +const Type& ColladaParser::ResolveLibraryReference( const std::map<std::string, Type>& pLibrary, const std::string& pURL) const 1.335 +{ 1.336 + typename std::map<std::string, Type>::const_iterator it = pLibrary.find( pURL); 1.337 + if( it == pLibrary.end()) 1.338 + ThrowException( boost::str( boost::format( "Unable to resolve library reference \"%s\".") % pURL)); 1.339 + return it->second; 1.340 +} 1.341 + 1.342 +} // end of namespace Assimp 1.343 + 1.344 +#endif // AI_COLLADAPARSER_H_INC