summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/code/AssetLib/SMD/SMDLoader.h
blob: db882a241c259cb1bf8586beb435e7e253012300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2022, assimp team

All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

/** @file  SMDLoader.h
 *  @brief Definition of the Valve SMD file format
 */

#pragma once
#ifndef AI_SMDLOADER_H_INCLUDED
#define AI_SMDLOADER_H_INCLUDED

#include <assimp/BaseImporter.h>
#include <assimp/ParsingUtils.h>
#include <assimp/types.h>
#include <assimp/texture.h>
#include <assimp/anim.h>
#include <assimp/material.h>

#include <vector>

struct aiNode;

namespace Assimp {
namespace SMD {

// ---------------------------------------------------------------------------
/** Data structure for a vertex in a SMD file
*/
struct Vertex {
    Vertex() AI_NO_EXCEPT : iParentNode(UINT_MAX) {
        // empty
    }

    //! Vertex position, normal and texture coordinate
    aiVector3D pos,nor,uv;

    //! Vertex parent node
    unsigned int iParentNode;

    //! Links to bones: pair.first is the bone index,
    //! pair.second is the vertex weight.
    //! WARN: The remaining weight (to reach 1.0f) is assigned
    //! to the parent node/bone
    std::vector<std::pair<unsigned int, float> > aiBoneLinks;
};

// ---------------------------------------------------------------------------
/** Data structure for a face in a SMD file
*/
struct Face {
    Face() AI_NO_EXCEPT :
            iTexture(0x0), avVertices{} {
        // empty
    }
    
    //! Texture index for the face
    unsigned int iTexture;

    //! The three vertices of the face
    Vertex avVertices[3];
};

// ---------------------------------------------------------------------------
/** Data structure for a bone in a SMD file
*/
struct Bone {
    //! Default constructor
    Bone() AI_NO_EXCEPT : iParent(UINT_MAX), bIsUsed(false) {
        // empty
    }

    //! Name of the bone
    std::string mName;

    //! Parent of the bone
    uint32_t iParent;

    //! Animation of the bone
    struct Animation {
        //! Public default constructor
        Animation() AI_NO_EXCEPT : iFirstTimeKey() {
            asKeys.reserve(20);
        }

        //! Data structure for a matrix key
        struct MatrixKey {
            //! Matrix at this time
            aiMatrix4x4 matrix;

            //! Absolute transformation matrix
            aiMatrix4x4 matrixAbsolute;

            //! Position
            aiVector3D vPos;

            //! Rotation (euler angles)
            aiVector3D vRot;

            //! Current time. may be negative, this
            //! will be fixed later
            double dTime;
        };

        //! Index of the key with the smallest time value
        uint32_t iFirstTimeKey;

        //! Array of matrix keys
        std::vector<MatrixKey> asKeys;

    } sAnim;

    //! Offset matrix of the bone
    aiMatrix4x4 mOffsetMatrix;

    //! true if the bone is referenced by at least one mesh
    bool bIsUsed;
};

} //! namespace SMD

// ---------------------------------------------------------------------------
/** Used to load Half-life 1 and 2 SMD models
*/
class ASSIMP_API SMDImporter : public BaseImporter {
public:
    SMDImporter();
    ~SMDImporter() override;

    // -------------------------------------------------------------------
    /** Returns whether the class can handle the format of the given file.
     * See BaseImporter::CanRead() for details.
     */
    bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
        bool checkSig) const override;

    // -------------------------------------------------------------------
    /** Called prior to ReadFile().
     * The function is a request to the importer to update its configuration
     * basing on the Importer's configuration property list.
     */
    void SetupProperties(const Importer* pImp) override;

protected:
    // -------------------------------------------------------------------
    /** Return importer meta information.
     * See #BaseImporter::GetInfo for the details
     */
    const aiImporterDesc* GetInfo () const override;

    // -------------------------------------------------------------------
    /** Imports the given file into the given scene structure.
    * See BaseImporter::InternReadFile() for details
    */
    void InternReadFile( const std::string& pFile, aiScene* pScene,
        IOSystem* pIOHandler) override;

    // -------------------------------------------------------------------
    /** Parse the SMD file and create the output scene
    */
    void ParseFile();
    void ReadSmd(const std::string &pFile, IOSystem* pIOHandler);

    // -------------------------------------------------------------------
    /** Parse the triangles section of the SMD file
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives a pointer to the heading line of
     * the next section (or to EOF)
    */
    void ParseTrianglesSection(const char* szCurrent,
        const char** szCurrentOut);

    // -------------------------------------------------------------------
    /** Parse the vertex animation section in VTA files
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives a pointer to the heading line of
     * the next section (or to EOF)
    */
    void ParseVASection(const char* szCurrent,
        const char** szCurrentOut);

    // -------------------------------------------------------------------
    /** Parse the nodes section of the SMD file
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives a pointer to the heading line of
     * the next section (or to EOF)
    */
    void ParseNodesSection(const char* szCurrent,
        const char** szCurrentOut);

    // -------------------------------------------------------------------
    /** Parse the skeleton section of the SMD file
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives a pointer to the heading line of
     * the next section (or to EOF)
    */
    void ParseSkeletonSection(const char* szCurrent,
        const char** szCurrentOut);

    // -------------------------------------------------------------------
    /** Parse a single triangle in the SMD file
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives the output cursor position
    */
    void ParseTriangle(const char* szCurrent,
        const char** szCurrentOut);


    // -------------------------------------------------------------------
    /** Parse a single vertex in the SMD file
     * \param szCurrent Current position in the file. Points to the first
     * data line of the section.
     * \param szCurrentOut Receives the output cursor position
     * \param vertex Vertex to be filled
    */
    void ParseVertex(const char* szCurrent,
        const char** szCurrentOut, SMD::Vertex& vertex,
        bool bVASection = false);

    // -------------------------------------------------------------------
    /** Get  the index of a texture. If the texture was not yet known
     *  it will be added to the internal texture list.
     * \param filename Name of the texture
     * \return Value texture index
     */
    unsigned int GetTextureIndex(const std::string& filename);

    // -------------------------------------------------------------------
    /** Parse a line in the skeleton section
     */
    void ParseSkeletonElement(const char* szCurrent,
        const char** szCurrentOut,int iTime);

    // -------------------------------------------------------------------
    /** Parse a line in the nodes section
     */
    void ParseNodeInfo(const char* szCurrent,
        const char** szCurrentOut);


    // -------------------------------------------------------------------
    /** Parse a floating-point value
     */
    bool ParseFloat(const char* szCurrent,
        const char** szCurrentOut, float& out);

    // -------------------------------------------------------------------
    /** Parse an unsigned integer. There may be no sign!
     */
    bool ParseUnsignedInt(const char* szCurrent,
        const char** szCurrentOut, unsigned int& out);

    // -------------------------------------------------------------------
    /** Parse a signed integer. Signs (+,-) are handled.
     */
    bool ParseSignedInt(const char* szCurrent,
        const char** szCurrentOut, int& out);

    // -------------------------------------------------------------------
    /** Fix invalid time values in the file
     */
    void FixTimeValues();

    // -------------------------------------------------------------------
    /** Add all children of a bone as subnodes to a node
     * \param pcNode Parent node
     * \param iParent Parent bone index
     */
    void AddBoneChildren(aiNode* pcNode, uint32_t iParent);

    // -------------------------------------------------------------------
    /** Build output meshes/materials/nodes/animations
     */
    void CreateOutputMeshes();
    void CreateOutputNodes();
    void CreateOutputAnimations(const std::string &pFile, IOSystem* pIOHandler);
    void CreateOutputAnimation(int index, const std::string &name);
    void GetAnimationFileList(const std::string &pFile, IOSystem* pIOHandler, std::vector<std::tuple<std::string, std::string>>& outList);
    void CreateOutputMaterials();


    // -------------------------------------------------------------------
    /** Print a log message together with the current line number
     */
    void LogErrorNoThrow(const char* msg);
    void LogWarning(const char* msg);


    // -------------------------------------------------------------------
    inline bool SkipLine( const char* in, const char** out)
    {
        Assimp::SkipLine(in,out);
        ++iLineNumber;
        return true;
    }
    // -------------------------------------------------------------------
    inline bool SkipSpacesAndLineEnd( const char* in, const char** out)
    {
        ++iLineNumber;
        return Assimp::SkipSpacesAndLineEnd(in,out);
    }

private:

    /** Configuration option: frame to be loaded */
    unsigned int configFrameID;

    /** Buffer to hold the loaded file */
    std::vector<char> mBuffer;

    /** Output scene to be filled
    */
    aiScene* pScene;

    /** Size of the input file in bytes
     */
    unsigned int iFileSize;

    /** Array of textures found in the file
     */
    std::vector<std::string> aszTextures;

    /** Array of triangles found in the file
     */
    std::vector<SMD::Face> asTriangles;

    /** Array of bones found in the file
     */
    std::vector<SMD::Bone> asBones;

    /** Smallest frame index found in the skeleton
     */
    int iSmallestFrame;

    /** Length of the whole animation, in frames
     */
    double dLengthOfAnim;

    /** Do we have texture coordinates?
     */
    bool bHasUVs;

    /** Current line number
     */
    unsigned int iLineNumber;

    bool bLoadAnimationList = true;
    bool noSkeletonMesh = false;
};

} // end of namespace Assimp

#endif // AI_SMDIMPORTER_H_INC