unsigned int MDCSurface_read(Surface& surface, const byte* buffer) { mdcSurface_t mdcSurface; { PointerInputStream inputStream(buffer); istream_read_mdcSurface(inputStream, mdcSurface); } { surface.vertices().reserve(mdcSurface.numVerts); PointerInputStream xyzStream(buffer + mdcSurface.ofsXyzNormals); PointerInputStream stStream(buffer + mdcSurface.ofsSt); // read verts into vertex array - xyz, st, normal for(std::size_t i = 0; i < mdcSurface.numVerts; i++) { mdcXyzNormal_t mdcXyzNormal; istream_read_mdcXyzNormal(xyzStream, mdcXyzNormal); mdcSt_t mdcSt; istream_read_mdcSt(stStream, mdcSt); surface.vertices().push_back( ArbitraryMeshVertex( Vertex3f( mdcXyzNormal.xyz[0] * MDC_XYZ_SCALE, mdcXyzNormal.xyz[1] * MDC_XYZ_SCALE, mdcXyzNormal.xyz[2] * MDC_XYZ_SCALE), DecodeNormal(reinterpret_cast<byte*>(&mdcXyzNormal.normal)), TexCoord2f(mdcSt.st[0], mdcSt.st[1]) ) ); } } { surface.indices().reserve(mdcSurface.numTriangles * 3); PointerInputStream triangleStream(buffer + mdcSurface.ofsTriangles); for(std::size_t i = 0; i < mdcSurface.numTriangles; i++) { mdcTriangle_t triangle; istream_read_mdcTriangle(triangleStream, triangle); surface.indices().insert(triangle.indexes[0]); surface.indices().insert(triangle.indexes[1]); surface.indices().insert(triangle.indexes[2]); } } { mdcShader_t shader; PointerInputStream inputStream(buffer + mdcSurface.ofsShaders); istream_read_mdcShader(inputStream, shader); surface.setShader(shader.name); } surface.updateAABB(); return mdcSurface.ofsEnd; }
void MD2Surface_read( Model& model, const byte* buffer, ArchiveFile& file ){ Surface& surface = model.newSurface(); md2Header_t header; { PointerInputStream inputStream( buffer ); istream_read_md2Header( inputStream, header ); } { md2Frame_t frame; PointerInputStream frameStream( buffer + header.ofs_frames ); istream_read_md2Frame( frameStream, frame ); surface.indices().reserve( header.num_tris * 3 ); Array<md2XyzNormal_t> md2Xyz( header.num_xyz ); for ( Array<md2XyzNormal_t>::iterator i = md2Xyz.begin(); i != md2Xyz.end(); ++i ) { istream_read_md2XyzNormal( frameStream, *i ); } Array<md2St_t> md2St( header.num_st ); PointerInputStream stStream( buffer + header.ofs_st ); for ( Array<md2St_t>::iterator i = md2St.begin(); i != md2St.end(); ++i ) { istream_read_md2St( stStream, *i ); } UniqueVertexBuffer<ArbitraryMeshVertex> inserter( surface.vertices() ); inserter.reserve( header.num_st ); PointerInputStream triangleStream( buffer + header.ofs_tris ); for ( int i = 0; i < header.num_tris; ++i ) { md2Triangle_t triangle; istream_read_md2Triangle( triangleStream, triangle ); surface.indices().insert( inserter.insert( MD2Vertex_construct( &header, &frame, &md2Xyz[triangle.index_xyz[0]], &md2St[triangle.index_st[0]] ) ) ); surface.indices().insert( inserter.insert( MD2Vertex_construct( &header, &frame, &md2Xyz[triangle.index_xyz[1]], &md2St[triangle.index_st[1]] ) ) ); surface.indices().insert( inserter.insert( MD2Vertex_construct( &header, &frame, &md2Xyz[triangle.index_xyz[2]], &md2St[triangle.index_st[2]] ) ) ); } } char skinname[MD2_MAX_SKINNAME]; char skinnameRelative[MD2_MAX_SKINNAME]; char path[MD2_MAX_SKINNAME]; int i = MD2_MAX_SKINNAME; PointerInputStream inputStream( buffer + header.ofs_skins ); inputStream.read( reinterpret_cast<byte*>( skinnameRelative ), MD2_MAX_SKINNAME ); // relative texture path - allows moving of models in game dir structure without changing the skinpath // e.g. used in ufo:ai if ( skinnameRelative[0] == '.' ) { strncpy( path, file.getName(), MD2_MAX_SKINNAME ); for (; i--; ) { // skip filename if ( path[i] == '/' || path[i] == '\\' ) { break; } path[i] = '\0'; } // globalErrorStream() << "modified skinname: " << path << " (path) and " << skinnameRelative << " (texture)" << "\n"; snprintf( skinname, MD2_MAX_SKINNAME, "%s%s", path, &skinnameRelative[1] ); // globalErrorStream() << skinname << "\n"; } else { strcpy( skinname, skinnameRelative ); } surface.setShader( skinname ); surface.updateAABB(); }