1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-29 07:37: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> #include <iostream>
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
struct face
{
int32_t f1, f2, f3;
};
int parseBlockEntry( char* data, std::vector<PCB_BLOCK_ENTRY>& entries, int gOff ) 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; 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 ) std::string zoneNameToPath( const std::string& name )
{ {
char teri = name[0]; 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 &test_file1 = data1.get_file( "bg/ffxiv/" + zonePath + "/collision/list.pcb" );
auto &section1 = test_file1->access_data_sections().at( 0 ); 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; int offset1 = 0x20;
for( ; ; ) for( ; ; )
{ {
@ -180,22 +161,36 @@ int main( int argc, char* argv[] )
break; break;
} }
} }
LGB_FILE bgLgb(&section[0]); LGB_FILE bgLgb(&section[0]);
int total_vertices = 0;
int total_indices = 0;
int max_index = 0; int max_index = 0;
std::vector<vec3> vertices; std::vector<std::string> vertices;
std::vector<int> indices; std::vector<std::string> indices;
char *data; char *data;
int counter = 0; int counter = 0;
// dont bother if we cant write to a file
auto fp_out = fopen( (zoneName + ".obj").c_str(), "w" );
if( fp_out )
{
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, PCB_FILE> pcbFiles;
std::map<std::string, uint32_t> objCount;
auto loadPcbFile = [&]( const std::string& fileName) auto loadPcbFile = [&]( const std::string& fileName )
{ {
try try
{ {
@ -239,14 +234,13 @@ int main( int argc, char* argv[] )
std::cout << "Unable to load collision mesh " << fileName << "\n\tError:\n\t" << e.what() << "\n"; std::cout << "Unable to load collision mesh " << fileName << "\n\tError:\n\t" << e.what() << "\n";
} }
}; };
auto pushVerts = [&]( const PCB_FILE& pcb_file, const vec3* scale = nullptr, const vec3* rotation = nullptr, const vec3* translation = nullptr) 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;
for( auto &entry : pcb_file.entries ) for( auto &entry : pcb_file.entries )
{ {
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 x_base = abs( float( entry.header.x1 - entry.header.x ) );
float y_base = abs( float( entry.header.y1 - entry.header.y ) ); float y_base = abs( float( entry.header.y1 - entry.header.y ) );
float z_base = abs( float( entry.header.z1 - entry.header.z ) ); float z_base = abs( float( entry.header.z1 - entry.header.z ) );
@ -258,19 +252,16 @@ int main( int argc, char* argv[] )
v.x *= scale->x; v.x *= scale->x;
v.y *= scale->y; v.y *= scale->y;
v.z *= scale->z; v.z *= scale->z;
}
if( rotation )
{
v = v * matrix4::rotateX( rotation->x ); v = v * matrix4::rotateX( rotation->x );
v = v * matrix4::rotateY( rotation->y ); v = v * matrix4::rotateY( rotation->y );
v = v * matrix4::rotateZ( rotation->z ); v = v * matrix4::rotateZ( rotation->z );
}
if( translation )
{
v.x += translation->x; v.x += translation->x;
v.y += translation->y; v.y += translation->y;
v.z += translation->z; v.z += translation->z;
} }
}; };
for( auto &vertex_list : entry.data.vertices ) for( auto &vertex_list : entry.data.vertices )
@ -280,42 +271,44 @@ int main( int argc, char* argv[] )
v.y = vertex_list.y; v.y = vertex_list.y;
v.z = vertex_list.z; v.z = vertex_list.z;
makeTranslation( v ); makeTranslation( v );
vertices.push_back( v ); fprintf( fp_out, "v %f %f %f\n", v.x, v.y, v.z );
} }
for( const auto &link : entry.data.vertices_i16 ) for( const auto &link : entry.data.vertices_i16 )
{ {
vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF ); vec3 v( float( link.x ) / 0xFFFF, float( link.y ) / 0xFFFF, float( link.z ) / 0xFFFF );
float x = float( link.x ); v.x = v.x * x_base + entry.header.x;
float y = float( link.y ); v.y = v.y * y_base + entry.header.y;
float z = float( link.z ); v.z = v.z * z_base + entry.header.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 ); makeTranslation( v );
vertices.push_back( 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 ) for( const auto &index : entry.data.indices )
{ {
//if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 ) //if( index.index[0] != 0 || index.index[1] != 0 || index.index[2] != 0 )
{ {
indices.push_back( int( index.index[0] ) + max_index ); face f;
indices.push_back( int( index.index[1] ) + max_index ); f.f1 = index.index[0] + max_index;
indices.push_back( int( index.index[2] ) + 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; //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 = vertices.size();
max_index += entry.data.vertices.size() + entry.data.vertices_i16.size();
} }
}; };
for( const auto& fileName : stringList ) for( const auto& fileName : stringList )
{ {
loadPcbFile( fileName ); loadPcbFile( fileName );
pushVerts( pcbFiles[fileName] ); pushVerts( pcbFiles[fileName], fileName );
} }
std::cout << "Writing obj file " << "\n"; std::cout << "Writing obj file " << "\n";
std::cout << bgLgb.groups.size() << " entries " << "\n"; std::cout << bgLgb.groups.size() << " entries " << "\n";
@ -326,9 +319,9 @@ int main( int argc, char* argv[] )
if( !pEntry ) if( !pEntry )
continue; continue;
auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>( pEntry.get() ); auto pBgParts = dynamic_cast<LGB_BGPARTS_ENTRY*>(pEntry.get());
if( !pBgParts || pBgParts->collisionFileName.empty() if( !pBgParts || pBgParts->collisionFileName.empty()
|| std::find(stringList.begin(), stringList.end(), pBgParts->collisionFileName) != stringList.end()) || std::find( stringList.begin(), stringList.end(), pBgParts->collisionFileName ) != stringList.end() )
continue; continue;
auto it = pcbFiles.find( pBgParts->collisionFileName ); auto it = pcbFiles.find( pBgParts->collisionFileName );
if( it == pcbFiles.end() ) if( it == pcbFiles.end() )
@ -344,10 +337,10 @@ int main( int argc, char* argv[] )
const auto* translation = &pBgParts->header.translation; const auto* translation = &pBgParts->header.translation;
const auto& pcb_file = it->second; const auto& pcb_file = it->second;
pushVerts( pcb_file, scale, rotation, translation ); pushVerts( pcb_file, pBgParts->collisionFileName, scale, rotation, translation );
}
} }
} }
} }
toFile("test.obj", vertices, indices);
return 0; return 0;
} }