4.2.2.3 ファクトリからのメッシュの作成


4.2.2.3 ファクトリからのメッシュの作成

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

メッシュを作成するために、Simple::CreateSprites?()に次のコードを追加します。追加する場所は、`return true`の前で、かつ、メッシュファクトリをロードするために追加したコードの後です。

void Simple::CreateSprites ()
{
 ...

 // Add the sprite to the engine.
 csRef<iMeshWrapper> sprite (engine->CreateMeshWrapper (
       imeshfact, "MySprite", room,
       csVector3 (-3, 5, 3)));
 csMatrix3 m; m.Identity ();
 m *= 5.; // Don't do this scale in your own code! (see discussion)
 sprite->GetMovable ()->SetTransform (m);
 sprite->GetMovable ()->UpdateMove ();
 csRef<iSprite3DState> spstate (
   scfQueryInterface<iSprite3DState> (sprite->GetMeshObject ()));
 spstate->SetAction ("default");
 sprite->SetZBufMode (CS_ZBUF_USE);
 sprite->SetRenderPriority (engine->GetObjectRenderPriority ());
}

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.

メッシュを作成する最も簡単な方法は、engine->CreateMeshWrapper?()を使用することです。これは、与えられたメッシュファクトリを、以下のことをするために使用します。

  • メッシュを作成します
  • 作成したメッシュに名前を付けます
  • 正確に、ワールドで指定した位置に、メッシュを配置します

メッシュの名前はscriptingのために役に立ちます。そのため、スクリプトは名前をもったオブジェクトを参照することができます。

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).

メッシュは、iMovableインターフェイスを通して動かすことができます。sprite->GetMovable?()を使用することにより、iMovableインターフェイスの参照を取得することができます。SetTransform?()とSetPosition?()を呼ぶことで、ルーム内にメッシュを正確に配置するための変換マトリクスとベクトルをセットアップします。この特別なケースでは、5倍の大きさのスプライトを作成するために、変換とスケールとして、5の単位行列を使用します。移動した後は(セクターまたはポジションのアップデート)、内部データ構造を変更するために movable->UpdateMove?()を呼ばなければなりません。(すなわち、エンジンは視覚情報を再計算するために、これを使用します。)

WARNING: The code above actually scaled the object by giving it a transformation matrix which is multiplied with 5. Don't do this in your own code. Many subsystems in Crystal Space (like the collision detection system, the visibility culling system and the lighting system) don't work properly with scaled objects. Instead scale your object using HardTransform?().

警告: 上記のコードは、5を掛け算した変換マトリクスを与えることによって、オブジェクトのスケールを変更しました。あなたのコードでは、これはしないで下さい。多くのCrystal Space内のサブシステムは(衝突探知システム、視認抜粋システム、ライトシステムのような)、スケールを変更したオブジェクトを使用すると、たぶん動きません。代わりにHardTransform?()を使用することでオブジェクトをスケーリングしてください。

If you read the mesh object documentation (see section 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.

もし、メッシュオブジェクトドキュメント(see section Mesh Object Plug-In System) を注意深く読むと、メッシュオブジェクトの視覚属性をコントロールするために、オブジェクトまたはファクトリのための状態インターフェイスを取得する必要があることがわかる。この例では、デフォルトの振る舞いを設定するために、メッシュから、`iSprite3DState`を実行します。振る舞いは、フレームの1セットで、アニメーションを制御するために使用されます。私たちの簡単な例では、スプライトは1つのフレームのみ持っています。そのため、アニメーションを見ることはできません。

The calls to SetZBufMode?() and SetRenderPriority?() are not strictly required since the values set here are default for meshes anyway. But the two lines serve as an example on how to set other values for these. SetZBufMode?() sets the Z-buffer mode that the engine will use to render this object. With `CS_ZBUF_USE' this means that the Z-buffer will be used fully. Other values are `CS_ZBUF_FILL' (only fill the Z-buffer), `CS_ZBUF_TEST' (only test the Z-buffer), or `CS_ZBUF_NONE' (ignore the Z-buffer). `CS_ZBUF_USE' is a good default for objects in a room. `CS_ZBUF_FILL' is usually used for the outside walls of a sector.

ここで設定される値は、とにかくメッシュのためのデフォルトであるため、SetZBufMode?()とSetRenderPriority?()を呼ぶことは、厳重には要求されません。しかし、2ラインは、これらを他の値に設定する方法の例として重要な役目を果たします。SetZBufMode?()は、Z-bufferモードを設定します。Z-bufferは、エンジンが、このオブジェクトをレンダするために使用します。`CS_ZBUF_USE`を設定すると、Z-bufferが全体に使用されます。他の値は`CS_ZBUF_FILL`(塗りつぶしのみ、Z-buffer)、`CS_ZBUF_TEST`(テストのみ、Z-buffer)または、`CS_ZBUF_NONE`(Z-bufferを無視)です。`CS_ZBUF_USE`は、ルーム内のオブジェクトのための良いデフォルト値です。`CS_ZBUF_FILL`は、よくセクタの外壁として使用されます。

SetRenderPriority?() controls when this object is rendered in this sector. By default there are several render priorities. Some of the more important ones are: sky, wall, object, and alpha. Every object can be put in a render priority queue. Objects in lower render priorities will be rendered before objects in higher render priorities. In addition some render priority queues sort objects internally based on some other criteria. For example, the `alpha' render priority will render all objects in that queue from back to front. You can create your own additional render queues if you want. The default render priority is object which you can get with the call engine->GetObjectRenderPriority?(). Most objects with Z-buffer mode equal to `CS_ZBUF_USE' are placed in the `object' render priority queue.

SetRenderPriority?()は、オブジェクトがセクタ内でレンダされるときに制御します。初期設定で、いくつかのレンダ優先度があります。いくつかのより重要なものは次のとおりです。空、壁、オブジェクト、それとアルファです。すべてのオブジェクトは、レンダ優先度キューに置くことができます。レンダ優先度の低いオブジェクトは、高いオブジェクトより前にレンダされます。加えて、いくつかのレンダ優先度キューは、いくつかの他の基準を基礎として、内部でオブジェクトをソートします。例えば、`alpha`レンダ優先度は、キューの中のすべてのオブジェクトを後ろから前に向かってレンダします。もし必要なら、あなたはあなた自身のレンダキューを作成することができます。デフォルトレンダ優先度キューは、engine->GetObjectRenderPriority?()を呼ぶことで取得することのできるオブジェクトです。Z-bufferモードを使用する(つまり`CS_ZBUF_USE`)大抵のオブジェクトは、`object`レンダ優先度キューに配置されます。

For more information on render priorities and Z-buffer mode you can look at the HOWTO on this subject (see section Render Priorities and Objects in Sectors).

レンダ優先度とZ-bufferモードに関してのより多くの情報は、この章の操作手順をご覧下さい(see section Render Priorities and Objects in Sectors)。

This concludes the second Simple tutorial. You can now go on to the next tutorial (see section Simple Tutorial 3: Map Loading, Collision Detection)) to learn how you can load a map from a file instead of creating your rooms programmatically.

これで、第2のシンプルチュートリアルは終わりです。あなたは、次のチュートリアルに進むことができます。(see section Simple Tutorial 3: Map Loading, Collision Detection)) 次のチュートリアルでは、プログラムでルームを作成する代わりに、ファイルからマップをロードする方法を学びます。