Accessing material names from inside a mesh file

dougkeenan

Addon Developer
Addon Developer
Donator
Joined
Nov 18, 2007
Messages
617
Reaction score
0
Points
16
Location
Indianapolis
Website
www.orbithangar.com
The title says it all ... is there a way inside Orbiter to access the material names from inside the .msh file? (I could parse it in Windows but that would be cheating.)
 

Bloodest

Beta Tester
Beta Tester
Joined
May 6, 2008
Messages
6
Reaction score
0
Points
0
Location
Peterburg
I think that in memory Orbiter no names are present - only pointers.

But in structures MESHGROUP there is a link between a grid and a material index - MESHGROUP:: MtrlIdx. On this index it is possible to extract properties of a material - function oapiMeshMaterial ().

The material Name can be received having read (out of API) corresponding .msh file and having made the list of names of materials - IHMO indexes will coincide.
 

dougkeenan

Addon Developer
Addon Developer
Donator
Joined
Nov 18, 2007
Messages
617
Reaction score
0
Points
16
Location
Indianapolis
Website
www.orbithangar.com
The material Name can be received having read (out of API) corresponding .msh file and having made the list of names of materials - IHMO indexes will coincide.
"read (out of API)" - yes thank you, that's what I was afraid of. Looks like a MFD will have to fopen the msh file and parse for the names manually. The names can then be connected by index number to oapiMeshMaterial as you mention. It doesn't look like Orbiter changes the order around so that should be easy but c'mon, how hard would that have been to add the name value to the material structure? :blink:

Anyway new problem, it looks like Orbiter doesn't allow multiple vShip->AddMesh calls of the same msh file without a CTD either. And although we can load the mesh with a translation ("beep!") offset, no similar API function is available to rotate the mesh on loading. I got spoiled by the Irrlicht engine, grumble grumble grumble ...
 

dougkeenan

Addon Developer
Addon Developer
Donator
Joined
Nov 18, 2007
Messages
617
Reaction score
0
Points
16
Location
Indianapolis
Website
www.orbithangar.com
Ended up doing it myself, here's some code posted for giggles. This snippet keeps a list of material names for later comparison so only new materials get added.
Code:
{
 char string[128] ; // mesh name + path
 FILE *file ;
 // add block mesh
 data->meshhandles[data->showblocks] = oapiLoadMeshGlobal( cc_blocks[data->showblocks]->meshname )  ;
 if( data->meshhandles[data->showblocks] )
 {
  // mesh fetched successfully
  data->meshindex[data->showblocks] = vnShip->AddMesh( data->meshhandles[data->showblocks] ) ;
  // sort submeshes
  data->meshgroupcount[data->showblocks] = oapiMeshGroupCount( data->meshhandles[data->showblocks] ) ;
  for( i=0; i<data->meshgroupcount[data->showblocks]; i++ )
  {
   data->meshgroups[data->showblocks][i] = oapiMeshGroup( data->meshhandles[data->showblocks], i ) ;
  }
  // sort materials
  data->matcount[data->showblocks] = oapiMeshMaterialCount( data->meshhandles[data->showblocks] ) ;
  for( j=0; j<data->matcount[data->showblocks]; j++ )
  {
   data->materials[data->showblocks][j] = oapiMeshMaterial( data->meshhandles[data->showblocks], j ) ;
  }
  sprintf( string, "%s%s.msh", MESHDIR, cc_blocks[data->showblocks]->meshname ) ;
  file = fopen( string, "rt" ) ;
  if( file )
  {
   int nummats ;
   bool matFound ;
   char matString[MAX_MATNAMELEN] ;
   // parsing mesh for materials
   while( fgets( string, 128, file ) )
   {
    if( !strncmp( string, "MATERIALS ", 10 ) )
    {
     if( sscanf( string+10, "%d", &nummats ) )
     {
      if( nummats == data->matcount[data->showblocks] )
      {
       // #nummats materials found
       while( fgets( string, 128, file ) )
       {
        if( !strncmp( string, "MATERIAL ", 9 ) )
        {
         if( sscanf( string+9, "%s", matString ) )
         {
          // matStrings are material names
          matFound = false ;
          for( j=0; j<data->materialtotal; j++ )
          {
           if( !strcmp( matString, data->materialnames[j] ) ) 
           {
            matFound = true ;
            break ;
           }
          }
          if( matFound == false )
          {
           strcpy( data->materialnames[data->materialtotal], matString ) ;
           data->materialtotal++ ;
          }
         }
        }
       }
      }
     }
    }
   }
   fclose( file ) ;
  }
 }
 else
 {
  Panic( "Can't open meshfile for materials!" ) ;
 }
}
 
Top