vrshoot

view 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 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 SMDLoader.h
42 * @brief Defintion of the Valve SMD file format
43 */
45 #ifndef AI_SMDLOADER_H_INCLUDED
46 #define AI_SMDLOADER_H_INCLUDED
48 // internal headers
49 #include "BaseImporter.h"
50 #include "ParsingUtils.h"
52 // public Assimp headers
53 #include "assimp/types.h"
54 #include "assimp/texture.h"
55 #include "assimp/anim.h"
56 #include "assimp/material.h"
57 struct aiNode;
59 // STL headers
60 #include <vector>
62 namespace Assimp {
65 namespace SMD {
67 // ---------------------------------------------------------------------------
68 /** Data structure for a vertex in a SMD file
69 */
70 struct Vertex
71 {
72 Vertex() : iParentNode(UINT_MAX)
73 {}
75 //! Vertex position, normal and texture coordinate
76 aiVector3D pos,nor,uv;
78 //! Vertex parent node
79 unsigned int iParentNode;
81 //! Links to bones: pair.first is the bone index,
82 //! pair.second is the vertex weight.
83 //! WARN: The remaining weight (to reach 1.0f) is assigned
84 //! to the parent node/bone
85 std::vector<std::pair<unsigned int, float> > aiBoneLinks;
86 };
88 // ---------------------------------------------------------------------------
89 /** Data structure for a face in a SMD file
90 */
91 struct Face
92 {
93 Face() : iTexture(0x0)
94 {}
96 //! Texture index for the face
97 unsigned int iTexture;
99 //! The three vertices of the face
100 Vertex avVertices[3];
101 };
103 // ---------------------------------------------------------------------------
104 /** Data structure for a bone in a SMD file
105 */
106 struct Bone
107 {
108 //! Default constructor
109 Bone() : iParent(UINT_MAX), bIsUsed(false)
110 {
111 }
113 //! Destructor
114 ~Bone()
115 {
116 }
118 //! Name of the bone
119 std::string mName;
121 //! Parent of the bone
122 uint32_t iParent;
124 //! Animation of the bone
125 struct Animation
126 {
127 //! Public default constructor
128 Animation()
129 {
130 asKeys.reserve(20);
131 }
133 //! Data structure for a matrix key
134 struct MatrixKey
135 {
136 //! Matrix at this time
137 aiMatrix4x4 matrix;
139 //! Absolute transformation matrix
140 aiMatrix4x4 matrixAbsolute;
142 //! Position
143 aiVector3D vPos;
145 //! Rotation (euler angles)
146 aiVector3D vRot;
148 //! Current time. may be negative, this
149 //! will be fixed later
150 double dTime;
151 };
153 //! Index of the key with the smallest time value
154 uint32_t iFirstTimeKey;
156 //! Array of matrix keys
157 std::vector<MatrixKey> asKeys;
159 } sAnim;
161 //! Offset matrix of the bone
162 aiMatrix4x4 mOffsetMatrix;
164 //! true if the bone is referenced by at least one mesh
165 bool bIsUsed;
166 };
168 } //! namespace SMD
170 // ---------------------------------------------------------------------------
171 /** Used to load Half-life 1 and 2 SMD models
172 */
173 class SMDImporter : public BaseImporter
174 {
175 public:
176 SMDImporter();
177 ~SMDImporter();
180 public:
182 // -------------------------------------------------------------------
183 /** Returns whether the class can handle the format of the given file.
184 * See BaseImporter::CanRead() for details.
185 */
186 bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
187 bool checkSig) const;
189 // -------------------------------------------------------------------
190 /** Called prior to ReadFile().
191 * The function is a request to the importer to update its configuration
192 * basing on the Importer's configuration property list.
193 */
194 void SetupProperties(const Importer* pImp);
196 protected:
199 // -------------------------------------------------------------------
200 /** Return importer meta information.
201 * See #BaseImporter::GetInfo for the details
202 */
203 const aiImporterDesc* GetInfo () const;
205 // -------------------------------------------------------------------
206 /** Imports the given file into the given scene structure.
207 * See BaseImporter::InternReadFile() for details
208 */
209 void InternReadFile( const std::string& pFile, aiScene* pScene,
210 IOSystem* pIOHandler);
212 protected:
214 // -------------------------------------------------------------------
215 /** Parse the SMD file and create the output scene
216 */
217 void ParseFile();
219 // -------------------------------------------------------------------
220 /** Parse the triangles section of the SMD file
221 * \param szCurrent Current position in the file. Points to the first
222 * data line of the section.
223 * \param szCurrentOut Receives a pointer to the heading line of
224 * the next section (or to EOF)
225 */
226 void ParseTrianglesSection(const char* szCurrent,
227 const char** szCurrentOut);
229 // -------------------------------------------------------------------
230 /** Parse the vertex animation section in VTA files
231 * \param szCurrent Current position in the file. Points to the first
232 * data line of the section.
233 * \param szCurrentOut Receives a pointer to the heading line of
234 * the next section (or to EOF)
235 */
236 void ParseVASection(const char* szCurrent,
237 const char** szCurrentOut);
239 // -------------------------------------------------------------------
240 /** Parse the nodes section of the SMD file
241 * \param szCurrent Current position in the file. Points to the first
242 * data line of the section.
243 * \param szCurrentOut Receives a pointer to the heading line of
244 * the next section (or to EOF)
245 */
246 void ParseNodesSection(const char* szCurrent,
247 const char** szCurrentOut);
249 // -------------------------------------------------------------------
250 /** Parse the skeleton section of the SMD file
251 * \param szCurrent Current position in the file. Points to the first
252 * data line of the section.
253 * \param szCurrentOut Receives a pointer to the heading line of
254 * the next section (or to EOF)
255 */
256 void ParseSkeletonSection(const char* szCurrent,
257 const char** szCurrentOut);
259 // -------------------------------------------------------------------
260 /** Parse a single triangle in the SMD file
261 * \param szCurrent Current position in the file. Points to the first
262 * data line of the section.
263 * \param szCurrentOut Receives the output cursor position
264 */
265 void ParseTriangle(const char* szCurrent,
266 const char** szCurrentOut);
269 // -------------------------------------------------------------------
270 /** Parse a single vertex in the SMD file
271 * \param szCurrent Current position in the file. Points to the first
272 * data line of the section.
273 * \param szCurrentOut Receives the output cursor position
274 * \param vertex Vertex to be filled
275 */
276 void ParseVertex(const char* szCurrent,
277 const char** szCurrentOut, SMD::Vertex& vertex,
278 bool bVASection = false);
280 // -------------------------------------------------------------------
281 /** Get the index of a texture. If the texture was not yet known
282 * it will be added to the internal texture list.
283 * \param filename Name of the texture
284 * \return Value texture index
285 */
286 unsigned int GetTextureIndex(const std::string& filename);
288 // -------------------------------------------------------------------
289 /** Computes absolute bone transformations
290 * All output transformations are in worldspace.
291 */
292 void ComputeAbsoluteBoneTransformations();
295 // -------------------------------------------------------------------
296 /** Parse a line in the skeleton section
297 */
298 void ParseSkeletonElement(const char* szCurrent,
299 const char** szCurrentOut,int iTime);
301 // -------------------------------------------------------------------
302 /** Parse a line in the nodes section
303 */
304 void ParseNodeInfo(const char* szCurrent,
305 const char** szCurrentOut);
308 // -------------------------------------------------------------------
309 /** Parse a floating-point value
310 */
311 bool ParseFloat(const char* szCurrent,
312 const char** szCurrentOut, float& out);
314 // -------------------------------------------------------------------
315 /** Parse an unsigned integer. There may be no sign!
316 */
317 bool ParseUnsignedInt(const char* szCurrent,
318 const char** szCurrentOut, unsigned int& out);
320 // -------------------------------------------------------------------
321 /** Parse a signed integer. Signs (+,-) are handled.
322 */
323 bool ParseSignedInt(const char* szCurrent,
324 const char** szCurrentOut, int& out);
326 // -------------------------------------------------------------------
327 /** Fix invalid time values in the file
328 */
329 void FixTimeValues();
331 // -------------------------------------------------------------------
332 /** Add all children of a bone as subnodes to a node
333 * \param pcNode Parent node
334 * \param iParent Parent bone index
335 */
336 void AddBoneChildren(aiNode* pcNode, uint32_t iParent);
338 // -------------------------------------------------------------------
339 /** Build output meshes/materials/nodes/animations
340 */
341 void CreateOutputMeshes();
342 void CreateOutputNodes();
343 void CreateOutputAnimations();
344 void CreateOutputMaterials();
347 // -------------------------------------------------------------------
348 /** Print a log message together with the current line number
349 */
350 void LogErrorNoThrow(const char* msg);
351 void LogWarning(const char* msg);
354 // -------------------------------------------------------------------
355 inline bool SkipLine( const char* in, const char** out)
356 {
357 Assimp::SkipLine(in,out);
358 ++iLineNumber;
359 return true;
360 }
361 // -------------------------------------------------------------------
362 inline bool SkipSpacesAndLineEnd( const char* in, const char** out)
363 {
364 ++iLineNumber;
365 return Assimp::SkipSpacesAndLineEnd(in,out);
366 }
368 private:
370 /** Configuration option: frame to be loaded */
371 unsigned int configFrameID;
373 /** Buffer to hold the loaded file */
374 const char* mBuffer;
376 /** Output scene to be filled
377 */
378 aiScene* pScene;
380 /** Size of the input file in bytes
381 */
382 unsigned int iFileSize;
384 /** Array of textures found in the file
385 */
386 std::vector<std::string> aszTextures;
388 /** Array of triangles found in the file
389 */
390 std::vector<SMD::Face> asTriangles;
392 /** Array of bones found in the file
393 */
394 std::vector<SMD::Bone> asBones;
396 /** Smallest frame index found in the skeleton
397 */
398 int iSmallestFrame;
400 /** Length of the whole animation, in frames
401 */
402 double dLengthOfAnim;
404 /** Do we have texture coordinates?
405 */
406 bool bHasUVs;
408 /** Current line numer
409 */
410 unsigned int iLineNumber;
412 };
414 } // end of namespace Assimp
416 #endif // AI_SMDIMPORTER_H_INC