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

5.3.3 Creating a Mesh from a Factory

First we need to add an additional include to the include section:

 
#include "imesh/sprite3d.h"

To create a mesh just add the following code in Simple::Initialize (before return true and after the code we just added to load the mesh factory):

 
bool Simple::Initialize (int argc, const char* const argv[])
{
  ...

  // Add the sprite to the engine.
  iMeshWrapper* sprite = engine->CreateMeshWrapper (
  	imeshfact, "MySprite", room,
	csVector3 (-3, 5, 3));
  csMatrix3 m; m.Identity (); m *= 5.;
  sprite->GetMovable ()->SetTransform (m);
  sprite->GetMovable ()->UpdateMove ();
  iSprite3DState* spstate =
    SCF_QUERY_INTERFACE (sprite->GetMeshObject (), iSprite3DState);
  spstate->SetAction ("default");
  imeshfact->DecRef ();
  spstate->DecRef ();
  sprite->DeferUpdateLighting (CS_NLIGHT_STATIC|CS_NLIGHT_DYNAMIC, 10);
  sprite->DecRef ();

  return true;
}

The easiest way to create a mesh is to use engine->CreateMeshWrapper(). This will take care of using the given mesh factory to create the mesh, give it a name, and correctly place it at the given position in the world. The name of a mesh can be useful for scripting so that a script can refer to objects with their names.

Moving meshes (and things) is done through the iMovable interface to which you can get a reference by doing sprite->GetMovable(). The calls to SetTransform() and SetPosition() set up a transformation matrix and vector to correctly place the mesh in the room. In this particular case we use the identity matrix as a transform and scale it with five to make the sprite five times bigger. After doing movement (either updating the sectors or the position) you must call movable->UpdateMove() to change internal data structures (i.e. the engine may use this to recalculate visibility information).

If you read the mesh object documentation (see see section 7.8 Mesh Object Plug-In System) carefully you will see that in order to control visual attributes of mesh objects you will have to get the state interface for either the object or the factory. In this example we query iSprite3DState from the mesh in order to set the default action. An action is a set of frames and is used to control animation. In our simple example the sprite has only one frame so there is not much animation to see.

Whenever you are done with some interface you obtained through SCF_QUERY_INTERFACE() you should release the pointer with DecRef().

The last call to DeferUpdateLighting is interesting. If you don't call this then the sprite will be black. This call tells the engine to update lighting on this sprite as soon as it is being rendered. You need to call this function again when lights move or when the object itself moves. Note that this function is not very expensive. It will only recalculate lighting when the object is really visible.

This concludes the second Simple tutorial. You can now go on to the next tutorial (see section 5.4 Simple Tutorial 3: Map Loading)) to learn how you can load a level from a file instead of creating your rooms in the code.


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

This document was generated using texi2html