vrshoot

view libs/assimp/ColladaHelper.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /** Helper structures for the Collada loader */
3 /*
4 Open Asset Import Library (assimp)
5 ----------------------------------------------------------------------
7 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 ----------------------------------------------------------------------
41 */
43 #ifndef AI_COLLADAHELPER_H_INC
44 #define AI_COLLADAHELPER_H_INC
46 namespace Assimp {
47 namespace Collada {
49 /** Collada file versions which evolved during the years ... */
50 enum FormatVersion
51 {
52 FV_1_5_n,
53 FV_1_4_n,
54 FV_1_3_n
55 };
58 /** Transformation types that can be applied to a node */
59 enum TransformType
60 {
61 TF_LOOKAT,
62 TF_ROTATE,
63 TF_TRANSLATE,
64 TF_SCALE,
65 TF_SKEW,
66 TF_MATRIX
67 };
69 /** Different types of input data to a vertex or face */
70 enum InputType
71 {
72 IT_Invalid,
73 IT_Vertex, // special type for per-index data referring to the <vertices> element carrying the per-vertex data.
74 IT_Position,
75 IT_Normal,
76 IT_Texcoord,
77 IT_Color,
78 IT_Tangent,
79 IT_Bitangent
80 };
82 /** Contains all data for one of the different transformation types */
83 struct Transform
84 {
85 std::string mID; ///< SID of the transform step, by which anim channels address their target node
86 TransformType mType;
87 float f[16]; ///< Interpretation of data depends on the type of the transformation
88 };
90 /** A collada camera. */
91 struct Camera
92 {
93 Camera()
94 : mOrtho (false)
95 , mHorFov (10e10f)
96 , mVerFov (10e10f)
97 , mAspect (10e10f)
98 , mZNear (0.1f)
99 , mZFar (1000.f)
100 {}
102 // Name of camera
103 std::string mName;
105 // True if it is an orthografic camera
106 bool mOrtho;
108 //! Horizontal field of view in degrees
109 float mHorFov;
111 //! Vertical field of view in degrees
112 float mVerFov;
114 //! Screen aspect
115 float mAspect;
117 //! Near& far z
118 float mZNear, mZFar;
119 };
121 #define aiLightSource_AMBIENT 0xdeaddead
122 #define ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET 1e9f
124 /** A collada light source. */
125 struct Light
126 {
127 Light()
128 : mAttConstant (1.f)
129 , mAttLinear (0.f)
130 , mAttQuadratic (0.f)
131 , mFalloffAngle (180.f)
132 , mFalloffExponent (0.f)
133 , mPenumbraAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET)
134 , mOuterAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET)
135 , mIntensity (1.f)
136 {}
138 //! Type of the light source aiLightSourceType + ambient
139 unsigned int mType;
141 //! Color of the light
142 aiColor3D mColor;
144 //! Light attenuation
145 float mAttConstant,mAttLinear,mAttQuadratic;
147 //! Spot light falloff
148 float mFalloffAngle;
149 float mFalloffExponent;
151 // -----------------------------------------------------
152 // FCOLLADA extension from here
154 //! ... related stuff from maja and max extensions
155 float mPenumbraAngle;
156 float mOuterAngle;
158 //! Common light intensity
159 float mIntensity;
160 };
162 /** Short vertex index description */
163 struct InputSemanticMapEntry
164 {
165 InputSemanticMapEntry()
166 : mSet (0)
167 {}
169 //! Index of set, optional
170 unsigned int mSet;
172 //! Name of referenced vertex input
173 InputType mType;
174 };
176 /** Table to map from effect to vertex input semantics */
177 struct SemanticMappingTable
178 {
179 //! Name of material
180 std::string mMatName;
182 //! List of semantic map commands, grouped by effect semantic name
183 std::map<std::string, InputSemanticMapEntry> mMap;
185 //! For std::find
186 bool operator == (const std::string& s) const {
187 return s == mMatName;
188 }
189 };
191 /** A reference to a mesh inside a node, including materials assigned to the various subgroups.
192 * The ID refers to either a mesh or a controller which specifies the mesh
193 */
194 struct MeshInstance
195 {
196 ///< ID of the mesh or controller to be instanced
197 std::string mMeshOrController;
199 ///< Map of materials by the subgroup ID they're applied to
200 std::map<std::string, SemanticMappingTable> mMaterials;
201 };
203 /** A reference to a camera inside a node*/
204 struct CameraInstance
205 {
206 ///< ID of the camera
207 std::string mCamera;
208 };
210 /** A reference to a light inside a node*/
211 struct LightInstance
212 {
213 ///< ID of the camera
214 std::string mLight;
215 };
217 /** A reference to a node inside a node*/
218 struct NodeInstance
219 {
220 ///< ID of the node
221 std::string mNode;
222 };
224 /** A node in a scene hierarchy */
225 struct Node
226 {
227 std::string mName;
228 std::string mID;
229 std::string mSID;
230 Node* mParent;
231 std::vector<Node*> mChildren;
233 /** Operations in order to calculate the resulting transformation to parent. */
234 std::vector<Transform> mTransforms;
236 /** Meshes at this node */
237 std::vector<MeshInstance> mMeshes;
239 /** Lights at this node */
240 std::vector<LightInstance> mLights;
242 /** Cameras at this node */
243 std::vector<CameraInstance> mCameras;
245 /** Node instances at this node */
246 std::vector<NodeInstance> mNodeInstances;
248 /** Rootnodes: Name of primary camera, if any */
249 std::string mPrimaryCamera;
251 //! Constructor. Begin with a zero parent
252 Node() {
253 mParent = NULL;
254 }
256 //! Destructor: delete all children subsequently
257 ~Node() {
258 for( std::vector<Node*>::iterator it = mChildren.begin(); it != mChildren.end(); ++it)
259 delete *it;
260 }
261 };
263 /** Data source array: either floats or strings */
264 struct Data
265 {
266 bool mIsStringArray;
267 std::vector<float> mValues;
268 std::vector<std::string> mStrings;
269 };
271 /** Accessor to a data array */
272 struct Accessor
273 {
274 size_t mCount; // in number of objects
275 size_t mSize; // size of an object, in elements (floats or strings, mostly 1)
276 size_t mOffset; // in number of values
277 size_t mStride; // Stride in number of values
278 std::vector<std::string> mParams; // names of the data streams in the accessors. Empty string tells to ignore.
279 size_t mSubOffset[4]; // Suboffset inside the object for the common 4 elements. For a vector, thats XYZ, for a color RGBA and so on.
280 // For example, SubOffset[0] denotes which of the values inside the object is the vector X component.
281 std::string mSource; // URL of the source array
282 mutable const Data* mData; // Pointer to the source array, if resolved. NULL else
284 Accessor()
285 {
286 mCount = 0; mSize = 0; mOffset = 0; mStride = 0; mData = NULL;
287 mSubOffset[0] = mSubOffset[1] = mSubOffset[2] = mSubOffset[3] = 0;
288 }
289 };
291 /** A single face in a mesh */
292 struct Face
293 {
294 std::vector<size_t> mIndices;
295 };
297 /** An input channel for mesh data, referring to a single accessor */
298 struct InputChannel
299 {
300 InputType mType; // Type of the data
301 size_t mIndex; // Optional index, if multiple sets of the same data type are given
302 size_t mOffset; // Index offset in the indices array of per-face indices. Don't ask, can't explain that any better.
303 std::string mAccessor; // ID of the accessor where to read the actual values from.
304 mutable const Accessor* mResolved; // Pointer to the accessor, if resolved. NULL else
306 InputChannel() { mType = IT_Invalid; mIndex = 0; mOffset = 0; mResolved = NULL; }
307 };
309 /** Subset of a mesh with a certain material */
310 struct SubMesh
311 {
312 std::string mMaterial; ///< subgroup identifier
313 size_t mNumFaces; ///< number of faces in this submesh
314 };
316 /** Contains data for a single mesh */
317 struct Mesh
318 {
319 Mesh()
320 {
321 for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
322 mNumUVComponents[i] = 2;
323 }
325 // just to check if there's some sophisticated addressing involved...
326 // which we don't support, and therefore should warn about.
327 std::string mVertexID;
329 // Vertex data addressed by vertex indices
330 std::vector<InputChannel> mPerVertexData;
332 // actual mesh data, assembled on encounter of a <p> element. Verbose format, not indexed
333 std::vector<aiVector3D> mPositions;
334 std::vector<aiVector3D> mNormals;
335 std::vector<aiVector3D> mTangents;
336 std::vector<aiVector3D> mBitangents;
337 std::vector<aiVector3D> mTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
338 std::vector<aiColor4D> mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
340 unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS];
342 // Faces. Stored are only the number of vertices for each face.
343 // 1 == point, 2 == line, 3 == triangle, 4+ == poly
344 std::vector<size_t> mFaceSize;
346 // Position indices for all faces in the sequence given in mFaceSize -
347 // necessary for bone weight assignment
348 std::vector<size_t> mFacePosIndices;
350 // Submeshes in this mesh, each with a given material
351 std::vector<SubMesh> mSubMeshes;
352 };
354 /** Which type of primitives the ReadPrimitives() function is going to read */
355 enum PrimitiveType
356 {
357 Prim_Invalid,
358 Prim_Lines,
359 Prim_LineStrip,
360 Prim_Triangles,
361 Prim_TriStrips,
362 Prim_TriFans,
363 Prim_Polylist,
364 Prim_Polygon
365 };
367 /** A skeleton controller to deform a mesh with the use of joints */
368 struct Controller
369 {
370 // the URL of the mesh deformed by the controller.
371 std::string mMeshId;
373 // accessor URL of the joint names
374 std::string mJointNameSource;
376 ///< The bind shape matrix, as array of floats. I'm not sure what this matrix actually describes, but it can't be ignored in all cases
377 float mBindShapeMatrix[16];
379 // accessor URL of the joint inverse bind matrices
380 std::string mJointOffsetMatrixSource;
382 // input channel: joint names.
383 InputChannel mWeightInputJoints;
384 // input channel: joint weights
385 InputChannel mWeightInputWeights;
387 // Number of weights per vertex.
388 std::vector<size_t> mWeightCounts;
390 // JointIndex-WeightIndex pairs for all vertices
391 std::vector< std::pair<size_t, size_t> > mWeights;
392 };
394 /** A collada material. Pretty much the only member is a reference to an effect. */
395 struct Material
396 {
397 std::string mEffect;
398 };
400 /** Type of the effect param */
401 enum ParamType
402 {
403 Param_Sampler,
404 Param_Surface
405 };
407 /** A param for an effect. Might be of several types, but they all just refer to each other, so I summarize them */
408 struct EffectParam
409 {
410 ParamType mType;
411 std::string mReference; // to which other thing the param is referring to.
412 };
414 /** Shading type supported by the standard effect spec of Collada */
415 enum ShadeType
416 {
417 Shade_Invalid,
418 Shade_Constant,
419 Shade_Lambert,
420 Shade_Phong,
421 Shade_Blinn
422 };
424 /** Represents a texture sampler in collada */
425 struct Sampler
426 {
427 Sampler()
428 : mWrapU (true)
429 , mWrapV (true)
430 , mMirrorU ()
431 , mMirrorV ()
432 , mOp (aiTextureOp_Multiply)
433 , mUVId (UINT_MAX)
434 , mWeighting (1.f)
435 , mMixWithPrevious (1.f)
436 {}
438 /** Name of image reference
439 */
440 std::string mName;
442 /** Wrap U?
443 */
444 bool mWrapU;
446 /** Wrap V?
447 */
448 bool mWrapV;
450 /** Mirror U?
451 */
452 bool mMirrorU;
454 /** Mirror V?
455 */
456 bool mMirrorV;
458 /** Blend mode
459 */
460 aiTextureOp mOp;
462 /** UV transformation
463 */
464 aiUVTransform mTransform;
466 /** Name of source UV channel
467 */
468 std::string mUVChannel;
470 /** Resolved UV channel index or UINT_MAX if not known
471 */
472 unsigned int mUVId;
474 // OKINO/MAX3D extensions from here
475 // -------------------------------------------------------
477 /** Weighting factor
478 */
479 float mWeighting;
481 /** Mixing factor from OKINO
482 */
483 float mMixWithPrevious;
484 };
486 /** A collada effect. Can contain about anything according to the Collada spec,
487 but we limit our version to a reasonable subset. */
488 struct Effect
489 {
490 // Shading mode
491 ShadeType mShadeType;
493 // Colors
494 aiColor4D mEmissive, mAmbient, mDiffuse, mSpecular,
495 mTransparent, mReflective;
497 // Textures
498 Sampler mTexEmissive, mTexAmbient, mTexDiffuse, mTexSpecular,
499 mTexTransparent, mTexBump, mTexReflective;
501 // Scalar factory
502 float mShininess, mRefractIndex, mReflectivity;
503 float mTransparency;
505 // local params referring to each other by their SID
506 typedef std::map<std::string, Collada::EffectParam> ParamLibrary;
507 ParamLibrary mParams;
509 // MAX3D extensions
510 // ---------------------------------------------------------
511 // Double-sided?
512 bool mDoubleSided, mWireframe, mFaceted;
514 Effect()
515 : mShadeType (Shade_Phong)
516 , mEmissive ( 0, 0, 0, 1)
517 , mAmbient ( 0.1f, 0.1f, 0.1f, 1)
518 , mDiffuse ( 0.6f, 0.6f, 0.6f, 1)
519 , mSpecular ( 0.4f, 0.4f, 0.4f, 1)
520 , mTransparent ( 0, 0, 0, 1)
521 , mShininess (10.0f)
522 , mRefractIndex (1.f)
523 , mReflectivity (1.f)
524 , mTransparency (0.f)
525 , mDoubleSided (false)
526 , mWireframe (false)
527 , mFaceted (false)
528 {
529 }
530 };
532 /** An image, meaning texture */
533 struct Image
534 {
535 std::string mFileName;
537 /** If image file name is zero, embedded image data
538 */
539 std::vector<uint8_t> mImageData;
541 /** If image file name is zero, file format of
542 * embedded image data.
543 */
544 std::string mEmbeddedFormat;
546 };
548 /** An animation channel. */
549 struct AnimationChannel
550 {
551 /** URL of the data to animate. Could be about anything, but we support only the
552 * "NodeID/TransformID.SubElement" notation
553 */
554 std::string mTarget;
556 /** Source URL of the time values. Collada calls them "input". Meh. */
557 std::string mSourceTimes;
558 /** Source URL of the value values. Collada calls them "output". */
559 std::string mSourceValues;
560 };
562 /** An animation. Container for 0-x animation channels or 0-x animations */
563 struct Animation
564 {
565 /** Anim name */
566 std::string mName;
568 /** the animation channels, if any */
569 std::vector<AnimationChannel> mChannels;
571 /** the sub-animations, if any */
572 std::vector<Animation*> mSubAnims;
574 /** Destructor */
575 ~Animation()
576 {
577 for( std::vector<Animation*>::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it)
578 delete *it;
579 }
580 };
582 /** Description of a collada animation channel which has been determined to affect the current node */
583 struct ChannelEntry
584 {
585 const Collada::AnimationChannel* mChannel; ///> the source channel
586 std::string mTransformId; // the ID of the transformation step of the node which is influenced
587 size_t mTransformIndex; // Index into the node's transform chain to apply the channel to
588 size_t mSubElement; // starting index inside the transform data
590 // resolved data references
591 const Collada::Accessor* mTimeAccessor; ///> Collada accessor to the time values
592 const Collada::Data* mTimeData; ///> Source data array for the time values
593 const Collada::Accessor* mValueAccessor; ///> Collada accessor to the key value values
594 const Collada::Data* mValueData; ///> Source datat array for the key value values
596 ChannelEntry() { mChannel = NULL; mSubElement = 0; }
597 };
599 } // end of namespace Collada
600 } // end of namespace Assimp
602 #endif // AI_COLLADAHELPER_H_INC