vrshoot
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/LWOLoader.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,478 @@ 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 Declaration of the LWO importer class. */ 1.45 +#ifndef AI_LWOLOADER_H_INCLUDED 1.46 +#define AI_LWOLOADER_H_INCLUDED 1.47 + 1.48 +#include "assimp/types.h" 1.49 +#include "assimp/DefaultLogger.hpp" 1.50 + 1.51 +#include "LWOFileData.h" 1.52 +#include "BaseImporter.h" 1.53 + 1.54 +struct aiTexture; 1.55 +struct aiNode; 1.56 + 1.57 +namespace Assimp { 1.58 +using namespace LWO; 1.59 + 1.60 +// --------------------------------------------------------------------------- 1.61 +/** Class to load LWO files. 1.62 + * 1.63 + * @note Methods named "xxxLWO2[xxx]" are used with the newer LWO2 format. 1.64 + * Methods named "xxxLWOB[xxx]" are used with the older LWOB format. 1.65 + * Methods named "xxxLWO[xxx]" are used with both formats. 1.66 + * Methods named "xxx" are used to preprocess the loaded data - 1.67 + * they aren't specific to one format version 1.68 +*/ 1.69 +// --------------------------------------------------------------------------- 1.70 +class LWOImporter : public BaseImporter 1.71 +{ 1.72 +public: 1.73 + LWOImporter(); 1.74 + ~LWOImporter(); 1.75 + 1.76 + 1.77 +public: 1.78 + 1.79 + // ------------------------------------------------------------------- 1.80 + /** Returns whether the class can handle the format of the given file. 1.81 + * See BaseImporter::CanRead() for details. 1.82 + */ 1.83 + bool CanRead( const std::string& pFile, IOSystem* pIOHandler, 1.84 + bool checkSig) const; 1.85 + 1.86 + 1.87 + // ------------------------------------------------------------------- 1.88 + /** Called prior to ReadFile(). 1.89 + * The function is a request to the importer to update its configuration 1.90 + * basing on the Importer's configuration property list. 1.91 + */ 1.92 + void SetupProperties(const Importer* pImp); 1.93 + 1.94 +protected: 1.95 + 1.96 + // ------------------------------------------------------------------- 1.97 + // Get list of supported extensions 1.98 + const aiImporterDesc* GetInfo () const; 1.99 + 1.100 + // ------------------------------------------------------------------- 1.101 + /** Imports the given file into the given scene structure. 1.102 + * See BaseImporter::InternReadFile() for details 1.103 + */ 1.104 + void InternReadFile( const std::string& pFile, aiScene* pScene, 1.105 + IOSystem* pIOHandler); 1.106 + 1.107 +private: 1.108 + 1.109 + // ------------------------------------------------------------------- 1.110 + /** Loads a LWO file in the older LWOB format (LW < 6) 1.111 + */ 1.112 + void LoadLWOBFile(); 1.113 + 1.114 + // ------------------------------------------------------------------- 1.115 + /** Loads a LWO file in the newer LWO2 format (LW >= 6) 1.116 + */ 1.117 + void LoadLWO2File(); 1.118 + 1.119 + 1.120 + // ------------------------------------------------------------------- 1.121 + /** Parsing functions used for all file format versions 1.122 + */ 1.123 + inline void GetS0(std::string& out,unsigned int max); 1.124 + inline float GetF4(); 1.125 + inline uint32_t GetU4(); 1.126 + inline uint16_t GetU2(); 1.127 + inline uint8_t GetU1(); 1.128 + 1.129 + 1.130 + // ------------------------------------------------------------------- 1.131 + /** Loads a surface chunk from an LWOB file 1.132 + * @param size Maximum size to be read, in bytes. 1.133 + */ 1.134 + void LoadLWOBSurface(unsigned int size); 1.135 + 1.136 + // ------------------------------------------------------------------- 1.137 + /** Loads a surface chunk from an LWO2 file 1.138 + * @param size Maximum size to be read, in bytes. 1.139 + */ 1.140 + void LoadLWO2Surface(unsigned int size); 1.141 + 1.142 + // ------------------------------------------------------------------- 1.143 + /** Loads a texture block from a LWO2 file. 1.144 + * @param size Maximum size to be read, in bytes. 1.145 + * @param head Header of the SUF.BLOK header 1.146 + */ 1.147 + void LoadLWO2TextureBlock(LE_NCONST IFF::SubChunkHeader* head, 1.148 + unsigned int size ); 1.149 + 1.150 + // ------------------------------------------------------------------- 1.151 + /** Loads a shader block from a LWO2 file. 1.152 + * @param size Maximum size to be read, in bytes. 1.153 + * @param head Header of the SUF.BLOK header 1.154 + */ 1.155 + void LoadLWO2ShaderBlock(LE_NCONST IFF::SubChunkHeader* head, 1.156 + unsigned int size ); 1.157 + 1.158 + // ------------------------------------------------------------------- 1.159 + /** Loads an image map from a LWO2 file 1.160 + * @param size Maximum size to be read, in bytes. 1.161 + * @param tex Texture object to be filled 1.162 + */ 1.163 + void LoadLWO2ImageMap(unsigned int size, LWO::Texture& tex ); 1.164 + void LoadLWO2Gradient(unsigned int size, LWO::Texture& tex ); 1.165 + void LoadLWO2Procedural(unsigned int size, LWO::Texture& tex ); 1.166 + 1.167 + // loads the header - used by thethree functions above 1.168 + void LoadLWO2TextureHeader(unsigned int size, LWO::Texture& tex ); 1.169 + 1.170 + // ------------------------------------------------------------------- 1.171 + /** Loads the LWO tag list from the file 1.172 + * @param size Maximum size to be read, in bytes. 1.173 + */ 1.174 + void LoadLWOTags(unsigned int size); 1.175 + 1.176 + // ------------------------------------------------------------------- 1.177 + /** Load polygons from a POLS chunk 1.178 + * @param length Size of the chunk 1.179 + */ 1.180 + void LoadLWO2Polygons(unsigned int length); 1.181 + void LoadLWOBPolygons(unsigned int length); 1.182 + 1.183 + // ------------------------------------------------------------------- 1.184 + /** Load polygon tags from a PTAG chunk 1.185 + * @param length Size of the chunk 1.186 + */ 1.187 + void LoadLWO2PolygonTags(unsigned int length); 1.188 + 1.189 + // ------------------------------------------------------------------- 1.190 + /** Load a vertex map from a VMAP/VMAD chunk 1.191 + * @param length Size of the chunk 1.192 + * @param perPoly Operate on per-polygon base? 1.193 + */ 1.194 + void LoadLWO2VertexMap(unsigned int length, bool perPoly); 1.195 + 1.196 + // ------------------------------------------------------------------- 1.197 + /** Load polygons from a PNTS chunk 1.198 + * @param length Size of the chunk 1.199 + */ 1.200 + void LoadLWOPoints(unsigned int length); 1.201 + 1.202 + // ------------------------------------------------------------------- 1.203 + /** Load a clip from a CLIP chunk 1.204 + * @param length Size of the chunk 1.205 + */ 1.206 + void LoadLWO2Clip(unsigned int length); 1.207 + 1.208 + // ------------------------------------------------------------------- 1.209 + /** Load an envelope from an EVL chunk 1.210 + * @param length Size of the chunk 1.211 + */ 1.212 + void LoadLWO2Envelope(unsigned int length); 1.213 + 1.214 + // ------------------------------------------------------------------- 1.215 + /** Count vertices and faces in a LWOB/LWO2 file 1.216 + */ 1.217 + void CountVertsAndFacesLWO2(unsigned int& verts, 1.218 + unsigned int& faces, 1.219 + uint16_t*& cursor, 1.220 + const uint16_t* const end, 1.221 + unsigned int max = UINT_MAX); 1.222 + 1.223 + void CountVertsAndFacesLWOB(unsigned int& verts, 1.224 + unsigned int& faces, 1.225 + LE_NCONST uint16_t*& cursor, 1.226 + const uint16_t* const end, 1.227 + unsigned int max = UINT_MAX); 1.228 + 1.229 + // ------------------------------------------------------------------- 1.230 + /** Read vertices and faces in a LWOB/LWO2 file 1.231 + */ 1.232 + void CopyFaceIndicesLWO2(LWO::FaceList::iterator& it, 1.233 + uint16_t*& cursor, 1.234 + const uint16_t* const end); 1.235 + 1.236 + // ------------------------------------------------------------------- 1.237 + void CopyFaceIndicesLWOB(LWO::FaceList::iterator& it, 1.238 + LE_NCONST uint16_t*& cursor, 1.239 + const uint16_t* const end, 1.240 + unsigned int max = UINT_MAX); 1.241 + 1.242 + // ------------------------------------------------------------------- 1.243 + /** Resolve the tag and surface lists that have been loaded. 1.244 + * Generates the mMapping table. 1.245 + */ 1.246 + void ResolveTags(); 1.247 + 1.248 + // ------------------------------------------------------------------- 1.249 + /** Resolve the clip list that has been loaded. 1.250 + * Replaces clip references with real clips. 1.251 + */ 1.252 + void ResolveClips(); 1.253 + 1.254 + // ------------------------------------------------------------------- 1.255 + /** Add a texture list to an output material description. 1.256 + * 1.257 + * @param pcMat Output material 1.258 + * @param in Input texture list 1.259 + * @param type Type identifier of the texture list 1.260 + */ 1.261 + bool HandleTextures(aiMaterial* pcMat, const TextureList& in, 1.262 + aiTextureType type); 1.263 + 1.264 + // ------------------------------------------------------------------- 1.265 + /** Adjust a texture path 1.266 + */ 1.267 + void AdjustTexturePath(std::string& out); 1.268 + 1.269 + // ------------------------------------------------------------------- 1.270 + /** Convert a LWO surface description to an ASSIMP material 1.271 + */ 1.272 + void ConvertMaterial(const LWO::Surface& surf,aiMaterial* pcMat); 1.273 + 1.274 + 1.275 + // ------------------------------------------------------------------- 1.276 + /** Get a list of all UV/VC channels required by a specific surface. 1.277 + * 1.278 + * @param surf Working surface 1.279 + * @param layer Working layer 1.280 + * @param out Output list. The members are indices into the 1.281 + * UV/VC channel lists of the layer 1.282 + */ 1.283 + void FindUVChannels(/*const*/ LWO::Surface& surf, 1.284 + LWO::SortedRep& sorted, 1.285 + /*const*/ LWO::Layer& layer, 1.286 + unsigned int out[AI_MAX_NUMBER_OF_TEXTURECOORDS]); 1.287 + 1.288 + // ------------------------------------------------------------------- 1.289 + char FindUVChannels(LWO::TextureList& list, 1.290 + LWO::Layer& layer,LWO::UVChannel& uv, unsigned int next); 1.291 + 1.292 + // ------------------------------------------------------------------- 1.293 + void FindVCChannels(const LWO::Surface& surf, 1.294 + LWO::SortedRep& sorted, 1.295 + const LWO::Layer& layer, 1.296 + unsigned int out[AI_MAX_NUMBER_OF_COLOR_SETS]); 1.297 + 1.298 + // ------------------------------------------------------------------- 1.299 + /** Generate the final node graph 1.300 + * Unused nodes are deleted. 1.301 + * @param apcNodes Flat list of nodes 1.302 + */ 1.303 + void GenerateNodeGraph(std::map<uint16_t,aiNode*>& apcNodes); 1.304 + 1.305 + // ------------------------------------------------------------------- 1.306 + /** Add children to a node 1.307 + * @param node Node to become a father 1.308 + * @param parent Index of the node 1.309 + * @param apcNodes Flat list of nodes - used nodes are set to NULL. 1.310 + */ 1.311 + void AddChildren(aiNode* node, uint16_t parent, 1.312 + std::vector<aiNode*>& apcNodes); 1.313 + 1.314 + // ------------------------------------------------------------------- 1.315 + /** Read a variable sized integer 1.316 + * @param inout Input and output buffer 1.317 + */ 1.318 + int ReadVSizedIntLWO2(uint8_t*& inout); 1.319 + 1.320 + // ------------------------------------------------------------------- 1.321 + /** Assign a value from a VMAP to a vertex and all vertices 1.322 + * attached to it. 1.323 + * @param base VMAP destination data 1.324 + * @param numRead Number of float's to be read 1.325 + * @param idx Absolute index of the first vertex 1.326 + * @param data Value of the VMAP to be assigned - read numRead 1.327 + * floats from this array. 1.328 + */ 1.329 + void DoRecursiveVMAPAssignment(VMapEntry* base, unsigned int numRead, 1.330 + unsigned int idx, float* data); 1.331 + 1.332 + // ------------------------------------------------------------------- 1.333 + /** Compute normal vectors for a mesh 1.334 + * @param mesh Input mesh 1.335 + * @param smoothingGroups Smoothing-groups-per-face array 1.336 + * @param surface Surface for the mesh 1.337 + */ 1.338 + void ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>& smoothingGroups, 1.339 + const LWO::Surface& surface); 1.340 + 1.341 + 1.342 + // ------------------------------------------------------------------- 1.343 + /** Setup a new texture after the corresponding chunk was 1.344 + * encountered in the file. 1.345 + * @param list Texture list 1.346 + * @param size Maximum number of bytes to be read 1.347 + * @return Pointer to new texture 1.348 + */ 1.349 + LWO::Texture* SetupNewTextureLWOB(LWO::TextureList& list, 1.350 + unsigned int size); 1.351 + 1.352 +protected: 1.353 + 1.354 + /** true if the file is a LWO2 file*/ 1.355 + bool mIsLWO2; 1.356 + 1.357 + /** true if the file is a LXOB file*/ 1.358 + bool mIsLXOB; 1.359 + 1.360 + /** Temporary list of layers from the file */ 1.361 + LayerList* mLayers; 1.362 + 1.363 + /** Pointer to the current layer */ 1.364 + LWO::Layer* mCurLayer; 1.365 + 1.366 + /** Temporary tag list from the file */ 1.367 + TagList* mTags; 1.368 + 1.369 + /** Mapping table to convert from tag to surface indices. 1.370 + UINT_MAX indicates that a no corresponding surface is available */ 1.371 + TagMappingTable* mMapping; 1.372 + 1.373 + /** Temporary surface list from the file */ 1.374 + SurfaceList* mSurfaces; 1.375 + 1.376 + /** Temporary clip list from the file */ 1.377 + ClipList mClips; 1.378 + 1.379 + /** Temporary envelope list from the file */ 1.380 + EnvelopeList mEnvelopes; 1.381 + 1.382 + /** file buffer */ 1.383 + uint8_t* mFileBuffer; 1.384 + 1.385 + /** Size of the file, in bytes */ 1.386 + unsigned int fileSize; 1.387 + 1.388 + /** Output scene */ 1.389 + aiScene* pScene; 1.390 + 1.391 + /** Configuration option: speed flag set? */ 1.392 + bool configSpeedFlag; 1.393 + 1.394 + /** Configuration option: index of layer to be loaded */ 1.395 + unsigned int configLayerIndex; 1.396 + 1.397 + /** Configuration option: name of layer to be loaded */ 1.398 + std::string configLayerName; 1.399 + 1.400 + /** True if we have a named layer */ 1.401 + bool hasNamedLayer; 1.402 +}; 1.403 + 1.404 + 1.405 +// ------------------------------------------------------------------------------------------------ 1.406 +inline float LWOImporter::GetF4() 1.407 +{ 1.408 + float f = *((float*)mFileBuffer);mFileBuffer += 4; 1.409 + AI_LSWAP4(f); 1.410 + return f; 1.411 +} 1.412 + 1.413 +// ------------------------------------------------------------------------------------------------ 1.414 +inline uint32_t LWOImporter::GetU4() 1.415 +{ 1.416 + uint32_t f = *((uint32_t*)mFileBuffer);mFileBuffer += 4; 1.417 + AI_LSWAP4(f); 1.418 + return f; 1.419 +} 1.420 + 1.421 +// ------------------------------------------------------------------------------------------------ 1.422 +inline uint16_t LWOImporter::GetU2() 1.423 +{ 1.424 + uint16_t f = *((uint16_t*)mFileBuffer);mFileBuffer += 2; 1.425 + AI_LSWAP2(f); 1.426 + return f; 1.427 +} 1.428 + 1.429 +// ------------------------------------------------------------------------------------------------ 1.430 +inline uint8_t LWOImporter::GetU1() 1.431 +{ 1.432 + return *mFileBuffer++; 1.433 +} 1.434 + 1.435 +// ------------------------------------------------------------------------------------------------ 1.436 +inline int LWOImporter::ReadVSizedIntLWO2(uint8_t*& inout) 1.437 +{ 1.438 + int i; 1.439 + int c = *inout;inout++; 1.440 + if(c != 0xFF) 1.441 + { 1.442 + i = c << 8; 1.443 + c = *inout;inout++; 1.444 + i |= c; 1.445 + } 1.446 + else 1.447 + { 1.448 + c = *inout;inout++; 1.449 + i = c << 16; 1.450 + c = *inout;inout++; 1.451 + i |= c << 8; 1.452 + c = *inout;inout++; 1.453 + i |= c; 1.454 + } 1.455 + return i; 1.456 +} 1.457 + 1.458 +// ------------------------------------------------------------------------------------------------ 1.459 +inline void LWOImporter::GetS0(std::string& out,unsigned int max) 1.460 +{ 1.461 + unsigned int iCursor = 0; 1.462 + const char*sz = (const char*)mFileBuffer; 1.463 + while (*mFileBuffer) 1.464 + { 1.465 + if (++iCursor > max) 1.466 + { 1.467 + DefaultLogger::get()->warn("LWO: Invalid file, string is is too long"); 1.468 + break; 1.469 + } 1.470 + ++mFileBuffer; 1.471 + } 1.472 + size_t len = (size_t) ((const char*)mFileBuffer-sz); 1.473 + out = std::string(sz,len); 1.474 + mFileBuffer += (len&0x1 ? 1 : 2); 1.475 +} 1.476 + 1.477 + 1.478 + 1.479 +} // end of namespace Assimp 1.480 + 1.481 +#endif // AI_LWOIMPORTER_H_INCLUDED