vrshoot
diff libs/assimp/SMDLoader.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/SMDLoader.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,416 @@ 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 SMDLoader.h 1.45 + * @brief Defintion of the Valve SMD file format 1.46 + */ 1.47 + 1.48 +#ifndef AI_SMDLOADER_H_INCLUDED 1.49 +#define AI_SMDLOADER_H_INCLUDED 1.50 + 1.51 +// internal headers 1.52 +#include "BaseImporter.h" 1.53 +#include "ParsingUtils.h" 1.54 + 1.55 +// public Assimp headers 1.56 +#include "assimp/types.h" 1.57 +#include "assimp/texture.h" 1.58 +#include "assimp/anim.h" 1.59 +#include "assimp/material.h" 1.60 +struct aiNode; 1.61 + 1.62 +// STL headers 1.63 +#include <vector> 1.64 + 1.65 +namespace Assimp { 1.66 + 1.67 + 1.68 +namespace SMD { 1.69 + 1.70 +// --------------------------------------------------------------------------- 1.71 +/** Data structure for a vertex in a SMD file 1.72 +*/ 1.73 +struct Vertex 1.74 +{ 1.75 + Vertex() : iParentNode(UINT_MAX) 1.76 + {} 1.77 + 1.78 + //! Vertex position, normal and texture coordinate 1.79 + aiVector3D pos,nor,uv; 1.80 + 1.81 + //! Vertex parent node 1.82 + unsigned int iParentNode; 1.83 + 1.84 + //! Links to bones: pair.first is the bone index, 1.85 + //! pair.second is the vertex weight. 1.86 + //! WARN: The remaining weight (to reach 1.0f) is assigned 1.87 + //! to the parent node/bone 1.88 + std::vector<std::pair<unsigned int, float> > aiBoneLinks; 1.89 +}; 1.90 + 1.91 +// --------------------------------------------------------------------------- 1.92 +/** Data structure for a face in a SMD file 1.93 +*/ 1.94 +struct Face 1.95 +{ 1.96 + Face() : iTexture(0x0) 1.97 + {} 1.98 + 1.99 + //! Texture index for the face 1.100 + unsigned int iTexture; 1.101 + 1.102 + //! The three vertices of the face 1.103 + Vertex avVertices[3]; 1.104 +}; 1.105 + 1.106 +// --------------------------------------------------------------------------- 1.107 +/** Data structure for a bone in a SMD file 1.108 +*/ 1.109 +struct Bone 1.110 +{ 1.111 + //! Default constructor 1.112 + Bone() : iParent(UINT_MAX), bIsUsed(false) 1.113 + { 1.114 + } 1.115 + 1.116 + //! Destructor 1.117 + ~Bone() 1.118 + { 1.119 + } 1.120 + 1.121 + //! Name of the bone 1.122 + std::string mName; 1.123 + 1.124 + //! Parent of the bone 1.125 + uint32_t iParent; 1.126 + 1.127 + //! Animation of the bone 1.128 + struct Animation 1.129 + { 1.130 + //! Public default constructor 1.131 + Animation() 1.132 + { 1.133 + asKeys.reserve(20); 1.134 + } 1.135 + 1.136 + //! Data structure for a matrix key 1.137 + struct MatrixKey 1.138 + { 1.139 + //! Matrix at this time 1.140 + aiMatrix4x4 matrix; 1.141 + 1.142 + //! Absolute transformation matrix 1.143 + aiMatrix4x4 matrixAbsolute; 1.144 + 1.145 + //! Position 1.146 + aiVector3D vPos; 1.147 + 1.148 + //! Rotation (euler angles) 1.149 + aiVector3D vRot; 1.150 + 1.151 + //! Current time. may be negative, this 1.152 + //! will be fixed later 1.153 + double dTime; 1.154 + }; 1.155 + 1.156 + //! Index of the key with the smallest time value 1.157 + uint32_t iFirstTimeKey; 1.158 + 1.159 + //! Array of matrix keys 1.160 + std::vector<MatrixKey> asKeys; 1.161 + 1.162 + } sAnim; 1.163 + 1.164 + //! Offset matrix of the bone 1.165 + aiMatrix4x4 mOffsetMatrix; 1.166 + 1.167 + //! true if the bone is referenced by at least one mesh 1.168 + bool bIsUsed; 1.169 +}; 1.170 + 1.171 +} //! namespace SMD 1.172 + 1.173 +// --------------------------------------------------------------------------- 1.174 +/** Used to load Half-life 1 and 2 SMD models 1.175 +*/ 1.176 +class SMDImporter : public BaseImporter 1.177 +{ 1.178 +public: 1.179 + SMDImporter(); 1.180 + ~SMDImporter(); 1.181 + 1.182 + 1.183 +public: 1.184 + 1.185 + // ------------------------------------------------------------------- 1.186 + /** Returns whether the class can handle the format of the given file. 1.187 + * See BaseImporter::CanRead() for details. 1.188 + */ 1.189 + bool CanRead( const std::string& pFile, IOSystem* pIOHandler, 1.190 + bool checkSig) const; 1.191 + 1.192 + // ------------------------------------------------------------------- 1.193 + /** Called prior to ReadFile(). 1.194 + * The function is a request to the importer to update its configuration 1.195 + * basing on the Importer's configuration property list. 1.196 + */ 1.197 + void SetupProperties(const Importer* pImp); 1.198 + 1.199 +protected: 1.200 + 1.201 + 1.202 + // ------------------------------------------------------------------- 1.203 + /** Return importer meta information. 1.204 + * See #BaseImporter::GetInfo for the details 1.205 + */ 1.206 + const aiImporterDesc* GetInfo () const; 1.207 + 1.208 + // ------------------------------------------------------------------- 1.209 + /** Imports the given file into the given scene structure. 1.210 + * See BaseImporter::InternReadFile() for details 1.211 + */ 1.212 + void InternReadFile( const std::string& pFile, aiScene* pScene, 1.213 + IOSystem* pIOHandler); 1.214 + 1.215 +protected: 1.216 + 1.217 + // ------------------------------------------------------------------- 1.218 + /** Parse the SMD file and create the output scene 1.219 + */ 1.220 + void ParseFile(); 1.221 + 1.222 + // ------------------------------------------------------------------- 1.223 + /** Parse the triangles section of the SMD file 1.224 + * \param szCurrent Current position in the file. Points to the first 1.225 + * data line of the section. 1.226 + * \param szCurrentOut Receives a pointer to the heading line of 1.227 + * the next section (or to EOF) 1.228 + */ 1.229 + void ParseTrianglesSection(const char* szCurrent, 1.230 + const char** szCurrentOut); 1.231 + 1.232 + // ------------------------------------------------------------------- 1.233 + /** Parse the vertex animation section in VTA files 1.234 + * \param szCurrent Current position in the file. Points to the first 1.235 + * data line of the section. 1.236 + * \param szCurrentOut Receives a pointer to the heading line of 1.237 + * the next section (or to EOF) 1.238 + */ 1.239 + void ParseVASection(const char* szCurrent, 1.240 + const char** szCurrentOut); 1.241 + 1.242 + // ------------------------------------------------------------------- 1.243 + /** Parse the nodes section of the SMD file 1.244 + * \param szCurrent Current position in the file. Points to the first 1.245 + * data line of the section. 1.246 + * \param szCurrentOut Receives a pointer to the heading line of 1.247 + * the next section (or to EOF) 1.248 + */ 1.249 + void ParseNodesSection(const char* szCurrent, 1.250 + const char** szCurrentOut); 1.251 + 1.252 + // ------------------------------------------------------------------- 1.253 + /** Parse the skeleton section of the SMD file 1.254 + * \param szCurrent Current position in the file. Points to the first 1.255 + * data line of the section. 1.256 + * \param szCurrentOut Receives a pointer to the heading line of 1.257 + * the next section (or to EOF) 1.258 + */ 1.259 + void ParseSkeletonSection(const char* szCurrent, 1.260 + const char** szCurrentOut); 1.261 + 1.262 + // ------------------------------------------------------------------- 1.263 + /** Parse a single triangle in the SMD file 1.264 + * \param szCurrent Current position in the file. Points to the first 1.265 + * data line of the section. 1.266 + * \param szCurrentOut Receives the output cursor position 1.267 + */ 1.268 + void ParseTriangle(const char* szCurrent, 1.269 + const char** szCurrentOut); 1.270 + 1.271 + 1.272 + // ------------------------------------------------------------------- 1.273 + /** Parse a single vertex in the SMD file 1.274 + * \param szCurrent Current position in the file. Points to the first 1.275 + * data line of the section. 1.276 + * \param szCurrentOut Receives the output cursor position 1.277 + * \param vertex Vertex to be filled 1.278 + */ 1.279 + void ParseVertex(const char* szCurrent, 1.280 + const char** szCurrentOut, SMD::Vertex& vertex, 1.281 + bool bVASection = false); 1.282 + 1.283 + // ------------------------------------------------------------------- 1.284 + /** Get the index of a texture. If the texture was not yet known 1.285 + * it will be added to the internal texture list. 1.286 + * \param filename Name of the texture 1.287 + * \return Value texture index 1.288 + */ 1.289 + unsigned int GetTextureIndex(const std::string& filename); 1.290 + 1.291 + // ------------------------------------------------------------------- 1.292 + /** Computes absolute bone transformations 1.293 + * All output transformations are in worldspace. 1.294 + */ 1.295 + void ComputeAbsoluteBoneTransformations(); 1.296 + 1.297 + 1.298 + // ------------------------------------------------------------------- 1.299 + /** Parse a line in the skeleton section 1.300 + */ 1.301 + void ParseSkeletonElement(const char* szCurrent, 1.302 + const char** szCurrentOut,int iTime); 1.303 + 1.304 + // ------------------------------------------------------------------- 1.305 + /** Parse a line in the nodes section 1.306 + */ 1.307 + void ParseNodeInfo(const char* szCurrent, 1.308 + const char** szCurrentOut); 1.309 + 1.310 + 1.311 + // ------------------------------------------------------------------- 1.312 + /** Parse a floating-point value 1.313 + */ 1.314 + bool ParseFloat(const char* szCurrent, 1.315 + const char** szCurrentOut, float& out); 1.316 + 1.317 + // ------------------------------------------------------------------- 1.318 + /** Parse an unsigned integer. There may be no sign! 1.319 + */ 1.320 + bool ParseUnsignedInt(const char* szCurrent, 1.321 + const char** szCurrentOut, unsigned int& out); 1.322 + 1.323 + // ------------------------------------------------------------------- 1.324 + /** Parse a signed integer. Signs (+,-) are handled. 1.325 + */ 1.326 + bool ParseSignedInt(const char* szCurrent, 1.327 + const char** szCurrentOut, int& out); 1.328 + 1.329 + // ------------------------------------------------------------------- 1.330 + /** Fix invalid time values in the file 1.331 + */ 1.332 + void FixTimeValues(); 1.333 + 1.334 + // ------------------------------------------------------------------- 1.335 + /** Add all children of a bone as subnodes to a node 1.336 + * \param pcNode Parent node 1.337 + * \param iParent Parent bone index 1.338 + */ 1.339 + void AddBoneChildren(aiNode* pcNode, uint32_t iParent); 1.340 + 1.341 + // ------------------------------------------------------------------- 1.342 + /** Build output meshes/materials/nodes/animations 1.343 + */ 1.344 + void CreateOutputMeshes(); 1.345 + void CreateOutputNodes(); 1.346 + void CreateOutputAnimations(); 1.347 + void CreateOutputMaterials(); 1.348 + 1.349 + 1.350 + // ------------------------------------------------------------------- 1.351 + /** Print a log message together with the current line number 1.352 + */ 1.353 + void LogErrorNoThrow(const char* msg); 1.354 + void LogWarning(const char* msg); 1.355 + 1.356 + 1.357 + // ------------------------------------------------------------------- 1.358 + inline bool SkipLine( const char* in, const char** out) 1.359 + { 1.360 + Assimp::SkipLine(in,out); 1.361 + ++iLineNumber; 1.362 + return true; 1.363 + } 1.364 + // ------------------------------------------------------------------- 1.365 + inline bool SkipSpacesAndLineEnd( const char* in, const char** out) 1.366 + { 1.367 + ++iLineNumber; 1.368 + return Assimp::SkipSpacesAndLineEnd(in,out); 1.369 + } 1.370 + 1.371 +private: 1.372 + 1.373 + /** Configuration option: frame to be loaded */ 1.374 + unsigned int configFrameID; 1.375 + 1.376 + /** Buffer to hold the loaded file */ 1.377 + const char* mBuffer; 1.378 + 1.379 + /** Output scene to be filled 1.380 + */ 1.381 + aiScene* pScene; 1.382 + 1.383 + /** Size of the input file in bytes 1.384 + */ 1.385 + unsigned int iFileSize; 1.386 + 1.387 + /** Array of textures found in the file 1.388 + */ 1.389 + std::vector<std::string> aszTextures; 1.390 + 1.391 + /** Array of triangles found in the file 1.392 + */ 1.393 + std::vector<SMD::Face> asTriangles; 1.394 + 1.395 + /** Array of bones found in the file 1.396 + */ 1.397 + std::vector<SMD::Bone> asBones; 1.398 + 1.399 + /** Smallest frame index found in the skeleton 1.400 + */ 1.401 + int iSmallestFrame; 1.402 + 1.403 + /** Length of the whole animation, in frames 1.404 + */ 1.405 + double dLengthOfAnim; 1.406 + 1.407 + /** Do we have texture coordinates? 1.408 + */ 1.409 + bool bHasUVs; 1.410 + 1.411 + /** Current line numer 1.412 + */ 1.413 + unsigned int iLineNumber; 1.414 + 1.415 +}; 1.416 + 1.417 +} // end of namespace Assimp 1.418 + 1.419 +#endif // AI_SMDIMPORTER_H_INC