1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2015, assimp team
6 All rights reserved.
7 
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:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
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.
20 
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.
25 
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.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 /** @file Md3FileData.h
42  *
43  *  @brief Defines helper data structures for importing MD3 files.
44  *  http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
45  */
46 #ifndef AI_MD3FILEHELPER_H_INC
47 #define AI_MD3FILEHELPER_H_INC
48 
49 #include <string>
50 #include <vector>
51 #include <sstream>
52 // Urho3D: VS2008 compatibility
53 #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
54 #include <stdint.h>
55 #else
56 #include "../include/assimp/Compiler/pstdint.h"
57 #endif
58 
59 #include "../include/assimp/types.h"
60 #include "../include/assimp/mesh.h"
61 #include "../include/assimp/anim.h"
62 
63 #include "./../include/assimp/Compiler/pushpack1.h"
64 
65 namespace Assimp    {
66 namespace MD3   {
67 
68 // to make it easier for us, we test the magic word against both "endianesses"
69 #define AI_MD3_MAGIC_NUMBER_BE  AI_MAKE_MAGIC("IDP3")
70 #define AI_MD3_MAGIC_NUMBER_LE  AI_MAKE_MAGIC("3PDI")
71 
72 // common limitations
73 #define AI_MD3_VERSION          15
74 #define AI_MD3_MAXQPATH         64
75 #define AI_MD3_MAXFRAME         16
76 #define AI_MD3_MAX_FRAMES       1024
77 #define AI_MD3_MAX_TAGS         16
78 #define AI_MD3_MAX_SURFACES     32
79 #define AI_MD3_MAX_SHADERS      256
80 #define AI_MD3_MAX_VERTS        4096
81 #define AI_MD3_MAX_TRIANGLES    8192
82 
83 // master scale factor for all vertices in a MD3 model
84 #define AI_MD3_XYZ_SCALE        (1.0f/64.0f)
85 
86 // -------------------------------------------------------------------------------
87 /** @brief Data structure for the MD3 main header
88  */
89 struct Header
90 {
91     //! magic number
92     uint32_t IDENT;
93 
94     //! file format version
95     uint32_t VERSION;
96 
97     //! original name in .pak archive
98     char NAME[ AI_MD3_MAXQPATH ];
99 
100     //! unknown
101     int32_t FLAGS;
102 
103     //! number of frames in the file
104     uint32_t NUM_FRAMES;
105 
106     //! number of tags in the file
107     uint32_t NUM_TAGS;
108 
109     //! number of surfaces in the file
110     uint32_t NUM_SURFACES;
111 
112     //! number of skins in the file
113     uint32_t NUM_SKINS;
114 
115     //! offset of the first frame
116     uint32_t OFS_FRAMES;
117 
118     //! offset of the first tag
119     uint32_t OFS_TAGS;
120 
121     //! offset of the first surface
122     uint32_t OFS_SURFACES;
123 
124     //! end of file
125     uint32_t OFS_EOF;
126 } PACK_STRUCT;
127 
128 
129 // -------------------------------------------------------------------------------
130 /** @brief Data structure for the frame header
131  */
132 struct Frame
133 {
134     //! minimum bounds
135     aiVector3D min;
136 
137     //! maximum bounds
138     aiVector3D max;
139 
140     //! local origin for this frame
141     aiVector3D origin;
142 
143     //! radius of bounding sphere
144     float radius;
145 
146     //! name of frame
147     char name[ AI_MD3_MAXFRAME ];
148 
149 } PACK_STRUCT;
150 
151 
152 // -------------------------------------------------------------------------------
153 /** @brief Data structure for the tag header
154  */
155 struct Tag
156 {
157     //! name of the tag
158     char NAME[ AI_MD3_MAXQPATH ];
159 
160     //! Local tag origin and orientation
161     aiVector3D  origin;
162     float  orientation[3][3];
163 
164 } PACK_STRUCT;
165 
166 
167 // -------------------------------------------------------------------------------
168 /** @brief Data structure for the surface header
169  */
170 struct Surface
171 {
172     //! magic number
173     int32_t IDENT;
174 
175     //! original name of the surface
176     char NAME[ AI_MD3_MAXQPATH ];
177 
178     //! unknown
179     int32_t FLAGS;
180 
181     //! number of frames in the surface
182     uint32_t NUM_FRAMES;
183 
184     //! number of shaders in the surface
185     uint32_t NUM_SHADER;
186 
187     //! number of vertices in the surface
188     uint32_t NUM_VERTICES;
189 
190     //! number of triangles in the surface
191     uint32_t NUM_TRIANGLES;
192 
193 
194     //! offset to the triangle data
195     uint32_t OFS_TRIANGLES;
196 
197     //! offset to the shader data
198     uint32_t OFS_SHADERS;
199 
200     //! offset to the texture coordinate data
201     uint32_t OFS_ST;
202 
203     //! offset to the vertex/normal data
204     uint32_t OFS_XYZNORMAL;
205 
206     //! offset to the end of the Surface object
207     int32_t OFS_END;
208 } PACK_STRUCT;
209 
210 // -------------------------------------------------------------------------------
211 /** @brief Data structure for a shader defined in there
212  */
213 struct Shader
214 {
215     //! filename of the shader
216     char NAME[ AI_MD3_MAXQPATH ];
217 
218     //! index of the shader
219     uint32_t SHADER_INDEX;
220 } PACK_STRUCT;
221 
222 
223 // -------------------------------------------------------------------------------
224 /** @brief Data structure for a triangle
225  */
226 struct Triangle
227 {
228     //! triangle indices
229     uint32_t INDEXES[3];
230 } PACK_STRUCT;
231 
232 
233 // -------------------------------------------------------------------------------
234 /** @brief Data structure for an UV coord
235  */
236 struct TexCoord
237 {
238     //! UV coordinates
239     float U,V;
240 } PACK_STRUCT;
241 
242 
243 // -------------------------------------------------------------------------------
244 /** @brief Data structure for a vertex
245  */
246 struct Vertex
247 {
248     //! X/Y/Z coordinates
249     int16_t X,Y,Z;
250 
251     //! encoded normal vector
252     uint16_t  NORMAL;
253 } PACK_STRUCT;
254 
255 #include "./../include/assimp/Compiler/poppack1.h"
256 
257 // -------------------------------------------------------------------------------
258 /** @brief Unpack a Q3 16 bit vector to its full float3 representation
259  *
260  *  @param p_iNormal Input normal vector in latitude/longitude form
261  *  @param p_afOut Pointer to an array of three floats to receive the result
262  *
263  *  @note This has been taken from q3 source (misc_model.c)
264  */
LatLngNormalToVec3(uint16_t p_iNormal,float * p_afOut)265 inline void LatLngNormalToVec3(uint16_t p_iNormal, float* p_afOut)
266 {
267     float lat = (float)(( p_iNormal >> 8u ) & 0xff);
268     float lng = (float)(( p_iNormal & 0xff ));
269     lat *= 3.141926f/128.0f;
270     lng *= 3.141926f/128.0f;
271 
272     p_afOut[0] = std::cos(lat) * std::sin(lng);
273     p_afOut[1] = std::sin(lat) * std::sin(lng);
274     p_afOut[2] = std::cos(lng);
275     return;
276 }
277 
278 
279 // -------------------------------------------------------------------------------
280 /** @brief Pack a Q3 normal into 16bit latitute/longitude representation
281  *  @param p_vIn Input vector
282  *  @param p_iOut Output normal
283  *
284  *  @note This has been taken from q3 source (mathlib.c)
285  */
Vec3NormalToLatLng(const aiVector3D & p_vIn,uint16_t & p_iOut)286 inline void Vec3NormalToLatLng( const aiVector3D& p_vIn, uint16_t& p_iOut )
287 {
288     // check for singularities
289     if ( 0.0f == p_vIn[0] && 0.0f == p_vIn[1] )
290     {
291         if ( p_vIn[2] > 0.0f )
292         {
293             ((unsigned char*)&p_iOut)[0] = 0;
294             ((unsigned char*)&p_iOut)[1] = 0;       // lat = 0, long = 0
295         }
296         else
297         {
298             ((unsigned char*)&p_iOut)[0] = 128;
299             ((unsigned char*)&p_iOut)[1] = 0;       // lat = 0, long = 128
300         }
301     }
302     else
303     {
304         int a, b;
305 
306         a = int(57.2957795f * ( atan2f( p_vIn[1], p_vIn[0] ) ) * (255.0f / 360.0f ));
307         a &= 0xff;
308 
309         b = int(57.2957795f * ( acosf( p_vIn[2] ) ) * ( 255.0f / 360.0f ));
310         b &= 0xff;
311 
312         ((unsigned char*)&p_iOut)[0] = b;   // longitude
313         ((unsigned char*)&p_iOut)[1] = a;   // lattitude
314     }
315 }
316 
317 }
318 }
319 
320 #endif // !! AI_MD3FILEHELPER_H_INC
321 
322