[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.10.2 Using Mesh Objects

Written by Jorrit Tyberghein, jorrit.tyberghein@gmail.com.

This section explains how to use Mesh Objects in general. It doesn't go into detail about the specific Mesh Object Types that Crystal Space supports.

#### General

When the engine works with instances of Mesh Objects it does so through the ``iMeshWrapper'` interface. ``iMeshWrapper'` holds the reference to the ``iMeshObject'` instances implementing the particular type of Mesh Object. ``iMeshWrapper'` is responsible for deciding when to draw a Mesh Object and when to light it. ``iMeshWrapper'` also manages the position of the object in the world and manages possible hierarchical transformations (i.e. ``iMeshWrapper'` has an array of ``iMeshWrapper'` children).

#### Loading From Map File

The easiest way to use Mesh Objects is to simply specify them in the map file for your level. There are two things that you need to do for that. First you have to specify an entry at top-level (directly inside the ``world'` statement) like this (this example uses the fountain plug-in to make a fountain):

    <meshfact name="fountainFactory">
      <plugin>crystalspace.mesh.loader.factory.fountain</plugin>
      <params />
    </meshfact>

This will use the mesh object loader plug-in which is identified by the SCF name mentioned by the ``plugin'` statement to load a factory (type ``iMeshObjectFactory'`) and register it with the engine. There are no parameters for the fountain's Mesh Object Factory so that's why the ``params'` block is empty. But you still have to specify it. Other mesh object factories may accept parameters.

Because the fountain defines no parameters for the Mesh Object Factory you only have to create one factory in order to make all fountains you need. But in some cases you may have to create multiple factories depending on the object characteristics you want.

To place a fountain in some room (sector) you use a ``meshobj'` statement, within the definition of a room or sector, like this:

    <meshobj name="fountain">
      <plugin>crystalspace.mesh.loader.fountain</plugin>
      <params>
        <factory>fountainFactory</factory>
        <number>300</number>
        <material>spark</material>
        <origin x="0" y="0" z="0"/>
        <dropsize w=".05" h=".05"/>
        <color red="0.7" green="0.9" blue="1.0"/>
        <accel x="0" y="-.1" z="0"/>
        <falltime>3</falltime>
        <speed>1</speed>
        <elevation>1.5</elevation>
        <azimuth>0</azimuth>
        <opening>.2</opening>
        <mixmode>
          <add />
        </mixmode>
      </params>
      <move>
        <v x="-10" y="-1" z="14"/>
        <matrix>
          <rot x="1.5"/>
        </matrix>
      </move>
    </meshobj>

This code fragment uses the ``crystalspace.mesh.loader.fountain'` loader plug-in named to load a Mesh Object (type ``iMeshObject'`) and register it with the engine (while placing it in the current sector). In this case there are many parameters. The first parameter should always be the name of the factory. This is true for all Mesh Objects regardless of their type. For a complete list of parameters available, you should refer to the fountain documentation.

The ``move'` statement is outside of the ``params'` block. The reason for this is that the position of Mesh Objects is controlled by the engine and not by the Mesh Object itself.

In addition to the above statements you can also nest other ``meshobj'` statements to create a hierarchy of Mesh Objects (not shown in the example above). If you do that, then the ``move'` statement must be interpreted relative to the parent.

When you have Mesh Objects loaded into Crystal Space using the map file syntax illustrated above, then you can query the parameters from your application by using the standard API in the ``iEngine'` interface.

#### Using Directly From Code

You can also create Mesh Objects directly from your application. The engine has a few conveniance functions you can use for that (`iEngine::CreateMeshwrapper()` and `iEngine::CreateMeshFactory()`). But here we show you the full way to create meshes from code:

Here is a code example for creating an instance of the ball plug-in.

    void Initialize ()
    {
      ...
      // Get the ball mesh object plug-in.
      ball_type = csQueryPluginClass<iMeshObjectType> (this,
        "crystalspace.mesh.object.ball");
      if (!ball_type)
        ball_type = csLoadPlugin<iMeshObjectType> (this,
          "crystalspace.mesh.object.ball");
      if (!ball_type)
        Printf (CS_MSG_WARNING, "No ball type plug-in found!\n");
      ball_factory = ball_type->NewFactory ();
      ...
    }
    ...
    void SetupWorld ()
    {
      ...
      // Make a ball using the ball plug-in.
      csRef<iMeshObject> ballMesh = ball_factory->NewInstance ();
      csRef<iBallState> ballState =
        scfQueryInterface<iBallState> (ballMesh);
      ballState->SetRadius (.5, .5, .5);
      ballState->SetMaterialWrapper (material);
      ballState->SetRimVertices (12);

      csRef<iMeshWrapper> ball =
        engine->CreateMeshWrapper (ballMesh, "MyBall", room);
      m.Identity ();
      ball->GetMovable ().SetTransform (m);
      ball->GetMovable ().SetPosition (csVector3 (-3, 5, -3));
      ball->GetMovable ().UpdateMove ();
      ...
    }

This example will load a ball using the plug-in and place it at (-3,5,-3) in ``room'`. It will also make sure that it will be lit by the 10 closest lights.

``iBallState'` is the state interface for setting the characteristics of ball Mesh Objects.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ]

This document was generated using texi2html 1.76.