1 #pragma once
2 
3 /// @file file_formats/id_md2.h
4 /// @details Md2 model file loading structures & constants.
5 
6 #include "ogl_include.h"
7 
8 #if defined(__cplusplus)
9 extern "C"
10 {
11 #endif
12 
13 //--------------------------------------------------------------------------------------------
14 //--------------------------------------------------------------------------------------------
15 // gcc does not properly recognize #pragma pack statements
16 
17 #if !defined(SET_PACKED)
18 #    if defined(__GNUC__)
19 #        define SET_PACKED() __attribute__ ((__packed__))
20 #    else
21 #        define SET_PACKED()
22 #    endif
23 #endif
24 
25 //--------------------------------------------------------------------------------------------
26 //--------------------------------------------------------------------------------------------
27 
28 /// Constants describing the standard md2 file
29     enum e_id_md2_constant
30     {
31         MD2_MAGIC_NUMBER   = 0x32504449,
32         MD2_VERSION        = 8,
33         MD2_MAX_NORMALS    = 162,
34         MD2_MAX_TRIANGLES  = 0x1000,
35         MD2_MAX_VERTICES   = 0x0800,
36         MD2_MAX_TEXCOORDS  = 0x0800,
37         MD2_MAX_FRAMES     = 0x0200,
38         MD2_MAX_SKINS      = 0x0020,
39         MD2_MAX_FRAMESIZE  = ( MD2_MAX_VERTICES * 4 + 128 )
40     };
41 
42 //--------------------------------------------------------------------------------------------
43 //--------------------------------------------------------------------------------------------
44 // try to make sure that the raw data structs are packed,
45 // so that structures can be read/written directly using fread()/fwrite()
46 
47 #pragma pack(push, 1)
48 
49     /* MD2 header */
50     struct s_id_md2_header
51     {
52         int ident;
53         int version;
54 
55         int skinwidth;
56         int skinheight;
57         int framesize;
58 
59         int num_skins;
60         int num_vertices;
61         int num_st;
62         int num_tris;
63         int size_glcmds;
64         int num_frames;
65 
66         int offset_skins;
67         int offset_st;
68         int offset_tris;
69         int offset_frames;
70         int offset_glcmds;
71         int offset_end;
72 
73     } SET_PACKED();
74     typedef struct s_id_md2_header id_md2_header_t;
75 
76     /* Texture name */
77     struct s_id_md2_skin
78     {
79         char name[64];
80     } SET_PACKED();
81     typedef struct s_id_md2_skin id_md2_skin_t;
82 
83     /* Texture coords */
84     struct s_id_md2_texcoord
85     {
86         short s;
87         short t;
88     } SET_PACKED();
89     typedef struct s_id_md2_texcoord id_md2_texcoord_t;
90 
91     /* Triangle info */
92     struct s_id_md2_triangle
93     {
94         unsigned short vertex[3];
95         unsigned short st[3];
96     } SET_PACKED();
97     typedef struct s_id_md2_triangle id_md2_triangle_t;
98 
99     /* Compressed vertex */
100     struct s_id_md2_vertex
101     {
102         unsigned char v[3];
103         unsigned char normalIndex;
104     } SET_PACKED();
105     typedef struct s_id_md2_vertex id_md2_vertex_t;
106 
107     /* Model frame */
108     struct s_id_md2_frame_header
109     {
110         float            scale[3];
111         float            translate[3];
112         char             name[16];
113     } SET_PACKED();
114     typedef struct s_id_md2_frame_header id_md2_frame_header_t;
115 
116     /* Model frame */
117     struct s_id_md2_frame
118     {
119         float            scale[3];
120         float            translate[3];
121         char             name[16];
122         id_md2_vertex_t *verts;
123     } SET_PACKED();
124     typedef struct s_id_md2_frame id_md2_frame_t;
125 
126     /* GL command packet */
127     struct s_id_glcmd_packed
128     {
129         float s;
130         float t;
131         int index;
132     } SET_PACKED();
133     typedef struct s_id_glcmd_packed id_glcmd_packed_t;
134 
135     /* MD2 model structure */
136     struct s_id_md2_model
137     {
138         id_md2_header_t    header;
139 
140         id_md2_skin_t     *skins;
141         id_md2_texcoord_t *texcoords;
142         id_md2_triangle_t *triangles;
143         id_md2_frame_t    *frames;
144         int               *glcmds;
145         GLuint             tex_id;
146     } SET_PACKED();
147     typedef struct s_id_md2_model id_md2_model_t;
148 
149 #pragma pack(pop)
150 
151 //--------------------------------------------------------------------------------------------
152 //--------------------------------------------------------------------------------------------
153     extern float kid_md2_normals[MD2_MAX_NORMALS][3];
154 
155 //--------------------------------------------------------------------------------------------
156 //--------------------------------------------------------------------------------------------
157 
158 /// functions to load the packed data structures directly from a file
159 /// only works with little endian machines
160     id_md2_model_t * id_md2_load( const char *filename, id_md2_model_t * mdl );
161     void             id_md2_free( id_md2_model_t * mdl );
162 
163 //--------------------------------------------------------------------------------------------
164 //--------------------------------------------------------------------------------------------
165 
166 #if defined(__cplusplus)
167 }
168 #endif
169 
170 //--------------------------------------------------------------------------------------------
171 //--------------------------------------------------------------------------------------------
172 
173 #define ID_MD2_H
174