Module: impscgt.cpp

Flat functions
 

EnlightenModelstatic void EnlightenModel(DGeode *model)

Some models have poor diffuse lighting
Esp. SCGT imported cars

ConvertModelstatic void ConvertModel(cstring geoName,cstring trkDir,cstring mapDir)

Convert a VRML model, processing on the fly
Model 'geoName' in dir 'trkDir'.

ConvertSCGTwrlvoid ConvertSCGTwrl()

Import all .wrl files, do NOT add to the scene
Expects files in <trackdir>/SCGT/wrl

FilterRequestbool FilterRequest()

Sets up filter

FilterMatchbool FilterMatch(cstring fname)

Returns TRUE if 'fname' is in the filter

AddModelstatic void AddModel(cstring geoName,cstring trkDir,cstring mapDir)

Adds a VRML model, processing on the fly
Model 'geoName' in dir 'trkDir'.

ImportDOFsvoid ImportDOFs()

Import all .wrl files, add to the scene, and process
accordingly
Expects files in <trackdir>/SCGT



/*
 * Import SCGT, DOF
 * 03-01-2001: Created! (21:07:14)
 * NOTES:
 * - Should perhaps be split into 2 modules, 1 for SCGT, 1 for Racer
 * (C) MarketGraph/RvG
 */

#include "main.h"
#pragma hdrstop
#include <qlib/dir.h>
#include <qlib/debug.h>
DEBUG_ENABLE

/*******
* Help *
*******/
#ifdef OBS
static void EnlightenModel(DGeode *model)
// Some models have poor diffuse lighting
// Esp. SCGT imported cars
{
  int i;
  for(i=0;i<model->materials;i++)
  {
    model->material[i]->SetDiffuseColor(1,1,1,1);
  }
}
#endif

/******************************
* Batch converting WRL -> DOF *
******************************/
static void ConvertModel(cstring geoName,cstring trkDir,cstring mapDir)
// Convert a VRML model, processing on the fly
// Model 'geoName' in dir 'trkDir'.
{
  RTV_Node node;
  char buf[1024];
  
//qdbg("ConvertModel, mapDir='%s'\n",mapDir);

  // Prepare node
  node.type=0;
  node.model=new DGeode(0);
  // Read model
  node.model->SetMapPath(mapDir);
  sprintf(buf,"%s/%s",trkDir,geoName);
  QCV->Select();                  // Make sure textures end in QCV
  DGeodeImportVRML(node.model,buf);
  
  // Make it shine
  DGeodeOptimizeIndices(node.model);
  DGeodePackIndices(node.model);
  //DGeodeRethinkNormals(node.model);
  node.model->DestroyNormals();
  
  // Write model in the same place as the maps
  sprintf(buf,"%s/%s",mapDir,geoName);
  // No ugly caps
  strlwr(buf);
  // New extension
  buf[strlen(buf)-4]=0;
  // Store filename without extension
  node.fileName=QFileBase(buf);
  strcat(buf,".dof");
//qdbg("Export to '%s'\n",buf);
  QFile f(buf,QFile::WRITE);
  node.model->ExportDOF(&f);
  
  // Cleanup
  delete node.model;
}
void ConvertSCGTwrl()
// Import all .wrl files, do NOT add to the scene
// Expects files in <trackdir>/SCGT/wrl
{
  cstring findDir;
  char buf[1024],tdir[1024],mapDir[1024];
  QDirectory *dir;
  QFileInfo fi;
  int i,total,current,pass;
  // Progress
  QRect r(100,100,400,150);
  QProgressDialog *dlg;
  
  i=QMessageBox("Convert all SCGT WRL to DOF",
    "This may take a while, are you sure?");
  if(i!=IDOK)
    return;
    
  // Find the track directory
  findDir=RFindFile(".",track->GetTrackDir());
//qdbg("findDir='%s'\n",findDir);
  strcpy(tdir,findDir);
  // Cut off "."
  tdir[strlen(tdir)-2]=0;
  
  // Find all models in <tdir>/SCGT/wrl/*.wrl
  // Run it twice, first counting for the progress dialog
  strcpy(mapDir,tdir);
  strcat(tdir,"/SCGT/wrl");
  current=total=0;
  dlg=new QProgressDialog(QSHELL,&r,"Converting...",
    "Converting all WRL files to DOF format.");
  dlg->Create();
  for(pass=0;pass<2;pass++)
  {
    dir=new QDirectory(tdir);
qdbg("Perhaps '%s'\n",buf);
    while(dir->ReadNext(buf,&fi))
    {
      // Filter
      if(strstr(buf,".wrl")==0&&strstr(buf,".WRL")==0)
        continue;
      if(pass==0)
      { // Just count
        total++;
      } else
      { // Actually convert
//qdbg("Model '%s'\n",buf);
        dlg->SetProgressText(buf);
        dlg->SetProgress(current,total);
        ConvertModel(buf,tdir,mapDir);
        current++;
        if(!dlg->Poll())break;
      }
    }
    delete dir;
  }
  delete dlg;
}

/************
* Filtering *
************/
static char filterPattern[256];
bool FilterRequest()
// Sets up filter
{
  QRect r(100,100,400,200);
  
  // Default filter
  if(!filterPattern[0])
    strcpy(filterPattern,"TRK2*");
  if(!QDlgString("Import DOF files","Enter filter expression:",
    filterPattern,sizeof(filterPattern)))
    return FALSE;
  return TRUE;
}
bool FilterMatch(cstring fname)
// Returns TRUE if 'fname' is in the filter
{
  // Matches filter?
  if(!QMatch(filterPattern,fname))return FALSE;
  return TRUE;
}


/****************************
* Batch importing DOF files *
****************************/
static void AddModel(cstring geoName,cstring trkDir,cstring mapDir)
// Adds a VRML model, processing on the fly
// Model 'geoName' in dir 'trkDir'.
{
  RTV_Node node;
  char buf[1024];
  
//qdbg("AddModel(%s,%s)\n",geoName,trkDir);
//qdbg("AddModel mapDir='%s'\n",mapDir);

  // Prepare node
  node.type=0;
  node.model=new DGeode(0);
  
  // Read model
  node.model->SetMapPath(mapDir);
  sprintf(buf,"%s/%s",trkDir,geoName);
  QCV->Select();                  // Make sure textures end in QCV
  {
    QFile f(buf);
    node.model->ImportDOF(&f);
  }
  
  // Write model in the same place as the maps
  sprintf(buf,"%s/%s",mapDir,geoName);
  // No ugly caps
  strlwr(buf);
  // New extension
  buf[strlen(buf)-4]=0;
  // Store filename without extension
  node.fileName=QFileBase(buf);
  
  // Add model to track
  track->AddNode(&node);
}
void ImportDOFs()
// Import all .wrl files, add to the scene, and process
// accordingly
// Expects files in <trackdir>/SCGT
{
  char buf[1024],tdir[1024],mapDir[1024];
  QDirectory *dir;
  QFileInfo fi;
  // Progress
  int   total,current,pass;
  QRect r(100,100,400,150);
  QProgressDialog *dlg;
  
  if(!FilterRequest())return;
  
  strcpy(tdir,track->GetTrackDir());
qdbg("tdir='%s'\n",tdir);
  
  // Find all models in <tdir>/*.dof
  strcpy(mapDir,tdir);
  
  dlg=new QProgressDialog(QSHELL,&r,"Importing...",
    "Importing matching DOF files.");
  dlg->Create();
  current=total=0;
  for(pass=0;pass<2;pass++)
  {
    dir=new QDirectory(tdir);
    while(dir->ReadNext(buf,&fi))
    {
//qdbg("Perhaps '%s'\n",buf);
      // Filter
      if(strstr(buf,".dof")==0&&strstr(buf,".DOF")==0)
        continue;
      if(!FilterMatch(buf))continue;
      if(pass==0)
      { // Just count
        total++;
      } else
      { // Actually add the model
        dlg->SetProgressText(buf);
        dlg->SetProgress(current,total);
        AddModel(buf,tdir,mapDir);
        // Show progress
        PaintTrack();
        current++;
        if(!dlg->Poll())break;
      }
    }
    delete dir;
  }
  delete dlg;
}