Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global Config =
- {
- InputDir = "x_files",
- OutputDir = "x_dff",
- };
- include_library( "fmt" );
- include_library( "io" );
- include_library( "string" );
- include_file( "utils.sgs" );
- /* utils.sgs part */
- function do_nullterm( str )
- {
- return string_part( str, 0, string_find( str, "\0" ) );
- }
- function rw_read_chunk( file )
- {
- header = fmt_unpack( "3l", file.read( 12 ) );
- data = "";
- ch = [];
- if( header[0] != 1 && header[0] != 2 && header[0] <= 0xff )
- {
- end = file.offset + header[1];
- while( file.offset < end )
- ch.push( rw_read_chunk( file ) );
- }
- else
- {
- data = file.read( header[1] );
- }
- return { type = header[0], size = header[1], data = data, ch = ch };
- }
- function rw_parse( path )
- {
- file = io_file( path, FILE_READ );
- return rw_read_chunk( file, file.size );
- }
- /* end */
- global
- DFF_GEOM_TRISTRIP = 1,
- DFF_GEOM_HASVERTS = 2,
- DFF_GEOM_HASUV = 4,
- DFF_GEOM_HASCOLORS = 8,
- DFF_GEOM_HASNORMALS = 16,
- DFF_GEOM_DYNLIGHTING = 32,
- DFF_GEOM_MULMATCOLOR = 64,
- DFF_GEOM_HASUV2 = 128;
- function OpenDFFFile( path )
- {
- file = rw_parse( path );
- frames = [];
- geometry = [];
- foreach( ch : file.ch )
- {
- if( ch.type == 0x0E )
- {
- data = ch.ch[0].data;
- cnt = fmt_unpack( "l", data )[0];
- off = 4;
- for( i = 0; i < cnt; ++i )
- {
- m_rot = fmt_unpack( "9f", string_part( data, off ) );
- off += 4 * 9;
- m_pos = fmt_unpack( "3f", string_part( data, off ) );
- off += 4 * 3;
- afd = fmt_unpack( "-ll", string_part( data, off ) );
- off += 8;
- F = { rotation = m_rot, position = m_pos, parent = afd[0] };
- frames.push( F );
- }
- }
- if( ch.type == 0x1A )
- {
- foreach( geom : ch.ch )
- {
- if( geom.type != 0x0F )
- continue; // not Geometry
- /*
- GEOMETRY STRUCT
- */
- data = geom.ch[0].data;
- pgeom = fmt_unpack( "wcclll", data );
- G = { flags = pgeom[0], numuvcoords = pgeom[1],
- gnflags = pgeom[2], numtris = pgeom[3], numverts = pgeom[4],
- numframes = pgeom[5] };
- off = 16;
- if( G.flags & DFF_GEOM_HASCOLORS )
- {
- G.colors = fmt_unpack( G.numverts $ "l", string_part( data, off ) );
- off += G.numverts * 4;
- }
- else G.colors = [];
- if( G.flags & DFF_GEOM_HASUV )
- {
- G.uv = fmt_unpack( G.numverts * 2 $ "f", string_part( data, off ) );
- off += G.numverts * 8;
- }
- else G.uv = [];
- G.faces = fmt_unpack( G.numtris * 4 $ "w", string_part( data, off ) );
- off += G.numtris * 8;
- G.boundingsphere = fmt_unpack( "4f", string_part( data, off ) );
- off += 16;
- G.haspn = fmt_unpack( "2l", string_part( data, off ) );
- off += 8;
- if( G.haspn[0] )
- {
- G.verts = fmt_unpack( G.numverts * 3 $ "f", string_part( data, off ) );
- off += G.numverts * 12;
- }
- else G.verts = [];
- if( G.haspn[1] )
- {
- G.normals = fmt_unpack( G.numverts * 3 $ "f", string_part( data, off ) );
- off += G.numverts * 12;
- }
- else G.normals = [];
- /*
- MATERIALS
- */
- G.materials = [];
- foreach( mtl : geom.ch[1].ch )
- {
- if( mtl.type != 0x07 )
- continue; // not Material
- pmtl = fmt_unpack( "llllfff", mtl.ch[0].data );
- M = { color = pmtl[1], hastex = pmtl[3],
- ambient = pmtl[4], specular = pmtl[5], diffuse = pmtl[6] };
- if( M.hastex )
- {
- M.tex_flags = fmt_unpack( "w", mtl.ch[1].ch[0].data )[0];
- M.tex_diffuse = do_nullterm( mtl.ch[1].ch[1].data );
- M.tex_alpha = do_nullterm( mtl.ch[1].ch[2].data );
- }
- G.materials.push( M );
- }
- /*
- "BIN MESH PLG" / SPLITS
- */
- G.splits = [];
- data = geom.ch[2].ch[0].data;
- pspls = fmt_unpack( "lll", data );
- off = 12;
- G.numindices = pspls[2];
- for( i = 0; i < pspls[1]; ++i )
- {
- pspl = fmt_unpack( "ll", string_part( data, off ) );
- off += 8;
- S = { material_id = pspl[1] };
- S.indices = fmt_unpack( pspl[0] $ "l", string_part( data, off ) );
- off += pspl[0] * 4;
- G.splits.push( S );
- }
- geometry.push( G );
- }
- }
- }
- data = { frames = frames, geometry = geometry };
- io_file_write( "dump.dff.txt", dumpvar( data, 1000 ) );
- printvar( data );
- return data;
- }
- OpenDFFFile( "x_files/airportgate.dff" );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement