1 /* -----------------------------------------------------------------------------
5 Copyright (c) 2002, Randy Reddig & seaw0lf
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"
39 /* uncomment when debugging this module */
40 /*#define DEBUG_PM_LWO*/
46 /* helper functions */
47 static const char *lwo_lwIDToStr( unsigned int lwID ){
48 static char lwIDStr[5];
54 lwIDStr[ 0 ] = (char)( ( lwID ) >> 24 );
55 lwIDStr[ 1 ] = (char)( ( lwID ) >> 16 );
56 lwIDStr[ 2 ] = (char)( ( lwID ) >> 8 );
57 lwIDStr[ 3 ] = (char)( ( lwID ) );
65 validates a LightWave Object model file. btw, i use the
66 preceding underscore cause it's a static func referenced
67 by one structure only.
69 static int _lwo_canload( PM_PARAMS_CANLOAD ){
71 unsigned int failID = 0;
75 /* create a new pico memorystream */
76 s = _pico_new_memstream( (const picoByte_t *)buffer, bufSize );
78 return PICO_PMV_ERROR_MEMORY;
81 ret = lwValidateObject( fileName, s, &failID, &failpos );
83 _pico_free_memstream( s );
90 loads a LightWave Object model file.
92 static picoModel_t *_lwo_load( PM_PARAMS_LOAD ){
94 unsigned int failID = 0;
105 int i, j, k, numverts;
107 picoModel_t *picoModel;
108 picoSurface_t *picoSurface;
109 picoShader_t *picoShader;
110 picoVec3_t xyz, normal;
114 int defaultSTAxis[ 2 ];
115 picoVec2_t defaultXYZtoSTScale;
117 picoVertexCombinationHash_t **hashTable;
118 picoVertexCombinationHash_t *vertexCombinationHash;
121 clock_t load_start, load_finish, convert_start, convert_finish;
122 double load_elapsed, convert_elapsed;
124 load_start = clock();
128 if ( frameNum < 0 || frameNum >= 1 ) {
129 _pico_printf( PICO_ERROR, "Invalid or out-of-range LWO frame specified" );
133 /* create a new pico memorystream */
134 s = _pico_new_memstream( (const picoByte_t *)buffer, bufSize );
139 obj = lwGetObject( fileName, s, &failID, &failpos );
141 _pico_free_memstream( s );
144 _pico_printf( PICO_ERROR, "Couldn't load LWO file, failed on ID '%s', position %d", lwo_lwIDToStr( failID ), failpos );
149 convert_start = load_finish = clock();
150 load_elapsed = (double)( load_finish - load_start ) / CLOCKS_PER_SEC;
153 /* -------------------------------------------------
155 ------------------------------------------------- */
157 /* create a new pico model */
158 picoModel = PicoNewModel();
159 if ( picoModel == NULL ) {
160 _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
165 PicoSetModelFrameNum( picoModel, frameNum );
166 PicoSetModelNumFrames( picoModel, 1 );
167 PicoSetModelName( picoModel, fileName );
168 PicoSetModelFileName( picoModel, fileName );
170 /* create all polygons from layer[ 0 ] that belong to this surface */
171 layer = &obj->layer[0];
173 /* warn the user that other layers are discarded */
174 if ( obj->nlayers > 1 ) {
175 _pico_printf( PICO_WARNING, "LWO loader discards any geometry data not in Layer 1 (%d layers found)", obj->nlayers );
178 /* initialize dummy normal */
179 normal[ 0 ] = normal[ 1 ] = normal[ 2 ] = 0.f;
181 /* setup default st map */
182 st[ 0 ] = st[ 1 ] = 0.f; /* st[0] holds max, st[1] holds max par one */
183 defaultSTAxis[ 0 ] = 0;
184 defaultSTAxis[ 1 ] = 1;
185 for ( i = 0; i < 3; i++ )
187 float min = layer->bbox[ i ];
188 float max = layer->bbox[ i + 3 ];
189 float size = max - min;
191 if ( size > st[ 0 ] ) {
192 defaultSTAxis[ 1 ] = defaultSTAxis[ 0 ];
193 defaultSTAxis[ 0 ] = i;
198 else if ( size > st[ 1 ] ) {
199 defaultSTAxis[ 1 ] = i;
203 defaultXYZtoSTScale[ 0 ] = 4.f / st[ 0 ];
204 defaultXYZtoSTScale[ 1 ] = 4.f / st[ 1 ];
206 /* LWO surfaces become pico surfaces */
210 /* allocate new pico surface */
211 picoSurface = PicoNewSurface( picoModel );
212 if ( picoSurface == NULL ) {
213 _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
214 PicoFreeModel( picoModel );
219 /* LWO model surfaces are all triangle meshes */
220 PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
222 /* set surface name */
223 PicoSetSurfaceName( picoSurface, surface->name );
225 /* create new pico shader */
226 picoShader = PicoNewShader( picoModel );
227 if ( picoShader == NULL ) {
228 _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
229 PicoFreeModel( picoModel );
234 /* detox and set shader name */
235 strncpy( name, surface->name, sizeof( name ) - 1 );
236 _pico_first_token( name );
237 _pico_setfext( name, NULL );
238 _pico_unixify( name );
239 PicoSetShaderName( picoShader, name );
241 /* associate current surface with newly created shader */
242 PicoSetSurfaceShader( picoSurface, picoShader );
244 /* copy indices and vertex data */
247 hashTable = PicoNewVertexCombinationHashTable();
249 if ( hashTable == NULL ) {
250 _pico_printf( PICO_ERROR, "Unable to allocate hash table" );
251 PicoFreeModel( picoModel );
256 for ( i = 0, pol = layer->polygon.pol; i < layer->polygon.count; i++, pol++ )
258 /* does this polygon belong to this surface? */
259 if ( pol->surf != surface ) {
263 /* we only support polygons of the FACE type */
264 if ( pol->type != ID_FACE ) {
265 _pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it's type != FACE (%s)", lwo_lwIDToStr( pol->type ) );
269 /* NOTE: LWO has support for non-convex polygons, do we want to store them as well? */
270 if ( pol->nverts != 3 ) {
271 _pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it has != 3 verts (%d)", pol->nverts );
275 for ( j = 0, v = pol->v; j < 3; j++, v++ )
277 pt = &layer->point.pt[ v->index ];
280 xyz[ 0 ] = pt->pos[ 0 ];
281 xyz[ 1 ] = pt->pos[ 2 ];
282 xyz[ 2 ] = pt->pos[ 1 ];
284 /* doom3 lwo data doesn't seem to have smoothing-angle information */
286 if ( surface->smooth <= 0 ) {
287 /* use face normals */
288 normal[ 0 ] = v->norm[ 0 ];
289 normal[ 1 ] = v->norm[ 2 ];
290 normal[ 2 ] = v->norm[ 1 ];
295 /* smooth normals later */
301 st[ 0 ] = xyz[ defaultSTAxis[ 0 ] ] * defaultXYZtoSTScale[ 0 ];
302 st[ 1 ] = xyz[ defaultSTAxis[ 1 ] ] * defaultXYZtoSTScale[ 1 ];
304 color[ 0 ] = (picoByte_t)( surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF );
305 color[ 1 ] = (picoByte_t)( surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF );
306 color[ 2 ] = (picoByte_t)( surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF );
309 /* set from points */
310 for ( k = 0, vm = pt->vm; k < pt->nvmaps; k++, vm++ )
312 if ( vm->vmap->type == LWID_( 'T','X','U','V' ) ) {
314 st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ];
315 st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ];
317 else if ( vm->vmap->type == LWID_( 'R','G','B','A' ) ) {
319 color[ 0 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF );
320 color[ 1 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF );
321 color[ 2 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF );
322 color[ 3 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 3 ] * 0xFF );
326 /* override with polygon data */
327 for ( k = 0, vm = v->vm; k < v->nvmaps; k++, vm++ )
329 if ( vm->vmap->type == LWID_( 'T','X','U','V' ) ) {
331 st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ];
332 st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ];
334 else if ( vm->vmap->type == LWID_( 'R','G','B','A' ) ) {
336 color[ 0 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF );
337 color[ 1 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF );
338 color[ 2 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF );
339 color[ 3 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 3 ] * 0xFF );
343 /* find vertex in this surface and if we can't find it there create it */
344 vertexCombinationHash = PicoFindVertexCombinationInHashTable( hashTable, xyz, normal, st, color );
346 if ( vertexCombinationHash ) {
347 /* found an existing one */
348 PicoSetSurfaceIndex( picoSurface, ( i * 3 + j ), vertexCombinationHash->index );
352 /* it is a new one */
353 vertexCombinationHash = PicoAddVertexCombinationToHashTable( hashTable, xyz, normal, st, color, (picoIndex_t) numverts );
355 if ( vertexCombinationHash == NULL ) {
356 _pico_printf( PICO_ERROR, "Unable to allocate hash bucket entry table" );
357 PicoFreeVertexCombinationHashTable( hashTable );
358 PicoFreeModel( picoModel );
363 /* add the vertex to this surface */
364 PicoSetSurfaceXYZ( picoSurface, numverts, xyz );
366 /* set dummy normal */
367 PicoSetSurfaceNormal( picoSurface, numverts, normal );
370 PicoSetSurfaceColor( picoSurface, 0, numverts, color );
373 PicoSetSurfaceST( picoSurface, 0, numverts, st );
376 PicoSetSurfaceIndex( picoSurface, ( i * 3 + j ), (picoIndex_t) numverts );
383 /* free the hashtable */
384 PicoFreeVertexCombinationHashTable( hashTable );
386 /* get next surface */
387 surface = surface->next;
391 load_start = convert_finish = clock();
397 load_finish = clock();
398 load_elapsed += (double)( load_finish - load_start ) / CLOCKS_PER_SEC;
399 convert_elapsed = (double)( convert_finish - convert_start ) / CLOCKS_PER_SEC;
400 _pico_printf( PICO_NORMAL, "Loaded model in in %-.2f second(s) (loading: %-.2fs converting: %-.2fs)\n", load_elapsed + convert_elapsed, load_elapsed, convert_elapsed );
403 /* return the new pico model */
407 /* pico file format module definition */
408 const picoModule_t picoModuleLWO =
410 "1.0", /* module version string */
411 "LightWave Object", /* module display name */
412 "Arnout van Meer", /* author's name */
413 "2003 Arnout van Meer, 2000 Ernie Wright", /* module copyright */
415 "lwo", NULL, NULL, NULL /* default extensions to use */
417 _lwo_canload, /* validation routine */
418 _lwo_load, /* load routine */
419 NULL, /* save validation routine */
420 NULL /* save routine */