88#include " math/Matrix.h"
99
1010#include < string.h>
11+ #include < nstd/File.hpp>
1112
1213extern bool g_extract_dmodels;
14+ extern bool g_export_worldUnityScript;
15+
1316extern String g_levname_moddir;
1417extern String g_levname_texdir;
1518
@@ -32,14 +35,15 @@ void WriteMODELToObjStream(IVirtualStream* pStream, MODEL* model, int modelSize,
3235 if (debugInfo)
3336 pStream->Print (" #vert count %d\r\n " , model->num_vertices );
3437
35- pStream->Print (" g %s_%d \r\n " , name_prefix, model_index );
36- pStream->Print (" o %s_%d \r\n " , name_prefix, model_index );
38+ pStream->Print (" g %s \r\n " , name_prefix);
39+ pStream->Print (" o %s \r\n " , name_prefix);
3740
3841 MODEL* vertex_ref = model;
3942
4043 if (model->instance_number > 0 ) // car models have vertex_ref=0
4144 {
42- pStream->Print (" #vertex data ref model: %d (count = %d)\r\n " , model->instance_number , model->num_vertices );
45+ if (debugInfo)
46+ pStream->Print (" #vertex data ref model: %d (count = %d)\r\n " , model->instance_number , model->num_vertices );
4347
4448 ModelRef_t* ref = g_levModels.GetModelByIndex (model->instance_number );// , regModels);
4549
@@ -52,11 +56,17 @@ void WriteMODELToObjStream(IVirtualStream* pStream, MODEL* model, int modelSize,
5256 vertex_ref = ref->model ;
5357 }
5458
59+ // export scaling
60+ Vector3D export_scale (-EXPORT_SCALING, -EXPORT_SCALING, EXPORT_SCALING);
61+
62+ if (g_export_worldUnityScript)
63+ export_scale = Vector3D (-EXPORT_SCALING, -EXPORT_SCALING, -EXPORT_SCALING);
64+
5565 // store vertices
5666 for (int i = 0 ; i < vertex_ref->num_vertices ; i++)
5767 {
5868 SVECTOR* vert = vertex_ref->pVertex (i);
59- Vector3D sfVert = Vector3D (vert->x * -EXPORT_SCALING , vert->y * -EXPORT_SCALING , vert->z * EXPORT_SCALING) ;
69+ Vector3D sfVert = Vector3D (vert->x , vert->y , vert->z ) * export_scale ;
6070
6171 sfVert = (translation * Vector4D (sfVert, 1 .0f )).xyz ();
6272
@@ -70,7 +80,7 @@ void WriteMODELToObjStream(IVirtualStream* pStream, MODEL* model, int modelSize,
7080 for (int i = 0 ; i < vertex_ref->num_point_normals ; i++)
7181 {
7282 SVECTOR* norm = vertex_ref->pPointNormal (i);
73- Vector3D sfNorm = Vector3D (norm->x * -EXPORT_SCALING , norm->y * -EXPORT_SCALING , norm->z * EXPORT_SCALING) ;
83+ Vector3D sfNorm = Vector3D (norm->x , norm->y , norm->z ) * export_scale ;
7484
7585 pStream->Print (" vn %g %g %g\r\n " ,
7686 sfNorm.x ,
@@ -295,7 +305,13 @@ void ExportDMODELToOBJ(MODEL* model, const char* model_name, int model_index, in
295305
296306 fstr.Print (" mtllib MODELPAGES.mtl\r\n " );
297307
298- WriteMODELToObjStream (&fstr, model, modelSize, model_index, model_name, true );
308+ #ifdef _DEBUG
309+ bool debugInfo = true ;
310+ #else
311+ bool debugInfo = false ;
312+ #endif
313+
314+ WriteMODELToObjStream (&fstr, model, modelSize, model_index, File::basename (String::fromCString (model_name)), debugInfo);
299315
300316 // success
301317 fclose (mdlFile);
@@ -342,26 +358,16 @@ void ExportAllModels()
342358
343359 for (int i = 0 ; i < MAX_MODELS; i++)
344360 {
345- ModelRef_t* modelRef = g_levModels.GetModelByIndex (i);
361+ ModelRef_t* ref = g_levModels.GetModelByIndex (i);
346362
347- if (!modelRef ->model )
363+ if (!ref || !ref ->model )
348364 continue ;
349365
350- String modelFileName (String::fromPrintf (" %s/ZMOD_%d" , (char *)g_levname_moddir, i));
351-
352- if (modelRef->name && modelRef->name [0 ] != 0 )
353- modelFileName = String::fromPrintf (" %s/%d_%s" , (char *)g_levname_moddir, i, modelRef->name );
366+ String modelName = strlen (ref->name ) > 0 ? String::fromCString (ref->name ) : String::fromPrintf (" MOD_%d" , ref->index );
367+ String modelPath = String::fromPrintf (" %s/%s" , (char *)g_levname_moddir, (char *)modelName);
354368
355369 // export model
356- ExportDMODELToOBJ (modelRef->model , modelFileName, i, modelRef->size );
357-
358- // save original dmodel2
359- FILE* dFile = fopen (String::fromPrintf (" %s.dmodel" , modelFileName), " wb" );
360- if (dFile)
361- {
362- fwrite (modelRef->model , modelRef->size , 1 , dFile);
363- fclose (dFile);
364- }
370+ ExportDMODELToOBJ (ref->model , modelPath, i, ref->size );
365371 }
366372}
367373
0 commit comments