vrshoot

view libs/assimp/ColladaParser.h @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file ColladaParser.h
42 * @brief Defines the parser helper class for the collada loader
43 */
45 #ifndef AI_COLLADAPARSER_H_INC
46 #define AI_COLLADAPARSER_H_INC
48 #include "irrXMLWrapper.h"
49 #include "ColladaHelper.h"
51 namespace Assimp
52 {
54 // ------------------------------------------------------------------------------------------
55 /** Parser helper class for the Collada loader.
56 *
57 * Does all the XML reading and builds internal data structures from it,
58 * but leaves the resolving of all the references to the loader.
59 */
60 class ColladaParser
61 {
62 friend class ColladaLoader;
64 protected:
65 /** Constructor from XML file */
66 ColladaParser( IOSystem* pIOHandler, const std::string& pFile);
68 /** Destructor */
69 ~ColladaParser();
71 /** Reads the contents of the file */
72 void ReadContents();
74 /** Reads the structure of the file */
75 void ReadStructure();
77 /** Reads asset informations such as coordinate system informations and legal blah */
78 void ReadAssetInfo();
80 /** Reads the animation library */
81 void ReadAnimationLibrary();
83 /** Reads an animation into the given parent structure */
84 void ReadAnimation( Collada::Animation* pParent);
86 /** Reads an animation sampler into the given anim channel */
87 void ReadAnimationSampler( Collada::AnimationChannel& pChannel);
89 /** Reads the skeleton controller library */
90 void ReadControllerLibrary();
92 /** Reads a controller into the given mesh structure */
93 void ReadController( Collada::Controller& pController);
95 /** Reads the joint definitions for the given controller */
96 void ReadControllerJoints( Collada::Controller& pController);
98 /** Reads the joint weights for the given controller */
99 void ReadControllerWeights( Collada::Controller& pController);
101 /** Reads the image library contents */
102 void ReadImageLibrary();
104 /** Reads an image entry into the given image */
105 void ReadImage( Collada::Image& pImage);
107 /** Reads the material library */
108 void ReadMaterialLibrary();
110 /** Reads a material entry into the given material */
111 void ReadMaterial( Collada::Material& pMaterial);
113 /** Reads the camera library */
114 void ReadCameraLibrary();
116 /** Reads a camera entry into the given camera */
117 void ReadCamera( Collada::Camera& pCamera);
119 /** Reads the light library */
120 void ReadLightLibrary();
122 /** Reads a light entry into the given light */
123 void ReadLight( Collada::Light& pLight);
125 /** Reads the effect library */
126 void ReadEffectLibrary();
128 /** Reads an effect entry into the given effect*/
129 void ReadEffect( Collada::Effect& pEffect);
131 /** Reads an COMMON effect profile */
132 void ReadEffectProfileCommon( Collada::Effect& pEffect);
134 /** Read sampler properties */
135 void ReadSamplerProperties( Collada::Sampler& pSampler);
137 /** Reads an effect entry containing a color or a texture defining that color */
138 void ReadEffectColor( aiColor4D& pColor, Collada::Sampler& pSampler);
140 /** Reads an effect entry containing a float */
141 void ReadEffectFloat( float& pFloat);
143 /** Reads an effect parameter specification of any kind */
144 void ReadEffectParam( Collada::EffectParam& pParam);
146 /** Reads the geometry library contents */
147 void ReadGeometryLibrary();
149 /** Reads a geometry from the geometry library. */
150 void ReadGeometry( Collada::Mesh* pMesh);
152 /** Reads a mesh from the geometry library */
153 void ReadMesh( Collada::Mesh* pMesh);
155 /** Reads a source element - a combination of raw data and an accessor defining
156 * things that should not be redefinable. Yes, that's another rant.
157 */
158 void ReadSource();
160 /** Reads a data array holding a number of elements, and stores it in the global library.
161 * Currently supported are array of floats and arrays of strings.
162 */
163 void ReadDataArray();
165 /** Reads an accessor and stores it in the global library under the given ID -
166 * accessors use the ID of the parent <source> element
167 */
168 void ReadAccessor( const std::string& pID);
170 /** Reads input declarations of per-vertex mesh data into the given mesh */
171 void ReadVertexData( Collada::Mesh* pMesh);
173 /** Reads input declarations of per-index mesh data into the given mesh */
174 void ReadIndexData( Collada::Mesh* pMesh);
176 /** Reads a single input channel element and stores it in the given array, if valid */
177 void ReadInputChannel( std::vector<Collada::InputChannel>& poChannels);
179 /** Reads a <p> primitive index list and assembles the mesh data into the given mesh */
180 void ReadPrimitives( Collada::Mesh* pMesh, std::vector<Collada::InputChannel>& pPerIndexChannels,
181 size_t pNumPrimitives, const std::vector<size_t>& pVCount, Collada::PrimitiveType pPrimType);
183 /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */
184 void ExtractDataObjectFromChannel( const Collada::InputChannel& pInput, size_t pLocalIndex, Collada::Mesh* pMesh);
186 /** Reads the library of node hierarchies and scene parts */
187 void ReadSceneLibrary();
189 /** Reads a scene node's contents including children and stores it in the given node */
190 void ReadSceneNode( Collada::Node* pNode);
192 /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */
193 void ReadNodeTransformation( Collada::Node* pNode, Collada::TransformType pType);
195 /** Reads a mesh reference in a node and adds it to the node's mesh list */
196 void ReadNodeGeometry( Collada::Node* pNode);
198 /** Reads the collada scene */
199 void ReadScene();
201 // Processes bind_vertex_input and bind elements
202 void ReadMaterialVertexInputBinding( Collada::SemanticMappingTable& tbl);
204 protected:
205 /** Aborts the file reading with an exception */
206 void ThrowException( const std::string& pError) const;
208 /** Skips all data until the end node of the current element */
209 void SkipElement();
211 /** Skips all data until the end node of the given element */
212 void SkipElement( const char* pElement);
214 /** Compares the current xml element name to the given string and returns true if equal */
215 bool IsElement( const char* pName) const;
217 /** Tests for the opening tag of the given element, throws an exception if not found */
218 void TestOpening( const char* pName);
220 /** Tests for the closing tag of the given element, throws an exception if not found */
221 void TestClosing( const char* pName);
223 /** Checks the present element for the presence of the attribute, returns its index
224 or throws an exception if not found */
225 int GetAttribute( const char* pAttr) const;
227 /** Returns the index of the named attribute or -1 if not found. Does not throw,
228 therefore useful for optional attributes */
229 int TestAttribute( const char* pAttr) const;
231 /** Reads the text contents of an element, throws an exception if not given.
232 Skips leading whitespace. */
233 const char* GetTextContent();
235 /** Reads the text contents of an element, returns NULL if not given.
236 Skips leading whitespace. */
237 const char* TestTextContent();
239 /** Reads a single bool from current text content */
240 bool ReadBoolFromTextContent();
242 /** Reads a single float from current text content */
243 float ReadFloatFromTextContent();
245 /** Calculates the resulting transformation from all the given transform steps */
246 aiMatrix4x4 CalculateResultTransform( const std::vector<Collada::Transform>& pTransforms) const;
248 /** Determines the input data type for the given semantic string */
249 Collada::InputType GetTypeForSemantic( const std::string& pSemantic);
251 /** Finds the item in the given library by its reference, throws if not found */
252 template <typename Type> const Type& ResolveLibraryReference(
253 const std::map<std::string, Type>& pLibrary, const std::string& pURL) const;
255 protected:
256 /** Filename, for a verbose error message */
257 std::string mFileName;
259 /** XML reader, member for everyday use */
260 irr::io::IrrXMLReader* mReader;
262 /** All data arrays found in the file by ID. Might be referred to by actually
263 everyone. Collada, you are a steaming pile of indirection. */
264 typedef std::map<std::string, Collada::Data> DataLibrary;
265 DataLibrary mDataLibrary;
267 /** Same for accessors which define how the data in a data array is accessed. */
268 typedef std::map<std::string, Collada::Accessor> AccessorLibrary;
269 AccessorLibrary mAccessorLibrary;
271 /** Mesh library: mesh by ID */
272 typedef std::map<std::string, Collada::Mesh*> MeshLibrary;
273 MeshLibrary mMeshLibrary;
275 /** node library: root node of the hierarchy part by ID */
276 typedef std::map<std::string, Collada::Node*> NodeLibrary;
277 NodeLibrary mNodeLibrary;
279 /** Image library: stores texture properties by ID */
280 typedef std::map<std::string, Collada::Image> ImageLibrary;
281 ImageLibrary mImageLibrary;
283 /** Effect library: surface attributes by ID */
284 typedef std::map<std::string, Collada::Effect> EffectLibrary;
285 EffectLibrary mEffectLibrary;
287 /** Material library: surface material by ID */
288 typedef std::map<std::string, Collada::Material> MaterialLibrary;
289 MaterialLibrary mMaterialLibrary;
291 /** Light library: surface light by ID */
292 typedef std::map<std::string, Collada::Light> LightLibrary;
293 LightLibrary mLightLibrary;
295 /** Camera library: surface material by ID */
296 typedef std::map<std::string, Collada::Camera> CameraLibrary;
297 CameraLibrary mCameraLibrary;
299 /** Controller library: joint controllers by ID */
300 typedef std::map<std::string, Collada::Controller> ControllerLibrary;
301 ControllerLibrary mControllerLibrary;
303 /** Pointer to the root node. Don't delete, it just points to one of
304 the nodes in the node library. */
305 Collada::Node* mRootNode;
307 /** Root animation container */
308 Collada::Animation mAnims;
310 /** Size unit: how large compared to a meter */
311 float mUnitSize;
313 /** Which is the up vector */
314 enum { UP_X, UP_Y, UP_Z } mUpDirection;
316 /** Collada file format version */
317 Collada::FormatVersion mFormat;
318 };
320 // ------------------------------------------------------------------------------------------------
321 // Check for element match
322 inline bool ColladaParser::IsElement( const char* pName) const
323 {
324 ai_assert( mReader->getNodeType() == irr::io::EXN_ELEMENT);
325 return ::strcmp( mReader->getNodeName(), pName) == 0;
326 }
328 // ------------------------------------------------------------------------------------------------
329 // Finds the item in the given library by its reference, throws if not found
330 template <typename Type>
331 const Type& ColladaParser::ResolveLibraryReference( const std::map<std::string, Type>& pLibrary, const std::string& pURL) const
332 {
333 typename std::map<std::string, Type>::const_iterator it = pLibrary.find( pURL);
334 if( it == pLibrary.end())
335 ThrowException( boost::str( boost::format( "Unable to resolve library reference \"%s\".") % pURL));
336 return it->second;
337 }
339 } // end of namespace Assimp
341 #endif // AI_COLLADAPARSER_H_INC