1 /* -----------------------------------------------------------------------------
3 InterQuake Model - PicoModel Library
5 Copyright (c) 2018-2021, FTE Team <fteqw.org>
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
18 Neither the names of the copyright holders nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ----------------------------------------------------------------------------- */
36 #include "picointernal.h"
38 extern const picoModule_t picoModuleIQM;
40 #define IQM_MAGIC "INTERQUAKEMODEL" //15+null
43 ========================================================================
45 .IQM triangle model file format
47 ========================================================================
78 typedef struct iqmHeader_s {
81 unsigned int filesize;
83 unsigned int num_text, ofs_text;
84 unsigned int num_meshes, ofs_meshes;
85 unsigned int num_vertexarrays, num_vertexes, ofs_vertexarrays;
86 unsigned int num_triangles, ofs_triangles, ofs_neighbors;
87 unsigned int num_joints, ofs_joints;
88 unsigned int num_poses, ofs_poses;
89 unsigned int num_anims, ofs_anims;
90 unsigned int num_frames, num_framechannels, ofs_frames, ofs_bounds;
91 unsigned int num_comment, ofs_comment;
92 unsigned int num_extensions, ofs_extensions;
95 typedef struct iqmmesh_s {
97 unsigned int material;
98 unsigned int first_vertex;
99 unsigned int num_vertexes;
100 unsigned int first_triangle;
101 unsigned int num_triangles;
104 typedef struct iqmvertexarray_s {
112 //is anyone actually going to run this on a big-endian cpu?
113 static iqmHeader_t SwapHeader(const iqmHeader_t *h)
116 r.version = _pico_little_long(h->version);
117 r.filesize = _pico_little_long(h->filesize);
118 r.flags = _pico_little_long(h->flags);
119 r.num_text = _pico_little_long(h->num_text);
120 r.ofs_text = _pico_little_long(h->ofs_text);
121 r.num_meshes = _pico_little_long(h->num_meshes);
122 r.ofs_meshes = _pico_little_long(h->ofs_meshes);
123 r.num_vertexarrays = _pico_little_long(h->num_vertexarrays);
124 r.num_vertexes = _pico_little_long(h->num_vertexes);
125 r.ofs_vertexarrays = _pico_little_long(h->ofs_vertexarrays);
126 r.num_triangles = _pico_little_long(h->num_triangles);
127 r.ofs_triangles = _pico_little_long(h->ofs_triangles);
128 r.ofs_neighbors = _pico_little_long(h->ofs_neighbors);
129 r.num_joints = _pico_little_long(h->num_joints);
130 r.ofs_joints = _pico_little_long(h->ofs_joints);
131 r.num_poses = _pico_little_long(h->num_poses);
132 r.ofs_poses = _pico_little_long(h->ofs_poses);
133 r.num_anims = _pico_little_long(h->num_anims);
134 r.ofs_anims = _pico_little_long(h->ofs_anims);
135 r.num_frames = _pico_little_long(h->num_frames);
136 r.num_framechannels = _pico_little_long(h->num_framechannels);
137 r.ofs_frames = _pico_little_long(h->ofs_frames);
138 r.ofs_bounds = _pico_little_long(h->ofs_bounds);
139 r.num_comment = _pico_little_long(h->num_comment);
140 r.ofs_comment = _pico_little_long(h->ofs_comment);
141 r.num_extensions = _pico_little_long(h->num_extensions);
142 r.ofs_extensions = _pico_little_long(h->ofs_extensions);
147 static int _iqm_canload( PM_PARAMS_CANLOAD ){
150 //make sure there's enough data for the header...
151 if ((size_t)bufSize < sizeof(h))
152 return PICO_PMV_ERROR_SIZE;
153 h = SwapHeader(buffer);
155 //make sure its actually an iqm
156 if (memcmp(h.id, IQM_MAGIC, sizeof(h.id)))
157 return PICO_PMV_ERROR_IDENT;
158 //v1 is flawed, we don't know about anything higher either.
160 return PICO_PMV_ERROR_VERSION;
161 //make sure its not truncated
162 if ((size_t)h.filesize != (size_t)bufSize)
163 return PICO_PMV_ERROR_SIZE;
165 //looks like we can probably use it.
169 // _iqm_load() loads an interquake model file.
170 static picoModel_t *_iqm_load( PM_PARAMS_LOAD ){
171 picoModel_t *picoModel;
172 picoSurface_t *picoSurface;
173 picoShader_t *picoShader;
176 picoVec3_t xyz, normal;
184 const char *stringtable;
186 const unsigned int *tri;
189 if (_iqm_canload(fileName, buffer, bufSize) != PICO_PMV_OK)
191 _pico_printf( PICO_ERROR, "%s is not an IQM File!", fileName );
194 h = SwapHeader(buffer);
195 stringtable = (const char*)buffer + h.ofs_text;
198 if ( h.num_anims != 0 ) {
199 _pico_printf( PICO_WARNING, "%s has animations! Using base pose only.", fileName );
202 /* create new pico model */
203 picoModel = PicoNewModel();
204 if ( picoModel == NULL ) {
205 _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
210 PicoSetModelFrameNum( picoModel, frameNum );
211 PicoSetModelNumFrames( picoModel, 1 ); /* sea */
212 PicoSetModelName( picoModel, fileName );
213 PicoSetModelFileName( picoModel, fileName );
215 for (s = 0; s < h.num_meshes; s++)
217 m = ((const iqmmesh_t*)((const char*)buffer + h.ofs_meshes))[s];
218 m.first_triangle = _pico_little_long(m.first_triangle);
219 m.first_vertex = _pico_little_long(m.first_vertex);
220 m.material = _pico_little_long(m.material);
221 m.name = _pico_little_long(m.name);
222 m.num_triangles = _pico_little_long(m.num_triangles);
223 m.num_vertexes = _pico_little_long(m.num_vertexes);
225 // allocate new pico surface
226 picoSurface = PicoNewSurface( picoModel );
227 if ( picoSurface == NULL ) {
228 _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
229 PicoFreeModel( picoModel );
234 memcpy(skinname, stringtable+m.material, sizeof(skinname));
235 _pico_setfext( skinname, "" );
236 _pico_unixify( skinname );
238 PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
239 PicoSetSurfaceName( picoSurface, stringtable+m.name );
240 picoShader = PicoNewShader( picoModel );
241 if ( picoShader == NULL ) {
242 _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
243 PicoFreeModel( picoModel );
247 PicoSetShaderName( picoShader, skinname );
249 // associate current surface with newly created shader
250 PicoSetSurfaceShader( picoSurface, picoShader );
253 // spew the surface's indexes
254 tri = (const unsigned int *)((const char *)buffer+h.ofs_triangles) + m.first_triangle*3;
255 for (t = 0; t < m.num_triangles*3; t++)
256 PicoSetSurfaceIndex( picoSurface, t, _pico_little_long(*tri++) - m.first_vertex );
258 for ( j = 0; j < h.num_vertexarrays; j++)
260 a = ((const iqmvertexarray_t*)((const char*)buffer + h.ofs_vertexarrays))[j];
261 a.flags = _pico_little_long(a.flags);
262 a.format = _pico_little_long(a.format);
263 a.offset = _pico_little_long(a.offset);
264 a.size = _pico_little_long(a.size);
265 a.type = _pico_little_long(a.type);
270 if (a.format == IQM_FLOAT && a.size >= 3)
272 inf = (const float*)((const char *)buffer + a.offset) + m.first_vertex*a.size;
273 for ( i = 0; i < m.num_vertexes; i++, inf += a.size )
275 xyz[0] = _pico_little_float(inf[0]);
276 xyz[1] = _pico_little_float(inf[1]);
277 xyz[2] = _pico_little_float(inf[2]);
278 PicoSetSurfaceXYZ( picoSurface, i, xyz );
283 if (a.format == IQM_FLOAT && a.size >= 2)
285 inf = (const float*)((const char *)buffer + a.offset) + m.first_vertex*a.size;
286 for ( i = 0; i < m.num_vertexes; i++, inf += a.size )
288 st[0] = _pico_little_float(inf[0]);
289 st[1] = _pico_little_float(inf[1]);
290 PicoSetSurfaceST( picoSurface, 0, i, st );
295 if (a.format == IQM_FLOAT && a.size >= 3)
297 inf = (const float*)((const char *)buffer + a.offset) + m.first_vertex*a.size;
298 for ( i = 0; i < m.num_vertexes; i++, inf += a.size )
300 normal[0] = _pico_little_float(inf[0]);
301 normal[1] = _pico_little_float(inf[1]);
302 normal[2] = _pico_little_float(inf[2]);
303 PicoSetSurfaceNormal( picoSurface, i, normal );
308 if (a.format == IQM_UBYTE && a.size >= 3)
310 inb = (const byte*)((const char *)buffer + a.offset) + m.first_vertex*a.size;
311 for ( i = 0; i < m.num_vertexes; i++, inb += a.size )
316 color[3] = (a.size>=4)?inb[3]:255;
317 PicoSetSurfaceColor( picoSurface, 0, i, color );
320 else if (a.format == IQM_FLOAT && a.size >= 3)
322 inf = (const float*)((const char *)buffer + a.offset) + m.first_vertex*a.size;
323 for ( i = 0; i < m.num_vertexes; i++, inf += a.size )
325 color[0] = inf[0]*255;
326 color[1] = inf[1]*255;
327 color[2] = inf[2]*255;
328 color[3] = (a.size>=4)?inf[3]*255:255;
329 PicoSetSurfaceColor( picoSurface, 0, i, color );
334 case IQM_BLENDINDEXES:
335 case IQM_BLENDWEIGHTS:
337 break; // these attributes are not relevant.
345 /* pico file format module definition */
346 const picoModule_t picoModuleIQM =
348 "0.1", /* module version string */
349 "InterQuake Model", /* module display name */
350 "Spoike", /* author's name */
351 "2018-2021 FTE Team", /* module copyright */
353 "iqm", NULL, NULL, NULL /* default extensions to use */
355 _iqm_canload, /* validation routine */
356 _iqm_load, /* load routine */
357 NULL, /* save validation routine */
358 NULL /* save routine */