[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

4.16.4 Loading 3DS and MD2 Models

Crystal Space can read 3DS and MD2 models natively.

Loading Models From World File

From a world file you can do it as follows:

 
<meshfact name="my3ds">
  <plugin>crystalspace.mesh.loader.factory.genmesh.3ds</plugin>
  <paramsfile>/data/my3ds.3ds</paramsfile>
</meshfact>
<meshfact name="mymd2">
  <plugin>crystalspace.mesh.loader.factory.sprite.3d.md2</plugin>
  <paramsfile>/data/mymd2.md2</paramsfile>
</meshfact>
...
<sector name="room">
  <meshobj name="my3dsMesh">
    <plugin>crystalspace.mesh.loader.genmesh</plugin>
    <params> <factory>my3ds</factory> </params>
    <move> <v x="0" y="0.3" z="15" /> </move>
  </meshobj>
  <meshobj name="mymd2Mesh">
    <plugin>crystalspace.mesh.loader.sprite.3d</plugin>
    <params>
      <factory>mymd2</factory>
      <action>wave</action>
      <material>mymd2_material</material>
    </params>
    <move> <v x="-2" y="0.3" z="15" /> </move>
  </meshobj>
</sector>

The 3DS loader will create genmesh factories. It supports multiple materials (will be converted to genmesh submeshes). The MD2 loader will create 3D sprite factories with animation (actions and frames). Note that in both cases you must load the materials used in the models seperatelly (in the textures/materials section of the world file). Note that in case of the MD2 loader there will be no material associated with the factory so you need to set a material (and usually an action too) in the mesh object.

Loading Models From Code

Both model loading plugins above support the iModelLoader interface. Using that interface it is very easy to load a 3DS or MD2 model from code. Here is an example using the 3DS model loader:

 
#include "imap/modelload.h"

void LoadModel ()
{
  csRef<iPluginManager> plugin_mgr = csQueryRegistry<iPluginManager> (object_reg);
  csRef<iModelLoader> loader = csLoadPlugin<iModelLoader> (plugin_mgr,
			"crystalspace.mesh.loader.factory.genmesh.3ds");
  iMeshFactoryWrapper* ff = loader->Load ("model", "/lib/model.3ds");
  if (!ff)
  {
    ...
  }
}

Or you can use the iLoader->Load() method which will automatically recognize the model and can also load Crystal Space XML files:

 
#include "imap/loader.h"

void LoadModel ()
{
  csRef<iLoader> loader = csQueryRegistry<iLoader> (object_reg);
  iBase* result;
  if (!loader->Load ("/lib/model.3ds", result))
  {
    // Error!
    ...
  }
  csRef<iMeshFactoryWrapper> ff = scfQueryInterface<iMeshFactoryWrapper> (result);
  if (ff)
  {
    ...
  }
}

This document was generated using texi2html 1.76.