vrshoot

view libs/assimp/3DSHelper.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 /*
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 Defines helper data structures for the import of 3DS files */
43 #ifndef AI_3DSFILEHELPER_H_INC
44 #define AI_3DSFILEHELPER_H_INC
47 #include "SpatialSort.h"
48 #include "SmoothingGroups.h"
50 namespace Assimp {
51 namespace D3DS {
53 #include "assimp/Compiler/pushpack1.h"
55 // ---------------------------------------------------------------------------
56 /** Discreet3DS class: Helper class for loading 3ds files. Defines chunks
57 * and data structures.
58 */
59 class Discreet3DS
60 {
61 private:
62 inline Discreet3DS() {}
64 public:
66 //! data structure for a single chunk in a .3ds file
67 struct Chunk
68 {
69 uint16_t Flag;
70 uint32_t Size;
71 } PACK_STRUCT;
74 //! Used for shading field in material3ds structure
75 //! From AutoDesk 3ds SDK
76 typedef enum
77 {
78 // translated to gouraud shading with wireframe active
79 Wire = 0x0,
81 // if this material is set, no vertex normals will
82 // be calculated for the model. Face normals + gouraud
83 Flat = 0x1,
85 // standard gouraud shading
86 Gouraud = 0x2,
88 // phong shading
89 Phong = 0x3,
91 // cooktorrance or anistropic phong shading ...
92 // the exact meaning is unknown, if you know it
93 // feel free to tell me ;-)
94 Metal = 0x4,
96 // required by the ASE loader
97 Blinn = 0x5
98 } shadetype3ds;
100 // Flags for animated keys
101 enum
102 {
103 KEY_USE_TENS = 0x1,
104 KEY_USE_CONT = 0x2,
105 KEY_USE_BIAS = 0x4,
106 KEY_USE_EASE_TO = 0x8,
107 KEY_USE_EASE_FROM = 0x10
108 } ;
110 enum
111 {
113 // ********************************************************************
114 // Basic chunks which can be found everywhere in the file
115 CHUNK_VERSION = 0x0002,
116 CHUNK_RGBF = 0x0010, // float4 R; float4 G; float4 B
117 CHUNK_RGBB = 0x0011, // int1 R; int1 G; int B
119 // Linear color values (gamma = 2.2?)
120 CHUNK_LINRGBF = 0x0013, // float4 R; float4 G; float4 B
121 CHUNK_LINRGBB = 0x0012, // int1 R; int1 G; int B
123 CHUNK_PERCENTW = 0x0030, // int2 percentage
124 CHUNK_PERCENTF = 0x0031, // float4 percentage
125 // ********************************************************************
127 // Prj master chunk
128 CHUNK_PRJ = 0xC23D,
130 // MDLI master chunk
131 CHUNK_MLI = 0x3DAA,
133 // Primary main chunk of the .3ds file
134 CHUNK_MAIN = 0x4D4D,
136 // Mesh main chunk
137 CHUNK_OBJMESH = 0x3D3D,
139 // Specifies the background color of the .3ds file
140 // This is passed through the material system for
141 // viewing purposes.
142 CHUNK_BKGCOLOR = 0x1200,
144 // Specifies the ambient base color of the scene.
145 // This is added to all materials in the file
146 CHUNK_AMBCOLOR = 0x2100,
148 // Specifies the background image for the whole scene
149 // This value is passed through the material system
150 // to the viewer
151 CHUNK_BIT_MAP = 0x1100,
152 CHUNK_BIT_MAP_EXISTS = 0x1101,
154 // ********************************************************************
155 // Viewport related stuff. Ignored
156 CHUNK_DEFAULT_VIEW = 0x3000,
157 CHUNK_VIEW_TOP = 0x3010,
158 CHUNK_VIEW_BOTTOM = 0x3020,
159 CHUNK_VIEW_LEFT = 0x3030,
160 CHUNK_VIEW_RIGHT = 0x3040,
161 CHUNK_VIEW_FRONT = 0x3050,
162 CHUNK_VIEW_BACK = 0x3060,
163 CHUNK_VIEW_USER = 0x3070,
164 CHUNK_VIEW_CAMERA = 0x3080,
165 // ********************************************************************
167 // Mesh chunks
168 CHUNK_OBJBLOCK = 0x4000,
169 CHUNK_TRIMESH = 0x4100,
170 CHUNK_VERTLIST = 0x4110,
171 CHUNK_VERTFLAGS = 0x4111,
172 CHUNK_FACELIST = 0x4120,
173 CHUNK_FACEMAT = 0x4130,
174 CHUNK_MAPLIST = 0x4140,
175 CHUNK_SMOOLIST = 0x4150,
176 CHUNK_TRMATRIX = 0x4160,
177 CHUNK_MESHCOLOR = 0x4165,
178 CHUNK_TXTINFO = 0x4170,
179 CHUNK_LIGHT = 0x4600,
180 CHUNK_CAMERA = 0x4700,
181 CHUNK_HIERARCHY = 0x4F00,
183 // Specifies the global scaling factor. This is applied
184 // to the root node's transformation matrix
185 CHUNK_MASTER_SCALE = 0x0100,
187 // ********************************************************************
188 // Material chunks
189 CHUNK_MAT_MATERIAL = 0xAFFF,
191 // asciiz containing the name of the material
192 CHUNK_MAT_MATNAME = 0xA000,
193 CHUNK_MAT_AMBIENT = 0xA010, // followed by color chunk
194 CHUNK_MAT_DIFFUSE = 0xA020, // followed by color chunk
195 CHUNK_MAT_SPECULAR = 0xA030, // followed by color chunk
197 // Specifies the shininess of the material
198 // followed by percentage chunk
199 CHUNK_MAT_SHININESS = 0xA040,
200 CHUNK_MAT_SHININESS_PERCENT = 0xA041 ,
202 // Specifies the shading mode to be used
203 // followed by a short
204 CHUNK_MAT_SHADING = 0xA100,
206 // NOTE: Emissive color (self illumination) seems not
207 // to be a color but a single value, type is unknown.
208 // Make the parser accept both of them.
209 // followed by percentage chunk (?)
210 CHUNK_MAT_SELF_ILLUM = 0xA080,
212 // Always followed by percentage chunk (?)
213 CHUNK_MAT_SELF_ILPCT = 0xA084,
215 // Always followed by percentage chunk
216 CHUNK_MAT_TRANSPARENCY = 0xA050,
218 // Diffuse texture channel 0
219 CHUNK_MAT_TEXTURE = 0xA200,
221 // Contains opacity information for each texel
222 CHUNK_MAT_OPACMAP = 0xA210,
224 // Contains a reflection map to be used to reflect
225 // the environment. This is partially supported.
226 CHUNK_MAT_REFLMAP = 0xA220,
228 // Self Illumination map (emissive colors)
229 CHUNK_MAT_SELFIMAP = 0xA33d,
231 // Bumpmap. Not specified whether it is a heightmap
232 // or a normal map. Assme it is a heightmap since
233 // artist normally prefer this format.
234 CHUNK_MAT_BUMPMAP = 0xA230,
236 // Specular map. Seems to influence the specular color
237 CHUNK_MAT_SPECMAP = 0xA204,
239 // Holds shininess data.
240 CHUNK_MAT_MAT_SHINMAP = 0xA33C,
242 // Scaling in U/V direction.
243 // (need to gen separate UV coordinate set
244 // and do this by hand)
245 CHUNK_MAT_MAP_USCALE = 0xA354,
246 CHUNK_MAT_MAP_VSCALE = 0xA356,
248 // Translation in U/V direction.
249 // (need to gen separate UV coordinate set
250 // and do this by hand)
251 CHUNK_MAT_MAP_UOFFSET = 0xA358,
252 CHUNK_MAT_MAP_VOFFSET = 0xA35a,
254 // UV-coordinates rotation around the z-axis
255 // Assumed to be in radians.
256 CHUNK_MAT_MAP_ANG = 0xA35C,
258 // Tiling flags for 3DS files
259 CHUNK_MAT_MAP_TILING = 0xa351,
261 // Specifies the file name of a texture
262 CHUNK_MAPFILE = 0xA300,
264 // Specifies whether a materail requires two-sided rendering
265 CHUNK_MAT_TWO_SIDE = 0xA081,
266 // ********************************************************************
268 // Main keyframer chunk. Contains translation/rotation/scaling data
269 CHUNK_KEYFRAMER = 0xB000,
271 // Supported sub chunks
272 CHUNK_TRACKINFO = 0xB002,
273 CHUNK_TRACKOBJNAME = 0xB010,
274 CHUNK_TRACKDUMMYOBJNAME = 0xB011,
275 CHUNK_TRACKPIVOT = 0xB013,
276 CHUNK_TRACKPOS = 0xB020,
277 CHUNK_TRACKROTATE = 0xB021,
278 CHUNK_TRACKSCALE = 0xB022,
280 // ********************************************************************
281 // Keyframes for various other stuff in the file
282 // Partially ignored
283 CHUNK_AMBIENTKEY = 0xB001,
284 CHUNK_TRACKMORPH = 0xB026,
285 CHUNK_TRACKHIDE = 0xB029,
286 CHUNK_OBJNUMBER = 0xB030,
287 CHUNK_TRACKCAMERA = 0xB003,
288 CHUNK_TRACKFOV = 0xB023,
289 CHUNK_TRACKROLL = 0xB024,
290 CHUNK_TRACKCAMTGT = 0xB004,
291 CHUNK_TRACKLIGHT = 0xB005,
292 CHUNK_TRACKLIGTGT = 0xB006,
293 CHUNK_TRACKSPOTL = 0xB007,
294 CHUNK_FRAMES = 0xB008,
295 // ********************************************************************
297 // light sub-chunks
298 CHUNK_DL_OFF = 0x4620,
299 CHUNK_DL_OUTER_RANGE = 0x465A,
300 CHUNK_DL_INNER_RANGE = 0x4659,
301 CHUNK_DL_MULTIPLIER = 0x465B,
302 CHUNK_DL_EXCLUDE = 0x4654,
303 CHUNK_DL_ATTENUATE = 0x4625,
304 CHUNK_DL_SPOTLIGHT = 0x4610,
306 // camera sub-chunks
307 CHUNK_CAM_RANGES = 0x4720
308 };
309 };
311 // ---------------------------------------------------------------------------
312 /** Helper structure representing a 3ds mesh face */
313 struct Face : public FaceWithSmoothingGroup
314 {
315 };
317 // ---------------------------------------------------------------------------
318 /** Helper structure representing a texture */
319 struct Texture
320 {
321 //! Default constructor
322 Texture()
323 : mOffsetU (0.0f)
324 , mOffsetV (0.0f)
325 , mScaleU (1.0f)
326 , mScaleV (1.0f)
327 , mRotation (0.0f)
328 , mMapMode (aiTextureMapMode_Wrap)
329 , iUVSrc (0)
330 {
331 mTextureBlend = get_qnan();
332 }
334 //! Specifies the blend factor for the texture
335 float mTextureBlend;
337 //! Specifies the filename of the texture
338 std::string mMapName;
340 //! Specifies texture coordinate offsets/scaling/rotations
341 float mOffsetU;
342 float mOffsetV;
343 float mScaleU;
344 float mScaleV;
345 float mRotation;
347 //! Specifies the mapping mode to be used for the texture
348 aiTextureMapMode mMapMode;
350 //! Used internally
351 bool bPrivate;
352 int iUVSrc;
353 };
355 #include "assimp/Compiler/poppack1.h"
357 // ---------------------------------------------------------------------------
358 /** Helper structure representing a 3ds material */
359 struct Material
360 {
361 //! Default constructor. Builds a default name for the material
362 Material()
363 :
364 mDiffuse (0.6f,0.6f,0.6f), // FIX ... we won't want object to be black
365 mSpecularExponent (0.0f),
366 mShininessStrength (1.0f),
367 mShading(Discreet3DS::Gouraud),
368 mTransparency (1.0f),
369 mBumpHeight (1.0f),
370 mTwoSided (false)
371 {
372 static int iCnt = 0;
374 char szTemp[128];
375 sprintf(szTemp,"UNNAMED_%i",iCnt++);
376 mName = szTemp;
377 }
379 //! Name of the material
380 std::string mName;
381 //! Diffuse color of the material
382 aiColor3D mDiffuse;
383 //! Specular exponent
384 float mSpecularExponent;
385 //! Shininess strength, in percent
386 float mShininessStrength;
387 //! Specular color of the material
388 aiColor3D mSpecular;
389 //! Ambient color of the material
390 aiColor3D mAmbient;
391 //! Shading type to be used
392 Discreet3DS::shadetype3ds mShading;
393 //! Opacity of the material
394 float mTransparency;
395 //! Diffuse texture channel
396 Texture sTexDiffuse;
397 //! Opacity texture channel
398 Texture sTexOpacity;
399 //! Specular texture channel
400 Texture sTexSpecular;
401 //! Reflective texture channel
402 Texture sTexReflective;
403 //! Bump texture channel
404 Texture sTexBump;
405 //! Emissive texture channel
406 Texture sTexEmissive;
407 //! Shininess texture channel
408 Texture sTexShininess;
409 //! Scaling factor for the bump values
410 float mBumpHeight;
411 //! Emissive color
412 aiColor3D mEmissive;
413 //! Ambient texture channel
414 //! (used by the ASE format)
415 Texture sTexAmbient;
416 //! True if the material must be rendered from two sides
417 bool mTwoSided;
418 };
420 // ---------------------------------------------------------------------------
421 /** Helper structure to represent a 3ds file mesh */
422 struct Mesh : public MeshWithSmoothingGroups<D3DS::Face>
423 {
424 //! Default constructor
425 Mesh()
426 {
427 static int iCnt = 0;
429 // Generate a default name for the mesh
430 char szTemp[128];
431 ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
432 mName = szTemp;
433 }
435 //! Name of the mesh
436 std::string mName;
438 //! Texture coordinates
439 std::vector<aiVector3D> mTexCoords;
441 //! Face materials
442 std::vector<unsigned int> mFaceMaterials;
444 //! Local transformation matrix
445 aiMatrix4x4 mMat;
446 };
448 // ---------------------------------------------------------------------------
449 /** Float key - quite similar to aiVectorKey and aiQuatKey. Both are in the
450 C-API, so it would be difficult to make them a template. */
451 struct aiFloatKey
452 {
453 double mTime; ///< The time of this key
454 float mValue; ///< The value of this key
456 #ifdef __cplusplus
458 // time is not compared
459 bool operator == (const aiFloatKey& o) const
460 {return o.mValue == this->mValue;}
462 bool operator != (const aiFloatKey& o) const
463 {return o.mValue != this->mValue;}
465 // Only time is compared. This operator is defined
466 // for use with std::sort
467 bool operator < (const aiFloatKey& o) const
468 {return mTime < o.mTime;}
470 bool operator > (const aiFloatKey& o) const
471 {return mTime < o.mTime;}
473 #endif
474 };
476 // ---------------------------------------------------------------------------
477 /** Helper structure to represent a 3ds file node */
478 struct Node
479 {
480 Node()
482 : mHierarchyPos (0)
483 , mHierarchyIndex (0)
485 {
486 static int iCnt = 0;
488 // Generate a default name for the node
489 char szTemp[128];
490 ::sprintf(szTemp,"UNNAMED_%i",iCnt++);
491 mName = szTemp;
493 aRotationKeys.reserve (20);
494 aPositionKeys.reserve (20);
495 aScalingKeys.reserve (20);
496 }
498 ~Node()
499 {
500 for (unsigned int i = 0; i < mChildren.size();++i)
501 delete mChildren[i];
502 }
504 //! Pointer to the parent node
505 Node* mParent;
507 //! Holds all child nodes
508 std::vector<Node*> mChildren;
510 //! Name of the node
511 std::string mName;
513 //! Dummy nodes: real name to be combined with the $$$DUMMY
514 std::string mDummyName;
516 //! Position of the node in the hierarchy (tree depth)
517 int16_t mHierarchyPos;
519 //! Index of the node
520 int16_t mHierarchyIndex;
522 //! Rotation keys loaded from the file
523 std::vector<aiQuatKey> aRotationKeys;
525 //! Position keys loaded from the file
526 std::vector<aiVectorKey> aPositionKeys;
528 //! Scaling keys loaded from the file
529 std::vector<aiVectorKey> aScalingKeys;
532 // For target lights (spot lights and directional lights):
533 // The position of the target
534 std::vector< aiVectorKey > aTargetPositionKeys;
536 // For cameras: the camera roll angle
537 std::vector< aiFloatKey > aCameraRollKeys;
539 //! Pivot position loaded from the file
540 aiVector3D vPivot;
542 //! Add a child node, setup the right parent node for it
543 //! \param pc Node to be 'adopted'
544 inline Node& push_back(Node* pc)
545 {
546 mChildren.push_back(pc);
547 pc->mParent = this;
548 return *this;
549 }
550 };
551 // ---------------------------------------------------------------------------
552 /** Helper structure analogue to aiScene */
553 struct Scene
554 {
555 //! List of all materials loaded
556 //! NOTE: 3ds references materials globally
557 std::vector<Material> mMaterials;
559 //! List of all meshes loaded
560 std::vector<Mesh> mMeshes;
562 //! List of all cameras loaded
563 std::vector<aiCamera*> mCameras;
565 //! List of all lights loaded
566 std::vector<aiLight*> mLights;
568 //! Pointer to the root node of the scene
569 // --- moved to main class
570 // Node* pcRootNode;
571 };
574 } // end of namespace D3DS
575 } // end of namespace Assimp
577 #endif // AI_XFILEHELPER_H_INC