Classes for making mesh nodes

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Classes for making mesh nodes

Post by Asimov »

Hi all,

I have done a lot of C++ and 2D stuff in the past, but just trying to get to grips with this engine. My C++ is rusty as I haven't done any C++ projects for a while too.

I notice a lot of tutorials keep everything in the main.cpp which I understand is to keep things simple.
In 2D if i was to make a sprite I wouldn't just add it all before main. I would make a sprite class, using a header file and various methods or functions for all the things that sprite can do.
This way I can have instances of the sprite class.

I presume I would be able to do this with a 3d mesh. eg make a class and send the model to that class, rather than having countless nodes.

eg in 2D to initialize my sprite I may do
crosshair.LoadSprite(L"data//crosshair",50,50,11,550 );

which calls my LoadSprite class and initializes the sprite.

I am pretty sure I can do this to call game entities, but not entirely sure the best way to go around this.

I presume I could set up the following code in the header file of the class

Code: Select all

IMesh* invader = smgr->getMesh("invadertest.obj");
IMeshSceneNode*node = smgr->addMeshSceneNode(invader);
 
if (node)
{
 
    node->setMaterialTexture(0, driver->getTexture("invader.tga"));
    node->setMaterialFlag(EMF_LIGHTING, false);
}
and then put my functions in the cpp of the same class.

I like to split my code up like this because it is easier for debugging, but I am new to the 3D side of it, but I wouldn't actually put IMesh* invader = smgr->getMesh("invadertest.obj"); as the obj would be passed to the function from the main.cpp if you know what I mean

Any thoughts are welcome

Asimov
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Classes for making mesh nodes

Post by mongoose7 »

There aren't a lot of things you can do with a mesh, and Irrlicht already provides all the methods. That is, the class you talk about is already implemented in Irrlicht. Of course, you may wrap it if you want, but there is no need.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi mongoose,
There aren't a lot of things you can do with a mesh, and Irrlicht already provides all the methods. That is, the class you talk about is already implemented in Irrlicht. Of course, you may wrap it if you want, but there is no need.
For instance if I wanted functions for moving my character left, or right. Or the Ai for the mesh. I normally put these in a class in a different header and cpp file, rather than clogging up the main.cpp file.

I tried to make a mesh class earlier but failed LOL, as I am not sure how to set up the IMESH without loading the model, in the header file
IMesh* something;

I am sure there is a way to do this, but I have only been using the engine since yesterday heh heh.

Asimov
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi all,

The other reason I want to make a class is this.
Instead of having all this

Code: Select all

IMesh* invader = smgr->getMesh("invader.md2");
IMeshSceneNode*node = smgr->addMeshSceneNode(invader);
 
if (node)
{
 
    node->setMaterialTexture(0, driver->getTexture("invader.tga"));
    node->setMaterialFlag(EMF_LIGHTING, false);
}
in my main.cpp I could replace it with something likethis

Code: Select all

 
invader.Load("invader.md2","invader.tga");
 
Which is much shorter and easier to read. If I can add normalmaps and stuff later I can put them in the same line and the class would take care of it.
So it simplifies the look of the code, and I never have to look in that class.

Although I am not sure how to go about making a mesh class yet.

Asimov
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Classes for making mesh nodes

Post by Seven »

I think that you are debating the 'is a' and a 'has a' relationship.

if you create a wrapper class for a game object and it is derived from, let's say ISceneNode, then your object 'is a' scenenode. If, on the other hand, you create a game object class and it has a scenenode in it (or 2 or 10) then your object 'has a' scenenode.

to me, gameobject have a scenenode, not are a scenenode.

I use a 'has a' relationship for all gameobejcts

Code: Select all

 
 
example :
  class FireSceneNode : public ISceneNode
       in this class you override all of the scenenode functions to make a cool fire effect
       this is an actual scenenode that can be used by lots of gameobjects
 
  class GameObjectFireBall : public GameObject
       FireSceneNode* Fireball;
       ILightSceneNode* Light;
       ISoundEffect* Sound;
 
       void moveForward() { some movement code }
       void onCollision(GameObject* whoDidIHit) { get bigger, make light more red, play explosion sound }    
 
       in this class, you have a instance of the FireSceneNode (that can scale for example)
       but you also have an instance of a lightnode (it grows with the fire)
       and you have code to move the object, which in turn moves the fire and light
       but also plays a sound when a collision is detected.
 
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi Seven,

That looks interesting. Have you got a more complete code example of how you call this class, and load your models using this class?
I am dying to make a game, but I am making baby steps at the moment.

I also know I can use the .irr format and load in a complete scene, but I also need to be able to load in individual models too. For this game I have in mind.

Asimov
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Classes for making mesh nodes

Post by CuteAlien »

The way I do that (at least in one project) is to have 3 functions. LoadMesh, LoadModel and LoadAnimatedModel. LoadMesh is called by LoadModel and LoadAnimatedModel. And I have 2 model loading functions because one returns ISceneNode* and the other IAnimatedMeshSceneNode* and they do somewhat different optimizations (static models can have more optimizations).

I have also some functions calling those based on some parameters from file (but a custom format, so not of use to you). And characters have their own class (you might call it Character or Player or Moveable, etc...). And those classes have pointers to ISceneNode* or IAnimatedMeshSceneNode* (you tend to know in those classes which one you need). Or there is also class called Level which has a ISceneNode* for the current level.

I can post my Load functions to show the idea, but they use a lot of stuff that is specific to my game. So be warned, there's strange comments, custom functions, lot's of logging-output, profiling data, etc: http://ideone.com/o62ldY
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi CuteAlien,

Thanks for the code. I will take some time looking through it. I am sure I will have some questions after examining it now.
I have had a quick look and already I have a couple of questions

What is the difference between LoadMesh and LoadModel? The descriptions seem to do the same thing.

I have had a quick look at your functions.

I notice in your code you start the first function with this

Code: Select all

scene::IAnimatedMesh* IrrlichtManager::LoadMesh(const Config& config, const c8* filename, bool animated)
I understand the

Code: Select all

LoadMesh(const Config& config, const c8* filename, bool animated)
part, but

Normally for instance say my class was called Strawberries, my function would look like this.

Code: Select all

 
Strawberries::EatStrawberries
{
}
 
So I am wondering where you get this bit of your code

Code: Select all

scene::IAnimatedMesh* IrrlichtManager::
are your functions in main.cpp or do you make a class to put your functions in.

I am a little rusty with my C++ so I am playing catchup.
Thanks for the help.

Asimov
sash
Competition winner
Posts: 35
Joined: Thu Nov 05, 2009 8:46 am
Location: Novosibirsk

Re: Classes for making mesh nodes

Post by sash »

Azimov,

Code: Select all

scene::IAnimatedMesh* IrrlichtManager::LoadMesh
This is a method LoadMesh of class IrrlichtManager returning pointer to IAnimatedMesh (located in namespace "scene").
Wow... never thought it could take such a number of words :)
Last edited by sash on Mon Dec 08, 2014 8:24 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Classes for making mesh nodes

Post by CuteAlien »

LoadModel is maybe a bad name. Maybe better name would be something like LoadNode as it's about creating nodes. The difference is that a mesh can be displayed many times - each with it's own node (they share the same geometry). Or in other cases it might load the same mesh from file, but has to keep it independent in memory (creating a new mesh copy and not using the one from cache) for each node. That's for example often necessary for animations to prevent that all models with the same mesh play the exact same animation. Thought I think I don't do that in this project (but in another one).

IrrlichtManager is the name of the class I used to have all kind of project specific tool-functions around Irrlicht. I use another one (ExtIrr) for project independent tool code for Irrlicht. There are corresponding header files for those classes (for example this is the header of my IrrlichtManager class: http://ideone.com/atkhEZ).

@sash: (already fixed)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sash
Competition winner
Posts: 35
Joined: Thu Nov 05, 2009 8:46 am
Location: Novosibirsk

Re: Classes for making mesh nodes

Post by sash »

CuteAlien, sure, I just got 500 http error from sourceforge when composed message.
Fixed initial message.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Classes for making mesh nodes

Post by CuteAlien »

Ah yeah - I also got a few times server errors today. Annoying.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi Sash,
This is a method LoadMesh of class IrrlichtManager returning pointer to IAnimatedMesh (located in namespace "scene").
Wow... never thought it could take such a number of words
Thanks. I guessed it was the class, but I have never seen a class name returning a pointer before. It is something I have never used in previous projects and never came across before, which have all been 2D of course.

Hi CuteAlien,
I am still looking through your code. Lots of interesting stuff that I have been googling. Will probably be a few days before I can ask any questions on this. I don't like getting beaten which is why I try everything in the book before asking a question. I have been modelling in 3ds max fore 8 years now, and it is about time I made my own 3D game. I have been programming since 1981, but keep switching between languages so I forget the nuances of each.

By the way I notice Sash is from Novosibirsk And you are from Tübingen, Germany and yet you both write better English than somebody from England.

Asimov
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Classes for making mesh nodes

Post by Asimov »

Hi CuteAlien,

Thanks for the code, but I was wondering if I could see your main.cpp so I can see how it all goes together.

I have to admit I am struggling understanding it all. Probably a little too much to take in, all in one go.

Obviously I have had experience with classes before, but I am having trouble understanding how the return pointers work.
In my missile command game I was working on, my classes were much simpler eg my header file for my enemy missile class is below

Code: Select all

 
#pragma once
#include "D3DGraphics.h"
#include "Mouse.h"
#include "Base.h"
#include "LevelInfo.h"
#include "Score.h"
 
 
 
// *******************************
#define _USE_MATH_DEFINES   // ***
#include "math.h"           // ***
// *******************************
 
 
class EMissile
{
public:
    EMissile();
    int EMissileX();
    int EMissileY();
    void UpdateMissile(D3DGraphics* gfx, Base base[], Score* score, Sound* thud, int Volume);
    void RandomMissile();
    bool Fired;
    bool CheckCityCrash(Base base[], Score* score);
    bool TurretCrash();
    int Exists(LevelInfo* levelrun);
    void ResetWave();
    bool Shield();
    
 
private:
    double x;
    double y;
    double TargetX;
    double TargetY;
    double Velocity;
    double locationX;
    double locationY;
 
    int waveCount;
};
I realise that you use these return pointers because you have to return things to the engine somehow, but I am having trouble understanding them,

Code: Select all

irr::IrrlichtDevice* GetIrrlichtDevice() const      { return mIrrlichtDevice; }
    irr::video::IVideoDriver*  GetVideoDriver() const   { return mVideoDriver; }
    irr::scene::ISceneManager* GetSceneManager() const  { return mSceneManager; }
    irr::IEventReceiver* GetEventReceiver() const       { return mEventReceiver; }
    irr::ITimer* GetIrrlichtTimer();
Also I have a question. Is there a reason why you don't use a header file for main.cpp in any of the examples?
And would it be wrong if I used a header file for it?

All we need to do is to get 3D buzz to make a lot of irrlicht engine tutorials.
I have tons of Unity ones but I really want to get to grips with irrlicht.

It would be good if some of the tutorials shipped with irrlicht had examples of using classes.

The biggest reason I like to use classes is that you quickly get lost if you stick all your code in main.cpp.
I usually create a class eg a sprite class which has everything I need for collision detection, loading the sprite and movement.

I started by taking apart the Loadmesh class, and trying to add the bare minimum to get it to work, and I quickly run
into compiler errors from inexperience.

I am going to keep working at this, but having a hard time at the moment.
Post Reply