Tutorial 09 IND 3dMesh

From IndieLib
Jump to: navigation, search


Starting with 3D meshes.

Before you read this tutorial I suggest you to make a small program with 2d entities on IndieLib, in order to get the basic concepts.

For this tutorial course you need 3d models exported to .x file with textures. You can use example models stored in "resources" folder in the IndieLib SDK. You can also get .x files from exporting it from 3ds max or another modelling program.


In This tutorial you’ll learn how to insert and manage 3d objects. Your game will be now 3D, and it’s quite easy. Check it yourself.

3D Meshes

3D meshes are used to initialize your models to engine. You can add them to 3d entities.

3D Entity

Used for drawing graphical objects into the screen and manipulating their attributes, like "SetAnimationSpeed”, "Set Position”, etc. See more in the Api Reference.

3D Entity Manager

Manager used for Adding/Removing 3D Entities.

Starting with Code

OK, let’s start with that what is most important – code. Open file "Main.cpp" stored in "a_09_IND_3dMesh" folder. First important code, with 3D Mesh Loading, we will load quite nice skeleton:

// Bones loading
IND_3dMesh mMeshBones;
if (!mI->MeshManager->Add (&mMeshBones, "..\\resources\\skeleton\\skeleton.x", "..\\resources\\skeleton")) return 0;

- Creating 3dMesh - Loading skeleton.x file (3d Model) and his textures.

Now we have to add mesh into entities:

// Creating 3d entity
IND_Entity3d mBones;               
mI->Entity3dManager->Add (&mBones);         // Entity adding
mBones.Set3dMesh (&mMeshBones);             // Set the 3d mesh into the entity

When we add mesh to entity, it must be placed somewhere. We do it like with 2d entities.

// ----- Changing the attributes of the 3d entities -----
   mBones.SetPosition (0, 0, 0);            //Set Position
   mBones.SetScale (1.5f, 1.5f, 1.5f);      //Scaling model

Camera settings – setting 2d camera for gui objects, and 3d camera for our skeleton model (2d/3d camera will be analyzed in next tutorials):

IND_Camera2d mCamera2d (mI->Window->GetWidth () / 2, mI->Window->GetHeight() / 2);
IND_Camera3d mCamera3d (0.0f, 1.4f, -4.0f);
mCamera3d.SetAspect ((float) mI->Window->GetWidth () / mI->Window->GetHeight());

Light - without this our model can’t be seen. Setting direction light, position etc. This will be analyzed in next tutorial:

IND_Light mLight;
mI->LightManager->Add (&mLight, IND_DIRECTIONAL_LIGHT);
mLight.SetPosition (0, 0, 0);
mLight.SetColor (1.0f, 1.0f, 1.0f, 1.0f);
mLight.SetDirection (0.0f, -0.3f, 0.5f);
mLight.SetRange (1000.0f);

Important Variables:

float   mAngle = 0;                   // Skeleton angle
int   mSequence = 0;                  // Sequence of animations
char   mText [2048]; mText [0] = 0;   // Text variables
int   mSpeed = 25;                    // Rotating speed
float   mDelta;                       //Delta time

Animation Changing:

// Change animation sequence Up
      if (mI->Input->OnKeyPress (IND_KEYUP))
         mSequence++;                                                     // Next animation
         if (mSequence == mMeshBones.GetNumSequences()) mSequence = 0;    // Looping sequences
         mBones.SetSequence (mSequence);                                  //Setting animation
      // Change animation sequence Down
      if (mI->Input->OnKeyPress (IND_KEYDOWN))
         mSequence--;                                                      // Animation sequence -1
         if (mSequence < 0) mSequence = mMeshBones.GetNumSequences() - 1;  // Looping sequences
         mBones.SetSequence (mSequence);                                   // Setting animation

SetSequence mean which one sequence you’ll now play, when exporting from .ex 3ds max you set sequences animation of your model. Turning around own pivot:

mAngle += mDelta * mSpeed;
Updating angle of entities:
<source lang="Cpp">
// Back
mBack.SetAngleXYZ (0, 0, mAngle);   
// Skeleton
mBones.SetAngleXYZ (0, mAngle, 0);

Rendering functions:

mI->Render->BeginScene ();
mI->Render->SetViewPort2d (0, 0, 800, 600);      //Setting VievPort 2d
mI->Render->SetCamera2d (&mCamera2d);            //Setting Camera 2d
mI->Entity2dManager->RenderEntities2d ();        //Rendering back and fonts
mI->Render->SetViewPort3d (0, 0, 800, 600);      //Setting VievPort 3d
mI->Render->SetCamera3d (&mCamera3d);            //Setting Camera 3d
mI->LightManager->Update ();                     //Updating light
mI->Entity3dManager->RenderEntities3d ();        //Rendering our skeleton

I hope that this tutorial wasn’t too hard, and after reading it you’ll be able to make your own program with own super models of monsters ; ). Remember to check the Api Reference.

Now you can check light tutorial.

By Armageddon ;)

Template:Category Tutorials

Personal tools