Coverage Report

Created: 2026-01-25 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/MD3/MD3Loader.h
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
  copyright notice, this list of conditions and the
15
  following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
  copyright notice, this list of conditions and the
19
  following disclaimer in the documentation and/or other
20
  materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
  contributors may be used to endorse or promote products
24
  derived from this software without specific prior
25
  written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/** @file  Md3Loader.h
43
 *  @brief Declaration of the .MD3 importer class.
44
 */
45
#ifndef AI_MD3LOADER_H_INCLUDED
46
#define AI_MD3LOADER_H_INCLUDED
47
48
#include "MD3FileData.h"
49
#include <assimp/BaseImporter.h>
50
#include <assimp/ByteSwapper.h>
51
#include <assimp/StringComparison.h>
52
#include <assimp/types.h>
53
54
#include <list>
55
56
struct aiMaterial;
57
58
namespace Assimp {
59
60
using namespace MD3;
61
namespace Q3Shader {
62
63
// ---------------------------------------------------------------------------
64
/** @brief Tiny utility data structure to hold the data of a .skin file
65
 */
66
struct SkinData {
67
    //! A single entry in texture list
68
    struct TextureEntry : public std::pair<std::string, std::string> {
69
        // did we resolve this texture entry?
70
        bool resolved;
71
72
        // for std::find()
73
0
        bool operator==(const std::string &f) const {
74
0
            return f == first;
75
0
        }
76
    };
77
78
    //! List of textures
79
    std::list<TextureEntry> textures;
80
81
    // rest is ignored for the moment
82
};
83
84
// ---------------------------------------------------------------------------
85
/** @brief Specifies cull modi for Quake shader files.
86
 */
87
enum ShaderCullMode {
88
    CULL_NONE,
89
    CULL_CW,
90
    CULL_CCW
91
};
92
93
// ---------------------------------------------------------------------------
94
/** @brief Specifies alpha blend modi (src + dest) for Quake shader files
95
 */
96
enum BlendFunc {
97
    BLEND_NONE,
98
    BLEND_GL_ONE,
99
    BLEND_GL_ZERO,
100
    BLEND_GL_DST_COLOR,
101
    BLEND_GL_ONE_MINUS_DST_COLOR,
102
    BLEND_GL_SRC_ALPHA,
103
    BLEND_GL_ONE_MINUS_SRC_ALPHA
104
};
105
106
// ---------------------------------------------------------------------------
107
/** @brief Specifies alpha test modi for Quake texture maps
108
 */
109
enum AlphaTestFunc {
110
    AT_NONE,
111
    AT_GT0,
112
    AT_LT128,
113
    AT_GE128
114
};
115
116
// ---------------------------------------------------------------------------
117
/** @brief Tiny utility data structure to hold a .shader map data block
118
 */
119
struct ShaderMapBlock {
120
    ShaderMapBlock() AI_NO_EXCEPT
121
0
            : blend_src(BLEND_NONE),
122
0
              blend_dest(BLEND_NONE),
123
0
              alpha_test(AT_NONE) {}
124
125
    //! Name of referenced map
126
    std::string name;
127
128
    //! Blend and alpha test settings for texture
129
    BlendFunc blend_src, blend_dest;
130
    AlphaTestFunc alpha_test;
131
132
    //! For std::find()
133
0
    bool operator==(const std::string &o) const {
134
0
        return !ASSIMP_stricmp(o, name);
135
0
    }
136
};
137
138
// ---------------------------------------------------------------------------
139
/** @brief Tiny utility data structure to hold a .shader data block
140
 */
141
struct ShaderDataBlock {
142
    ShaderDataBlock() AI_NO_EXCEPT
143
0
            : cull(CULL_CW) {}
144
145
    //! Name of referenced data element
146
    std::string name;
147
148
    //! Cull mode for the element
149
    ShaderCullMode cull;
150
151
    //! Maps defined in the shader
152
    std::list<ShaderMapBlock> maps;
153
154
    //! For std::find()
155
0
    bool operator==(const std::string &o) const {
156
0
        return !ASSIMP_stricmp(o, name);
157
0
    }
158
};
159
160
// ---------------------------------------------------------------------------
161
/** @brief Tiny utility data structure to hold the data of a .shader file
162
 */
163
struct ShaderData {
164
    //! Shader data blocks
165
    std::list<ShaderDataBlock> blocks;
166
};
167
168
// ---------------------------------------------------------------------------
169
/** @brief Load a shader file
170
 *
171
 *  Generally, parsing is error tolerant. There's no failure.
172
 *  @param fill Receives output data
173
 *  @param file File to be read.
174
 *  @param io IOSystem to be used for reading
175
 *  @return false if file is not accessible
176
 */
177
bool LoadShader(ShaderData &fill, const std::string &file, IOSystem *io);
178
179
// ---------------------------------------------------------------------------
180
/** @brief Convert a Q3Shader to an aiMaterial
181
 *
182
 *  @param[out] out Material structure to be filled.
183
 *  @param[in] shader Input shader
184
 */
185
void ConvertShaderToMaterial(aiMaterial *out, const ShaderDataBlock &shader);
186
187
// ---------------------------------------------------------------------------
188
/** @brief Load a skin file
189
 *
190
 *  Generally, parsing is error tolerant. There's no failure.
191
 *  @param fill Receives output data
192
 *  @param file File to be read.
193
 *  @param io IOSystem to be used for reading
194
 *  @return false if file is not accessible
195
 */
196
bool LoadSkin(SkinData &fill, const std::string &file, IOSystem *io);
197
198
} // namespace Q3Shader
199
200
// ---------------------------------------------------------------------------
201
/** @brief Importer class to load MD3 files
202
*/
203
class MD3Importer : public BaseImporter {
204
public:
205
    MD3Importer();
206
    ~MD3Importer() override;
207
208
    // -------------------------------------------------------------------
209
    /** Returns whether the class can handle the format of the given file.
210
    * See BaseImporter::CanRead() for details.  */
211
    bool CanRead(const std::string &pFile, IOSystem *pIOHandler,
212
            bool checkSig) const override;
213
214
    // -------------------------------------------------------------------
215
    /** Called prior to ReadFile().
216
    * The function is a request to the importer to update its configuration
217
    * basing on the Importer's configuration property list.
218
    */
219
    void SetupProperties(const Importer *pImp) override;
220
221
protected:
222
    // -------------------------------------------------------------------
223
    /** Return importer meta information.
224
     * See #BaseImporter::GetInfo for the details
225
     */
226
    const aiImporterDesc *GetInfo() const override;
227
228
    // -------------------------------------------------------------------
229
    /** Imports the given file into the given scene structure.
230
     * See BaseImporter::InternReadFile() for details
231
     */
232
    void InternReadFile(const std::string &pFile, aiScene *pScene,
233
            IOSystem *pIOHandler) override;
234
235
    // -------------------------------------------------------------------
236
    /** Validate offsets in the header
237
     */
238
    void ValidateHeaderOffsets();
239
    void ValidateSurfaceHeaderOffsets(const MD3::Surface *pcSurfHeader);
240
241
    // -------------------------------------------------------------------
242
    /** Read a Q3 multipart file
243
     *  @return true if multi part has been processed
244
     */
245
    bool ReadMultipartFile();
246
247
    // -------------------------------------------------------------------
248
    /** Try to read the skin for a MD3 file
249
     *  @param fill Receives output information
250
     */
251
    void ReadSkin(Q3Shader::SkinData &fill) const;
252
253
    // -------------------------------------------------------------------
254
    /** Try to read the shader for a MD3 file
255
     *  @param fill Receives output information
256
     */
257
    void ReadShader(Q3Shader::ShaderData &fill) const;
258
259
    // -------------------------------------------------------------------
260
    /** Convert a texture path in a MD3 file to a proper value
261
     *  @param[in] texture_name Path to be converted
262
     *  @param[in] header_path Base path specified in MD3 header
263
     *  @param[out] out Receives the converted output string
264
     */
265
    void ConvertPath(const char *texture_name, const char *header_path,
266
            std::string &out) const;
267
268
protected:
269
    /** Configuration option: frame to be loaded */
270
    unsigned int configFrameID;
271
272
    /** Configuration option: process multi-part files */
273
    bool configHandleMP;
274
275
    /** Configuration option: name of skin file to be read */
276
    std::string configSkinFile;
277
278
    /** Configuration option: whether to load shaders */
279
    bool configLoadShaders;
280
281
    /** Configuration option: name or path of shader */
282
    std::string configShaderFile;
283
284
    /** Configuration option: speed flag was set? */
285
    bool configSpeedFlag;
286
287
    /** Header of the MD3 file */
288
    BE_NCONST MD3::Header *pcHeader;
289
290
    /** File buffer  */
291
    BE_NCONST unsigned char *mBuffer;
292
293
    /** Size of the file, in bytes */
294
    unsigned int fileSize;
295
296
    /** Current file name */
297
    std::string mFile;
298
299
    /** Current base directory  */
300
    std::string path;
301
302
    /** Pure file we're currently reading */
303
    std::string filename;
304
305
    /** Output scene to be filled */
306
    aiScene *mScene;
307
308
    /** IO system to be used to access the data*/
309
    IOSystem *mIOHandler;
310
};
311
312
} // end of namespace Assimp
313
314
#endif // AI_3DSIMPORTER_H_INC