/* * LibMorph -- 3d modeler object loader/saver library. * * Copyright (C) 1999 Kuba Winnicki * * by Kuba Winnicki * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA. * */ /* * This set of IO routines is just a shameless mirror of GdkPixBuf IO code/api */ #include #include #include #include static int morph_check_lwob( unsigned char* buf, int size ) { if( size < 12 ) return FALSE; if( buf[ 0 ] == 'F' && buf[ 1 ] == 'O' && buf[ 2 ] == 'R' && buf[ 3 ] == 'M' && buf[ 8 ] == 'L' && buf[ 9 ] == 'W' && buf[ 10 ] == 'O' && buf[ 11 ] == 'B' ) return TRUE; return FALSE; } static int morph_check_3ds( unsigned char* buf, int size ) { if( size < 6 ) return FALSE; if( *( short* )buf == 0x4d4d ) return TRUE; return FALSE; } static struct { unsigned char* module_name; void* module; int ( * check_format )( unsigned char* buf, int size ); // boolean MorphModel* ( * load )( FILE* f, ... ); // ... opt enums int ( * save )( MorphModel* m, ... ); } morph_file_formats[] = { { "lwob", NULL, morph_check_lwob, NULL, NULL }, { "a3ds", NULL, morph_check_3ds, NULL, NULL }, /* { "awv", NULL, morph_check_, NULL, NULL }, { "maya", NULL, morph_check_maya, NULL, NULL }, { "simg", NULL, morph_check_, NULL, NULL }, { "vrml", NULL, morph_check_vrml, NULL, NULL }, */ { NULL, NULL, NULL, NULL, NULL } }; /* FIXME -- 1. generally * 2. where is dlclose? */ static void morph_model_handler_load( int idx ) { unsigned char module_name[ 100 ]; //FIXME //unsigned char* path; void* module, * load_sym, * save_sym; //path = g_module_build_path( LIBMORPH_LIBDIR, module_name ); sprintf( module_name, "%s/libmorph-%s.so", MORPH_LIBDIR, morph_file_formats[ idx ].module_name ); module = dlopen( module_name, RTLD_LAZY ); if( !module ) { fprintf( stderr, "Unable to load module: %s\n", module ); fflush( stderr ); return; } morph_file_formats[ idx ].module = module; morph_file_formats[ idx ].load = dlsym( module, "load_model" ); morph_file_formats[ idx ].save = dlsym( module, "save_model" ); } MorphModel* morph_model_load( const char* filename ) { MorphModel* m; FILE* f; int n, i; unsigned char buf[ 128 ]; f = fopen( filename, "r" ); if( !f ) return NULL; n = fread( &buf, 1, sizeof( buf ), f ); if( !n ) { fclose( f ); return NULL; } for( i = 0; morph_file_formats[ i ].module_name; i++ ) { if( ( *morph_file_formats[ i ].check_format )( buf, 12 ) ) { if( !morph_file_formats[ i ].load ) morph_model_handler_load( i ); if( !morph_file_formats[ i ].load ) { fclose( f ); return NULL; } fseek( f, 0, SEEK_SET ); m = ( *morph_file_formats[ i ].load )( f ); m->surfaces[ 0 ].color[ 0 ] = 0xff; m->surfaces[ 0 ].color[ 1 ] = 0xff; m->surfaces[ 0 ].color[ 2 ] = 0xff; m->surfaces[ 0 ].color[ 3 ] = 0xff; m->surfaces[ 0 ].diffuse = 1.0; m->surfaces[ 0 ].specular = 1.0; m->surfaces[ 0 ].specular_exp = 128; fclose( f ); //g_assert( obj->ref_count != 0 ); // what's that? return m; } } fclose( f ); fprintf( stderr, "Can't find handler for %s\n", filename ); return NULL; }