vrshoot

view libs/assimp/LWOLoader.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 Declaration of the LWO importer class. */
42 #ifndef AI_LWOLOADER_H_INCLUDED
43 #define AI_LWOLOADER_H_INCLUDED
45 #include "assimp/types.h"
46 #include "assimp/DefaultLogger.hpp"
48 #include "LWOFileData.h"
49 #include "BaseImporter.h"
51 struct aiTexture;
52 struct aiNode;
54 namespace Assimp {
55 using namespace LWO;
57 // ---------------------------------------------------------------------------
58 /** Class to load LWO files.
59 *
60 * @note Methods named "xxxLWO2[xxx]" are used with the newer LWO2 format.
61 * Methods named "xxxLWOB[xxx]" are used with the older LWOB format.
62 * Methods named "xxxLWO[xxx]" are used with both formats.
63 * Methods named "xxx" are used to preprocess the loaded data -
64 * they aren't specific to one format version
65 */
66 // ---------------------------------------------------------------------------
67 class LWOImporter : public BaseImporter
68 {
69 public:
70 LWOImporter();
71 ~LWOImporter();
74 public:
76 // -------------------------------------------------------------------
77 /** Returns whether the class can handle the format of the given file.
78 * See BaseImporter::CanRead() for details.
79 */
80 bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
81 bool checkSig) const;
84 // -------------------------------------------------------------------
85 /** Called prior to ReadFile().
86 * The function is a request to the importer to update its configuration
87 * basing on the Importer's configuration property list.
88 */
89 void SetupProperties(const Importer* pImp);
91 protected:
93 // -------------------------------------------------------------------
94 // Get list of supported extensions
95 const aiImporterDesc* GetInfo () const;
97 // -------------------------------------------------------------------
98 /** Imports the given file into the given scene structure.
99 * See BaseImporter::InternReadFile() for details
100 */
101 void InternReadFile( const std::string& pFile, aiScene* pScene,
102 IOSystem* pIOHandler);
104 private:
106 // -------------------------------------------------------------------
107 /** Loads a LWO file in the older LWOB format (LW < 6)
108 */
109 void LoadLWOBFile();
111 // -------------------------------------------------------------------
112 /** Loads a LWO file in the newer LWO2 format (LW >= 6)
113 */
114 void LoadLWO2File();
117 // -------------------------------------------------------------------
118 /** Parsing functions used for all file format versions
119 */
120 inline void GetS0(std::string& out,unsigned int max);
121 inline float GetF4();
122 inline uint32_t GetU4();
123 inline uint16_t GetU2();
124 inline uint8_t GetU1();
127 // -------------------------------------------------------------------
128 /** Loads a surface chunk from an LWOB file
129 * @param size Maximum size to be read, in bytes.
130 */
131 void LoadLWOBSurface(unsigned int size);
133 // -------------------------------------------------------------------
134 /** Loads a surface chunk from an LWO2 file
135 * @param size Maximum size to be read, in bytes.
136 */
137 void LoadLWO2Surface(unsigned int size);
139 // -------------------------------------------------------------------
140 /** Loads a texture block from a LWO2 file.
141 * @param size Maximum size to be read, in bytes.
142 * @param head Header of the SUF.BLOK header
143 */
144 void LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader* head,
145 unsigned int size );
147 // -------------------------------------------------------------------
148 /** Loads a shader block from a LWO2 file.
149 * @param size Maximum size to be read, in bytes.
150 * @param head Header of the SUF.BLOK header
151 */
152 void LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader* head,
153 unsigned int size );
155 // -------------------------------------------------------------------
156 /** Loads an image map from a LWO2 file
157 * @param size Maximum size to be read, in bytes.
158 * @param tex Texture object to be filled
159 */
160 void LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex );
161 void LoadLWO2Gradient(unsigned int size, LWO::Texture& tex );
162 void LoadLWO2Procedural(unsigned int size, LWO::Texture& tex );
164 // loads the header - used by thethree functions above
165 void LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex );
167 // -------------------------------------------------------------------
168 /** Loads the LWO tag list from the file
169 * @param size Maximum size to be read, in bytes.
170 */
171 void LoadLWOTags(unsigned int size);
173 // -------------------------------------------------------------------
174 /** Load polygons from a POLS chunk
175 * @param length Size of the chunk
176 */
177 void LoadLWO2Polygons(unsigned int length);
178 void LoadLWOBPolygons(unsigned int length);
180 // -------------------------------------------------------------------
181 /** Load polygon tags from a PTAG chunk
182 * @param length Size of the chunk
183 */
184 void LoadLWO2PolygonTags(unsigned int length);
186 // -------------------------------------------------------------------
187 /** Load a vertex map from a VMAP/VMAD chunk
188 * @param length Size of the chunk
189 * @param perPoly Operate on per-polygon base?
190 */
191 void LoadLWO2VertexMap(unsigned int length, bool perPoly);
193 // -------------------------------------------------------------------
194 /** Load polygons from a PNTS chunk
195 * @param length Size of the chunk
196 */
197 void LoadLWOPoints(unsigned int length);
199 // -------------------------------------------------------------------
200 /** Load a clip from a CLIP chunk
201 * @param length Size of the chunk
202 */
203 void LoadLWO2Clip(unsigned int length);
205 // -------------------------------------------------------------------
206 /** Load an envelope from an EVL chunk
207 * @param length Size of the chunk
208 */
209 void LoadLWO2Envelope(unsigned int length);
211 // -------------------------------------------------------------------
212 /** Count vertices and faces in a LWOB/LWO2 file
213 */
214 void CountVertsAndFacesLWO2(unsigned int& verts,
215 unsigned int& faces,
216 uint16_t*& cursor,
217 const uint16_t* const end,
218 unsigned int max = UINT_MAX);
220 void CountVertsAndFacesLWOB(unsigned int& verts,
221 unsigned int& faces,
222 LE_NCONST uint16_t*& cursor,
223 const uint16_t* const end,
224 unsigned int max = UINT_MAX);
226 // -------------------------------------------------------------------
227 /** Read vertices and faces in a LWOB/LWO2 file
228 */
229 void CopyFaceIndicesLWO2(LWO::FaceList::iterator& it,
230 uint16_t*& cursor,
231 const uint16_t* const end);
233 // -------------------------------------------------------------------
234 void CopyFaceIndicesLWOB(LWO::FaceList::iterator& it,
235 LE_NCONST uint16_t*& cursor,
236 const uint16_t* const end,
237 unsigned int max = UINT_MAX);
239 // -------------------------------------------------------------------
240 /** Resolve the tag and surface lists that have been loaded.
241 * Generates the mMapping table.
242 */
243 void ResolveTags();
245 // -------------------------------------------------------------------
246 /** Resolve the clip list that has been loaded.
247 * Replaces clip references with real clips.
248 */
249 void ResolveClips();
251 // -------------------------------------------------------------------
252 /** Add a texture list to an output material description.
253 *
254 * @param pcMat Output material
255 * @param in Input texture list
256 * @param type Type identifier of the texture list
257 */
258 bool HandleTextures(aiMaterial* pcMat, const TextureList& in,
259 aiTextureType type);
261 // -------------------------------------------------------------------
262 /** Adjust a texture path
263 */
264 void AdjustTexturePath(std::string& out);
266 // -------------------------------------------------------------------
267 /** Convert a LWO surface description to an ASSIMP material
268 */
269 void ConvertMaterial(const LWO::Surface& surf,aiMaterial* pcMat);
272 // -------------------------------------------------------------------
273 /** Get a list of all UV/VC channels required by a specific surface.
274 *
275 * @param surf Working surface
276 * @param layer Working layer
277 * @param out Output list. The members are indices into the
278 * UV/VC channel lists of the layer
279 */
280 void FindUVChannels(/*const*/ LWO::Surface& surf,
281 LWO::SortedRep& sorted,
282 /*const*/ LWO::Layer& layer,
283 unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]);
285 // -------------------------------------------------------------------
286 char FindUVChannels(LWO::TextureList& list,
287 LWO::Layer& layer,LWO::UVChannel& uv, unsigned int next);
289 // -------------------------------------------------------------------
290 void FindVCChannels(const LWO::Surface& surf,
291 LWO::SortedRep& sorted,
292 const LWO::Layer& layer,
293 unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]);
295 // -------------------------------------------------------------------
296 /** Generate the final node graph
297 * Unused nodes are deleted.
298 * @param apcNodes Flat list of nodes
299 */
300 void GenerateNodeGraph(std::map<uint16_t,aiNode*>& apcNodes);
302 // -------------------------------------------------------------------
303 /** Add children to a node
304 * @param node Node to become a father
305 * @param parent Index of the node
306 * @param apcNodes Flat list of nodes - used nodes are set to NULL.
307 */
308 void AddChildren(aiNode* node, uint16_t parent,
309 std::vector<aiNode*>& apcNodes);
311 // -------------------------------------------------------------------
312 /** Read a variable sized integer
313 * @param inout Input and output buffer
314 */
315 int ReadVSizedIntLWO2(uint8_t*& inout);
317 // -------------------------------------------------------------------
318 /** Assign a value from a VMAP to a vertex and all vertices
319 * attached to it.
320 * @param base VMAP destination data
321 * @param numRead Number of float's to be read
322 * @param idx Absolute index of the first vertex
323 * @param data Value of the VMAP to be assigned - read numRead
324 * floats from this array.
325 */
326 void DoRecursiveVMAPAssignment(VMapEntry* base, unsigned int numRead,
327 unsigned int idx, float* data);
329 // -------------------------------------------------------------------
330 /** Compute normal vectors for a mesh
331 * @param mesh Input mesh
332 * @param smoothingGroups Smoothing-groups-per-face array
333 * @param surface Surface for the mesh
334 */
335 void ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>& smoothingGroups,
336 const LWO::Surface& surface);
339 // -------------------------------------------------------------------
340 /** Setup a new texture after the corresponding chunk was
341 * encountered in the file.
342 * @param list Texture list
343 * @param size Maximum number of bytes to be read
344 * @return Pointer to new texture
345 */
346 LWO::Texture* SetupNewTextureLWOB(LWO::TextureList& list,
347 unsigned int size);
349 protected:
351 /** true if the file is a LWO2 file*/
352 bool mIsLWO2;
354 /** true if the file is a LXOB file*/
355 bool mIsLXOB;
357 /** Temporary list of layers from the file */
358 LayerList* mLayers;
360 /** Pointer to the current layer */
361 LWO::Layer* mCurLayer;
363 /** Temporary tag list from the file */
364 TagList* mTags;
366 /** Mapping table to convert from tag to surface indices.
367 UINT_MAX indicates that a no corresponding surface is available */
368 TagMappingTable* mMapping;
370 /** Temporary surface list from the file */
371 SurfaceList* mSurfaces;
373 /** Temporary clip list from the file */
374 ClipList mClips;
376 /** Temporary envelope list from the file */
377 EnvelopeList mEnvelopes;
379 /** file buffer */
380 uint8_t* mFileBuffer;
382 /** Size of the file, in bytes */
383 unsigned int fileSize;
385 /** Output scene */
386 aiScene* pScene;
388 /** Configuration option: speed flag set? */
389 bool configSpeedFlag;
391 /** Configuration option: index of layer to be loaded */
392 unsigned int configLayerIndex;
394 /** Configuration option: name of layer to be loaded */
395 std::string configLayerName;
397 /** True if we have a named layer */
398 bool hasNamedLayer;
399 };
402 // ------------------------------------------------------------------------------------------------
403 inline float LWOImporter::GetF4()
404 {
405 float f = *((float*)mFileBuffer);mFileBuffer += 4;
406 AI_LSWAP4(f);
407 return f;
408 }
410 // ------------------------------------------------------------------------------------------------
411 inline uint32_t LWOImporter::GetU4()
412 {
413 uint32_t f = *((uint32_t*)mFileBuffer);mFileBuffer += 4;
414 AI_LSWAP4(f);
415 return f;
416 }
418 // ------------------------------------------------------------------------------------------------
419 inline uint16_t LWOImporter::GetU2()
420 {
421 uint16_t f = *((uint16_t*)mFileBuffer);mFileBuffer += 2;
422 AI_LSWAP2(f);
423 return f;
424 }
426 // ------------------------------------------------------------------------------------------------
427 inline uint8_t LWOImporter::GetU1()
428 {
429 return *mFileBuffer++;
430 }
432 // ------------------------------------------------------------------------------------------------
433 inline int LWOImporter::ReadVSizedIntLWO2(uint8_t*& inout)
434 {
435 int i;
436 int c = *inout;inout++;
437 if(c != 0xFF)
438 {
439 i = c << 8;
440 c = *inout;inout++;
441 i |= c;
442 }
443 else
444 {
445 c = *inout;inout++;
446 i = c << 16;
447 c = *inout;inout++;
448 i |= c << 8;
449 c = *inout;inout++;
450 i |= c;
451 }
452 return i;
453 }
455 // ------------------------------------------------------------------------------------------------
456 inline void LWOImporter::GetS0(std::string& out,unsigned int max)
457 {
458 unsigned int iCursor = 0;
459 const char*sz = (const char*)mFileBuffer;
460 while (*mFileBuffer)
461 {
462 if (++iCursor > max)
463 {
464 DefaultLogger::get()->warn("LWO: Invalid file, string is is too long");
465 break;
466 }
467 ++mFileBuffer;
468 }
469 size_t len = (size_t) ((const char*)mFileBuffer-sz);
470 out = std::string(sz,len);
471 mFileBuffer += (len&0x1 ? 1 : 2);
472 }
476 } // end of namespace Assimp
478 #endif // AI_LWOIMPORTER_H_INCLUDED