1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 22:17:45 +00:00

cleaned up collision map exporter

- export by object
This commit is contained in:
Tahir Akhlaq 2017-10-17 15:14:48 +01:00
parent 9ec749a964
commit ae1b8a393e

View file

@ -15,6 +15,10 @@
#include <iostream>
namespace fs = std::experimental::filesystem;
struct face
{
int32_t f1, f2, f3;
};
int parseBlockEntry( char* data, std::vector<PCB_BLOCK_ENTRY>& entries, int gOff )
{
@ -72,29 +76,6 @@ int parseBlockEntry( char* data, std::vector<PCB_BLOCK_ENTRY>& entries, int gOff
return 0;
}
void toFile(const std::string& fileName, const std::vector<vec3>& vertices, const std::vector<int>& indices)
{
auto fp_out = fopen(fileName.c_str(), "w");
if (fp_out)
{
fprintf(fp_out, "\n");
fclose(fp_out);
}
fp_out = fopen(fileName.c_str(), "ab+");
if (fp_out)
{
for (const auto& v : vertices)
{
fprintf(fp_out, "v %f %f %f\n", v.x, v.y, v.z);
}
for (auto i = 0; i < indices.size(); i += 3)
{
fprintf(fp_out, "f %i %i %i\n", indices[i] + 1, indices[i + 1] + 1, indices[i + 2] + 1);
}
fclose(fp_out);
}
}
std::string zoneNameToPath( const std::string& name )
{
char teri = name[0];
@ -162,7 +143,7 @@ int main( int argc, char* argv[] )
auto &test_file1 = data1.get_file( "bg/ffxiv/" + zonePath + "/collision/list.pcb" );
auto &section1 = test_file1->access_data_sections().at( 0 );
std::string path = "bg/ffxiv/roc_r1/fld/r1f1/collision/";
std::string path = "bg/ffxiv/" + zonePath + "/collision/";
int offset1 = 0x20;
for( ; ; )
{
@ -180,174 +161,186 @@ int main( int argc, char* argv[] )
break;
}
}
LGB_FILE bgLgb(&section[0]);
int total_vertices = 0;
int total_indices = 0;
int max_index = 0;
std::vector<vec3> vertices;
std::vector<int> indices;
std::vector<std::string> vertices;
std::vector<std::string> indices;
char *data;
int counter = 0;
std::map<std::string, PCB_FILE> pcbFiles;
auto loadPcbFile = [&]( const std::string& fileName)
// dont bother if we cant write to a file
auto fp_out = fopen( (zoneName + ".obj").c_str(), "w" );
if( fp_out )
{
try
fprintf( fp_out, "\n" );
fclose( fp_out );
}
else
{
throw std::exception( "Cannot create new file. Run as admin." );
return 0;
}
fp_out = fopen( (zoneName + ".obj").c_str(), "ab+" );
if( fp_out )
{
std::map<std::string, PCB_FILE> pcbFiles;
std::map<std::string, uint32_t> objCount;
auto loadPcbFile = [&]( const std::string& fileName )
{
//std::cout << fileName << " ";
auto file = data1.get_file( fileName );
auto sections = file->get_data_sections();
auto dataSection = &sections.at( 0 )[0];
try
{
//std::cout << fileName << " ";
auto file = data1.get_file( fileName );
auto sections = file->get_data_sections();
auto dataSection = &sections.at( 0 )[0];
//std::cout << sections.size() << "\n";
//std::cout << sections.size() << "\n";
uint32_t offset = 0;
uint32_t offset = 0;
uint32_t groupCount = 0;
PCB_FILE pcb_file;
memcpy( &pcb_file.header, &dataSection[0], sizeof( pcb_file.header ) );
offset += sizeof( pcb_file.header );
bool isgroup = true;
while( isgroup )
{
PCB_BLOCK_ENTRY block_entry;
memcpy( &block_entry.header, &dataSection[0] + offset, sizeof( block_entry.header ) );
isgroup = block_entry.header.type == 0x30 ? true : false;
//printf( "BLOCKHEADER_%X: type: %i, group_size: %i\n", offset, block_entry.header.type, block_entry.header.group_size );
//
if( isgroup )
{
parseBlockEntry( &dataSection[0] + offset + 0x30, pcb_file.entries, offset );
offset += block_entry.header.group_size;
}
else
{
parseBlockEntry( &dataSection[0] + offset, pcb_file.entries, offset );
}
groupCount++;
}
pcbFiles.insert( std::make_pair( fileName, pcb_file ) );
}
catch( std::exception& e )
{
std::cout << "Unable to load collision mesh " << fileName << "\n\tError:\n\t" << e.what() << "\n";
}
};
auto pushVerts = [&]( const PCB_FILE& pcb_file, const std::string& name, const vec3* scale = nullptr, const vec3* rotation = nullptr, const vec3* translation = nullptr )
{
std::string name2 = (name + "_" + std::to_string( objCount[name]++ ));
fprintf( fp_out, "o %s\n", name2.c_str() );
uint32_t groupCount = 0;
PCB_FILE pcb_file;
memcpy( &pcb_file.header, &dataSection[0], sizeof( pcb_file.header ) );
offset += sizeof( pcb_file.header );
bool isgroup = true;
while( isgroup )
for( auto &entry : pcb_file.entries )
{
PCB_BLOCK_ENTRY block_entry;
memcpy( &block_entry.header, &dataSection[0] + offset, sizeof( block_entry.header ) );
isgroup = block_entry.header.type == 0x30 ? true : false;
float x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) );
//printf( "BLOCKHEADER_%X: type: %i, group_size: %i\n", offset, block_entry.header.type, block_entry.header.group_size );
//
if( isgroup )
auto makeTranslation = [&]( vec3& v )
{
parseBlockEntry( &dataSection[0] + offset + 0x30, pcb_file.entries, offset );
offset += block_entry.header.group_size;
}
else
if( scale )
{
v.x *= scale->x;
v.y *= scale->y;
v.z *= scale->z;
v = v * matrix4::rotateX( rotation->x );
v = v * matrix4::rotateY( rotation->y );
v = v * matrix4::rotateZ( rotation->z );
v.x += translation->x;
v.y += translation->y;
v.z += translation->z;
}
};
for( auto &vertex_list : entry.data.vertices )
{
parseBlockEntry( &dataSection[0] + offset, pcb_file.entries, offset );
vec3 v;
v.x = vertex_list.x;
v.y = vertex_list.y;
v.z = vertex_list.z;
makeTranslation( v );
fprintf( fp_out, "v %f %f %f\n", v.x, v.y, v.z );
}
groupCount++;
for( const auto &link : entry.data.vertices_i16 )
{
vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF );
v.x = v.x * x_base + entry.header.x;
v.y = v.y * y_base + entry.header.y;
v.z = v.z * z_base + entry.header.z;
makeTranslation( v );
fprintf( fp_out, "v %f %f %f\n", v.x, v.y, v.z );
}
//fprintf( fp_out, "g %s_", (name2 + "_" + std::to_string( groupCount++ )).c_str() );
for( const auto &index : entry.data.indices )
{
//if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 )
{
face f;
f.f1 = index.index[0] + max_index;
f.f2 = index.index[1] + max_index;
f.f3 = index.index[2] + max_index;
fprintf( fp_out, "f %i %i %i\n", f.f1 + 1, f.f2 + 1, f.f3 + 1 );
}
//std::cout << std::to_string( index.unknown[0] )<< " " << std::to_string( index.unknown[1] )<< " " << std::to_string( index.unknown[2]) << std::endl;
}
//max_index = vertices.size();
max_index += entry.data.vertices.size() + entry.data.vertices_i16.size();
}
pcbFiles.insert( std::make_pair( fileName, pcb_file ) );
}
catch( std::exception& e )
};
for( const auto& fileName : stringList )
{
std::cout << "Unable to load collision mesh " << fileName << "\n\tError:\n\t" << e.what() << "\n";
loadPcbFile( fileName );
pushVerts( pcbFiles[fileName], fileName );
}
};
auto pushVerts = [&]( const PCB_FILE& pcb_file, const vec3* scale = nullptr, const vec3* rotation = nullptr, const vec3* translation = nullptr)
{
for( auto &entry : pcb_file.entries )
std::cout << "Writing obj file " << "\n";
std::cout << bgLgb.groups.size() << " entries " << "\n";
for( const auto& group : bgLgb.groups )
{
total_vertices += entry.header.num_vertices;
total_vertices += entry.header.num_v16;
total_indices += entry.header.num_indices;
float x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) );
auto makeTranslation = [&]( vec3& v )
for( const auto pEntry : group.entries )
{
if( scale )
if( !pEntry )
continue;
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>(pEntry.get());
if( !pBgParts || pBgParts->collisionFileName.empty()
|| std::find( stringList.begin(), stringList.end(), pBgParts->collisionFileName ) != stringList.end() )
continue;
auto it = pcbFiles.find( pBgParts->collisionFileName );
if( it == pcbFiles.end() )
{
v.x *= scale->x;
v.y *= scale->y;
v.z *= scale->z;
loadPcbFile( pBgParts->collisionFileName );
}
if( rotation )
if( it != pcbFiles.end() )
{
v = v * matrix4::rotateX( rotation->x );
v = v * matrix4::rotateY( rotation->y );
v = v * matrix4::rotateZ( rotation->z );
//std::cout << pBgParts->collisionFileName << "\n";
const auto* scale = &pBgParts->header.scale;
const auto* rotation = &pBgParts->header.rotation;
const auto* translation = &pBgParts->header.translation;
const auto& pcb_file = it->second;
pushVerts( pcb_file, pBgParts->collisionFileName, scale, rotation, translation );
}
if( translation )
{
v.x += translation->x;
v.y += translation->y;
v.z += translation->z;
}
};
for( auto &vertex_list : entry.data.vertices )
{
vec3 v;
v.x = vertex_list.x;
v.y = vertex_list.y;
v.z = vertex_list.z;
makeTranslation( v );
vertices.push_back( v );
}
for( const auto &link : entry.data.vertices_i16 )
{
vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF );
float x = float( link.x );
float y = float( link.y );
float z = float( link.z );
v.x = (x / 0xFFFF) * x_base + entry.header.x;
v.y = (y / 0xFFFF) * y_base + entry.header.y;
v.z = (z / 0xFFFF) * z_base + entry.header.z;
makeTranslation( v );
vertices.push_back( v );
}
for( const auto &index : entry.data.indices )
{
//if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 )
{
indices.push_back( int( index.index[0] ) + max_index );
indices.push_back( int( index.index[1] ) + max_index );
indices.push_back( int( index.index[2] ) + max_index );
}
//std::cout << std::to_string( index.unknown[0] )<< " " << std::to_string( index.unknown[1] )<< " " << std::to_string( index.unknown[2]) << std::endl;
}
max_index = vertices.size();
}
};
for( const auto& fileName : stringList )
{
loadPcbFile( fileName );
pushVerts( pcbFiles[fileName] );
}
std::cout << "Writing obj file " << "\n";
std::cout << bgLgb.groups.size() << " entries " << "\n";
for( const auto& group : bgLgb.groups )
{
for( const auto pEntry : group.entries )
{
if( !pEntry )
continue;
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>( pEntry.get() );
if( !pBgParts || pBgParts->collisionFileName.empty()
|| std::find(stringList.begin(), stringList.end(), pBgParts->collisionFileName) != stringList.end())
continue;
auto it = pcbFiles.find( pBgParts->collisionFileName );
if( it == pcbFiles.end() )
{
loadPcbFile( pBgParts->collisionFileName );
}
if( it != pcbFiles.end() )
{
//std::cout << pBgParts->collisionFileName << "\n";
const auto* scale = &pBgParts->header.scale;
const auto* rotation = &pBgParts->header.rotation;
const auto* translation = &pBgParts->header.translation;
const auto& pcb_file = it->second;
pushVerts( pcb_file, scale, rotation, translation );
}
}
}
toFile("test.obj", vertices, indices);
return 0;
}