source: trunk/src/formats/md3_loader.cc @ 304

Last change on this file since 304 was 304, checked in by epyon, 11 years ago
  • mouse wheel support for both SDL 1.2 and 2.0
  • optional unmerged MD3 import
  • selective delete mesh form mesh_creator
  • minor fixes
File size: 13.8 KB
RevLine 
[148]1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/formats/md3_loader.hh"
8
9#include <glm/gtc/constants.hpp>
10#include "nv/logging.hh"
11#include <cstring>
12
13using namespace nv;
14
15// based on http://www.icculus.org/~phaethon/q3/formats/md3format.html#Surface
16
17// assuming low-endian
18#define MD3_MAX_FRAMES    1024
19#define MD3_MAX_TAGS      16
20#define MD3_MAX_SURFACES  32
21#define MD3_MAX_SHADERS   256
22#define MD3_MAX_VERTS     4096
23#define MD3_MAX_TRIANGLES 8192
24#define MD3_XYZ_SCALE     (1.0f/64.0f)
25
26struct md3_vec3_t
27{
28        float xyz[3];
29};
30
31struct md3_header_t
32{
33        char   ident[4]; // IDP3
34        sint32 version;  // 15
35        uint8  name[64]; // path name
36        sint32 flags;
37        sint32 num_frames;     // Number of Frame objects, with a maximum of MD3_MAX_FRAMES. Current value of MD3_MAX_FRAMES is 1024.
38        sint32 num_tags;       // Number of Tag objects, with a maximum of MD3_MAX_TAGS. Current value of MD3_MAX_TAGS is 16.
39        sint32 num_surfaces;   // Number of Surface objects, with a maximum of MD3_MAX_SURFACES. Current value of MD3_MAX_SURFACES is 32.
40        sint32 num_skins;      // Number of Skin objects. I should note that I have not seen an MD3 using this particular field for anything; this appears to be an artifact from the Quake 2 MD2 format. Surface objects have their own Shader field.
41        sint32 ofs_frames;     // Relative offset from start of MD3 object where Frame objects start. The Frame objects are written sequentially, that is, when you read one Frame object, you do not need to seek() for the next object.
42        sint32 ofs_tags;       // Relative offset from start of MD3 where Tag objects start. Similarly written sequentially.
43        sint32 ofs_surfaces;   // Relative offset from start of MD3 where Surface objects start. Again, written sequentially.
44        sint32 ofs_eof;        // Relative offset from start of MD3 to the end of the MD3 object. Note there is no offset for Skin objects.
45};
46
47struct md3_frame_t
48{
49        md3_vec3_t min_bounds;
50        md3_vec3_t max_bounds;
51        md3_vec3_t local_origin;
52        float      radius;
53        uint8      name[16];
54};
55
56struct md3_tag_t
57{
58        uint8      name[64];
59        md3_vec3_t origin;
60        md3_vec3_t axis[3];
61};
62
63struct md3_surface_header_t
64{
65        char   ident[4]; // IDP3
66        uint8  name[64]; // path name
67        sint32 flags;
68        sint32 num_frames;
69        sint32 num_shaders;
70        sint32 num_verts;
71        sint32 num_triangles;
72        sint32 ofs_triangles;
73        sint32 ofs_shaders;
74        sint32 ofs_st;
75        sint32 ofs_xyznormal;
76        sint32 ofs_end;
77};
78
79struct md3_shader_t
80{
81        uint8  name[64];
82        sint32 shader_index;
83};
84
85struct md3_triangle_t
86{
87        sint32 indexes[3];
88};
89
90struct md3_texcoord_t
91{
92        float  st[2];
93};
94
95struct md3_vertex_t
96{
97        sint16 x;
98        sint16 y;
99        sint16 z;
[149]100        uint16 normal;
[148]101};
102
103struct md3_surface_t
104{
105        md3_surface_header_t header;
106        md3_shader_t*        shaders;
107        md3_triangle_t*      triangles;
108        md3_texcoord_t*      st;
109        md3_vertex_t*        vertices;
110};
111
112struct md3_t
113{
114        md3_header_t   header;
115        md3_frame_t*   frames;
116        md3_tag_t*     tags;
117        md3_surface_t* surfaces;
[149]118        // extra information (not in md3 file)
119        sint32         vertices_per_frame;
[148]120};
121
122static bool check_md3_magic( char* magic )
123{
124        return magic[0] == 'I' && magic[1] == 'D' && magic[2] == 'P' && magic[3] == '3';
125}
126
127static void free_md3_surface( md3_surface_t * surface )
128{
129        delete[] surface->shaders;
130        delete[] surface->triangles;
131        delete[] surface->st;
132        delete[] surface->vertices;
133}
134
135static void free_md3( md3_t * md3 )
136{
137        sint32 count = md3->header.num_surfaces;
138        for ( sint32 i = 0; i < count; ++i )
139        {
140                free_md3_surface( &md3->surfaces[i] );
141        }
142        delete[] md3->frames;
143        delete[] md3->tags;
144        delete[] md3->surfaces;
145}
146
147static bool read_surface( md3_surface_t * surface, nv::stream& source )
148{
[198]149        sint32 pos = static_cast< sint32 >( source.tell() );
[148]150        source.read( &surface->header, sizeof(md3_surface_header_t), 1 );
151
152        if ( !check_md3_magic( surface->header.ident ) )          return false;
153        if ( surface->header.num_frames    >  MD3_MAX_FRAMES )    return false;
154        if ( surface->header.num_shaders   >  MD3_MAX_SHADERS )   return false;
155        if ( surface->header.num_verts     >  MD3_MAX_VERTS )     return false;
156        if ( surface->header.num_triangles >  MD3_MAX_TRIANGLES ) return false;
157
158        surface->shaders   = new md3_shader_t  [ surface->header.num_shaders ];
159        surface->vertices  = new md3_vertex_t  [ surface->header.num_verts * surface->header.num_frames ];
160        surface->st        = new md3_texcoord_t[ surface->header.num_verts ];
161        surface->triangles = new md3_triangle_t[ surface->header.num_triangles ];
162
163        source.seek( pos + surface->header.ofs_shaders, origin::SET );
[198]164        source.read( surface->shaders, sizeof( md3_shader_t ), static_cast<size_t>( surface->header.num_shaders ) );
[148]165
166        source.seek( pos + surface->header.ofs_triangles, origin::SET );
[198]167        source.read( surface->triangles, sizeof( md3_triangle_t ), static_cast<size_t>( surface->header.num_triangles ) );
[148]168
169        source.seek( pos + surface->header.ofs_st, origin::SET );
[198]170        source.read( surface->st, sizeof( md3_texcoord_t ), static_cast<size_t>( surface->header.num_verts ) );
[148]171
172        source.seek( pos + surface->header.ofs_xyznormal, origin::SET );
[198]173        source.read( surface->vertices, sizeof( md3_vertex_t ), static_cast<size_t>( surface->header.num_verts * surface->header.num_frames ) );
[148]174
175        if ( source.tell() != static_cast<std::size_t>( pos + surface->header.ofs_end ) ) return false;
176
177        return true;
178}
179
180static bool read_md3( md3_t * md3, nv::stream& source )
181{
182        md3->frames   = nullptr;
183        md3->tags     = nullptr;
184        md3->surfaces = nullptr;
185
186        source.read( &md3->header, sizeof(md3_header_t), 1 );
187
188        if ( !check_md3_magic( md3->header.ident ) )        return false;
189        if ( md3->header.num_frames   >  MD3_MAX_FRAMES )   return false;
190        if ( md3->header.num_tags     >  MD3_MAX_TAGS )     return false;
191        if ( md3->header.num_surfaces >  MD3_MAX_SURFACES )
192        {
193                // to always have a safe free
194                md3->header.num_surfaces = 0;
195                return false;
196        }
197
198        md3->frames   = new md3_frame_t  [ md3->header.num_frames ];
199        md3->tags     = new md3_tag_t    [ md3->header.num_tags * md3->header.num_frames ];
200        md3->surfaces = new md3_surface_t[ md3->header.num_surfaces ];
[198]201        std::memset( md3->surfaces, 0, static_cast< size_t >( md3->header.num_surfaces ) * sizeof(md3_surface_t) );
[148]202
203        source.seek( md3->header.ofs_frames, origin::SET );
[198]204        source.read( md3->frames, sizeof( md3_frame_t ), static_cast<size_t>( md3->header.num_frames ) );
[148]205
206        source.seek( md3->header.ofs_tags, origin::SET );
[198]207        source.read( md3->tags, sizeof( md3_tag_t ), static_cast<size_t>( md3->header.num_tags * md3->header.num_frames ) );
[148]208
209        source.seek( md3->header.ofs_surfaces, origin::SET );
[149]210        md3->vertices_per_frame = 0;
[148]211
212        for ( sint32 i = 0; i < md3->header.num_surfaces; ++i )
213        {
214                if ( !read_surface( md3->surfaces + i, source ) ) return false;
215                if ( md3->header.num_frames != md3->surfaces[i].header.num_frames ) return false;
[149]216                md3->vertices_per_frame += md3->surfaces[i].header.num_verts;
[148]217        }
218        return true;
219}
220
221static inline vec3 md3_vec3( const md3_vec3_t& v )
222{
223//      return vec3( v.xyz[0], v.xyz[1], v.xyz[2] );
224        return vec3( v.xyz[0], v.xyz[2], v.xyz[1] );
225}
226
227static inline vec2 md3_texcoord( const md3_texcoord_t& v )
228{
229        return vec2( v.st[0], v.st[1] );
230}
231
[149]232static vec3 s_normal_cache[256*256];
233static bool s_normal_ready = false;
[148]234
[304]235md3_loader::md3_loader( bool merge_all )
236        : m_md3( nullptr ), m_merge_all( merge_all )
[148]237{
[149]238        if ( !s_normal_ready )
239        {
240                float pi      = glm::pi<float>();
241                float convert = (2 * pi) / 255.0f;
242                int n = 0;
243                for ( int lat = 0; lat < 256; ++lat )
244                {
245                        float flat    = lat * convert;
246                        float sin_lat = glm::sin( flat );
247                        float cos_lat = glm::cos( flat );
248                        for ( int lng = 0; lng < 256; ++lng, ++n )
249                        {
250                                float flng    = lng * convert;
251                                float sin_lng = glm::sin( flng );
252                                float cos_lng = glm::cos( flng );
253                                s_normal_cache[n].x = cos_lat * sin_lng;
254//                              s_normal_cache[n].y = sin_lat * sin_lng;
255//                              s_normal_cache[n].z = cos_lng;
256                                s_normal_cache[n].z = sin_lat * sin_lng;
257                                s_normal_cache[n].y = cos_lng;
258                        }
259                }
260
261                s_normal_ready = true;
262        }
[148]263}
264
265
266nv::md3_loader::~md3_loader()
267{
268        if (m_md3 != nullptr)
269        {
270                free_md3( (md3_t*)(m_md3) );
271                delete (md3_t*)m_md3;
272        }
273}
274
275bool nv::md3_loader::load( stream& source )
276{
277        m_md3 = (void*)(new md3_t);
278        if ( !read_md3( (md3_t*)m_md3, source ) )
279        {
280                return false;
281        }
282        return true;
283}
284
[282]285nv::key_raw_channel* nv::md3_loader::load_tags( const std::string& tag )
[149]286{
287        md3_t* md3 = (md3_t*)m_md3;
[282]288        key_raw_channel* result = key_raw_channel::create<md3_key>( md3->header.num_frames );
289        // TODO: is this brain damaged in efficiency (loop nest order) or what?
[149]290        for ( sint32 f = 0; f < md3->header.num_frames; ++f )
291        {
292                for ( sint32 i = 0; i < md3->header.num_tags; ++i )
293                {
294                        const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * f];
295                        std::string rname((char*)(rtag.name));
296                        if (rname == tag)
297                        {
[230]298                                vec3 axisx  ( md3_vec3( rtag.axis[0] ) );
299                                vec3 axisz  ( md3_vec3( rtag.axis[1] ) );
300                                vec3 axisy  ( md3_vec3( rtag.axis[2] ) );
301                                vec3 origin ( md3_vec3( rtag.origin )  );
[282]302                                ((md3_key*)(result->data))[f].tform = transform( origin, quat( mat3( axisx, axisy, axisz ) ) );
[149]303                        }
304                }
305
306        }
[282]307        return result;
[149]308}
309
[239]310struct vtx_md3_pn
[224]311{
[239]312        nv::vec3 position;
313        nv::vec3 normal;
314};
[224]315
[239]316struct vtx_md3_t
[224]317{
[239]318        nv::vec2 texcoord;
319};
[224]320
[304]321mesh_data* nv::md3_loader::release_mesh_data( size_t index )
[149]322{
[287]323        mesh_data* data = new mesh_data;
[304]324        release_mesh_frame( data, -1, index );
[287]325        return data;
[149]326}
327
[304]328void nv::md3_loader::release_mesh_frame( mesh_data* data, sint32 frame, sint32 surface )
[153]329{
330        md3_t* md3 = (md3_t*)m_md3;
[304]331        sint32 num_surfaces  = md3->header.num_surfaces;
332        sint32 num_verts     = 0;
[148]333        sint32 current_frame = ( frame == -1 ? 0 : frame );
334        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
[304]335        sint32 current_surf  = ( surface == -1 ? 0 : surface );
336        sint32 surf_count    = ( surface == -1 ? md3->header.num_surfaces : 1 );
337        sint32 index_count   = 0;
[148]338
[304]339        if ( surface >= 0 )
340        {
341                index_count = md3->surfaces[surface].header.num_triangles * 3;
342                num_verts   = md3->surfaces[surface].header.num_verts;
343        }
344        else
345                for ( sint32 i = 0; i < num_surfaces; ++i )
346                {
347                        index_count += md3->surfaces[i].header.num_triangles * 3;
348                        num_verts   += md3->surfaces[i].header.num_verts;
349                }
350
[239]351        mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md3_pn >( num_verts * frame_count );
[304]352        mesh_raw_channel* mc_t  = mesh_raw_channel::create< vtx_md3_t >( num_verts );
353        mesh_raw_channel* ic = mesh_raw_channel::create_index< uint16 >( index_count );
[239]354        vtx_md3_pn* vtx_pn = (vtx_md3_pn*)mc_pn->data;
[304]355        vtx_md3_t*  vtx_t  = (vtx_md3_t*) mc_t->data;
356        uint16*     icp    = (uint16*)ic->data;
[149]357
[304]358        uint32 index  = 0;
359        uint32 iindex = 0;
360        sint32 index_base = 0;
361
362        while ( surf_count > 0 )
363        {
364                const md3_surface_t& surface = md3->surfaces[ current_surf ];
365                const uint32         vcount  = static_cast< uint32 >( surface.header.num_verts );
366                const uint32         tcount  = static_cast< uint32 >( surface.header.num_triangles );
367
368                for (uint32 j = 0; j < vcount; ++j )
369                {
370                        vtx_t[index++].texcoord = md3_texcoord( surface.st[j] );
371                }
372
373                for (size_t j = 0; j < tcount; ++j )
374                {
375                        const md3_triangle_t& t = surface.triangles[j];
376                        icp[iindex++] = static_cast< uint16 >( index_base + t.indexes[0] );
377                        icp[iindex++] = static_cast< uint16 >( index_base + t.indexes[1] );
378                        icp[iindex++] = static_cast< uint16 >( index_base + t.indexes[2] );
379                }
380                index_base += surface.header.num_verts;
381                ++current_surf;
382                --surf_count;
383        }
384
385        index = 0;
[148]386        while ( frame_count > 0 )
387        {
[304]388                current_surf  = ( surface == -1 ? 0 : surface );
389                surf_count    = ( surface == -1 ? md3->header.num_surfaces : 1 );
390
391                while ( surf_count > 0 )
[148]392                {
[304]393                        md3_surface_t& surface = md3->surfaces[current_surf];
[148]394                        sint32         vcount  = surface.header.num_verts;
395                        sint32         offset  = vcount * current_frame;
[149]396                        sint32         limit   = vcount + offset;
397                        for (sint32 j = offset; j < limit; ++j )
[148]398                        {
[149]399                                md3_vertex_t& v = surface.vertices[j];
[239]400                                vtx_pn[index].position = vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE );
401                                vtx_pn[index].normal   = s_normal_cache[ v.normal ];
402                                index++;
[148]403                        }
[304]404                        ++current_surf;
405                        --surf_count;
[148]406                }
407                ++current_frame;
408                --frame_count;
409        }
410
[287]411        data->set_name( (char*)md3->header.name );
412        data->add_channel( mc_pn );
413        data->add_channel( mc_t );
414        data->add_channel( ic );
[148]415}
[153]416
[291]417mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data( size_t )
[239]418{
419        md3_t* md3 = (md3_t*)m_md3;
[287]420        uint32 node_count = md3->header.num_tags;
421        if ( node_count == 0 ) return nullptr;;
422        mesh_node_data* nodes = new mesh_node_data[ node_count ];
423        for ( uint32 i = 0; i < node_count; ++i )
[239]424        {
[285]425                const md3_tag_t& rtag = md3->tags[i];
[239]426                std::string name( (char*)(rtag.name) );
[285]427
[287]428                nodes[i].transform = mat4();
429                nodes[i].name      = name;
430                nodes[i].parent_id = -1;
431                nodes[i].target_id = -1;
[289]432                nodes[i].data      = new key_data;
[287]433       
434                key_raw_channel* keys = load_tags( name );
435                nodes[i].data->add_channel( keys );
[239]436        }
[287]437        return new mesh_nodes_data( "tags", node_count, nodes );
[239]438}
439
[287]440mesh_data_pack* nv::md3_loader::release_mesh_data_pack()
[285]441{
[304]442        md3_t* md3 = (md3_t*)m_md3;
443        uint32 count = 1;
444        mesh_data* data = nullptr;
445        if ( m_merge_all )
446        {
447                data = new mesh_data[1];
448                release_mesh_frame( &data[0], -1, -1 );
449                data[0].set_name( (char*)md3->header.name );
450        }
451        else
452        {
453                count = md3->header.num_surfaces;
454                data = new mesh_data[ count ];
455                for ( uint32 i = 0; i < count; ++i )
456                {
457                        release_mesh_frame( &data[i], -1, i );
458                        data[i].set_name( (char*)md3->surfaces[i].header.name );
459                }
460        }
461        return new mesh_data_pack( count, data, release_mesh_nodes_data() );
[285]462}
463
[239]464size_t md3_loader::get_max_frames() const
465{
466        return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames );
467}
Note: See TracBrowser for help on using the repository browser.