Page 1 of 1

Back from the depths

Posted: Tue Mar 01, 2011 11:59 pm
by LookingForAPath
Hi, its been awhile since I last posted and I've been wanting to ask something for awhile now. I'm sorry if this is insanely retarded of me to ask, but I really have no clue atm.

Anyway, lets say I have a class and for each individual of the class array,
I want to draw a model.

Class Example:

Code: Select all


class ModelToDraw
{
int X;
int Y;
int Z;

int Roll;
int Yaw;
int Pitch;

string Model;
string Texture;

public:
};
Now, I know irrlicht uses pointers to its model drawing and what not

Copied from Irrlicht Tutorial 1: Hello World. All credit goes to its creator.

Code: Select all

        IAnimatedMesh* mesh(Pointer Here) = smgr->getMesh("../../medi/sydney.md2");
        if (!mesh)
        {
                device->drop();
                return 1;
        }
        IAnimatedMeshSceneNode* node(Pointer Here) = smgr->addAnimatedMeshSceneNode( mesh );

Now heres my problem, in this code,

Code: Select all

ModelToDraw[10];

for(int i = 0; i < 10; i++)
{
}
would I still use the same names * Mesh(or whatever I name it) and * Node(Or whatever I name it) for each one, or would that over write the previous ones?

Posted: Wed Mar 02, 2011 12:48 am
by polylux
Seemingly you have some difficulties with the principle of pointers and how to use them.
Unfortunately, I don't really get your problem. You state in your last code example an empty for-loop. Maybe it'd be helpful if you could at least state what you try to put in there (even in pseudo-code).

Re: Back from the depths

Posted: Wed Mar 02, 2011 12:52 am
by greenya
LookingForAPath wrote:Now heres my problem, in this code,

Code: Select all

ModelToDraw[10];

for(int i = 0; i < 10; i++)
{
}
would I still use the same names * Mesh(or whatever I name it) and * Node(Or whatever I name it) for each one, or would that over write the previous ones?
When you do:

Code: Select all

IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
Irrlicht' scene manager allocates all necessary memory and keep the pointer to that memory in its nodes tree. Also returns you a pointer to newly added node. You can assign new value to "node", a new pointer, old node will not get lost, it will work just fine, since scene manager keeps a pointer. So if you do not need to directly set up newly created node (tweak materials, position etc.), you can forget (reuse) this pointer.

Ordinary, if you need to access your node all the time (for example you update some data of the node), then its much easier and better just to keep the pointer on it in some variable.

Also example with comments:

Code: Select all

mesh = ...

// we create animated mesh scene node
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);

// we create a new copy, it is independent from previous; "node" now points to this new node,
// so if we do "node->setPosition()" after, we affect only this new node
node = smgr->addAnimatedMeshSceneNode(mesh);

mesh = ...

// one more node; "node" is not reused (and keeps contain pointer to previous node)
// this new node now has new "mesh"; previous two nodes will not be changed (they will have own old mesh)
smgr->addAnimatedMeshSceneNode(mesh);
Hope this helps :roll:

Posted: Wed Mar 02, 2011 12:58 am
by pippy3
This probably should go in Advanced/ Beginners help.

Don't make your own class to draw a model. Reuse Irrlicht's behaviors. Also put everything into an irrlicht array, don't use C style arrays (unless you really know what you're doing).

The Irrlicht API should be your bible. if you look at ISceneNode, there are many different scene nodes to model your functionally from.

I've written a class, and an example loop, that contains multiple animated meshes. I think that's something you wanted:

Code: Select all

/*
* mySceneNode.h
* creates a custom scene node
*/

#if defined(__APPLE__) || defined(MACOSX)
#include <Irrlicht/irrlicht.h>
#else
#include "irrlicht.h"
#endif

using namespace irr;

#ifndef _MY_SCENE_NODE_H_
#define _MY_SCENE_NODE_H_

class MySceneNode : public scene::ISceneNode {
//private:
protected:
	// list of different meshes
	core::array<scene::IAnimatedMeshSceneNode*> meshs;
	// collision detection, visibility etc.
	core::aabbox3d<irr::f32> boundingBox;
public:
	MySceneNode(scene::ISceneNode* parent, scene::ISceneManager* smgr, s32 id) :
	scene::ISceneNode(parent, smgr, id)
	{
		// add first mesh
		scene::IAnimatedMesh * meshData = smgr->getMesh("mesh1.b3d");
		// parent new mesh to this one
		scene::IAnimatedMeshSceneNode* mesh1 = smgr->addAnimatedMeshSceneNode(meshData, this);
		mesh1->setIsDebugObject(true);
		boundingBox.addInternalBox (mesh1->getBoundingBox());
		// add other meshes here
	};
	
	// deconstructor
	~MySceneNode() {
	};
	
	virtual const core::aabbox3d<irr::f32>& getBoundingBox() const
	{
		return boundingBox;
	}
	virtual void render () {
	}


};
#endif


/* in main.cpp
*
*/


core::array<MySceneNode*> ModelsToDraw;
ModelsToDraw.push_back(new MySceneNode(smgr->getRootSceneNode(), smgr, -1));
ModelsToDraw.push_back(new MySceneNode(smgr->getRootSceneNode(), smgr, -1));
for(u32 i = 0; i < ModelsToDraw.size(); i++) 
{ 
// put your loop stuff in here.
// I'd recommend not doing that, instead put the node specific behavior in a function called onAnimate(),
// this will make it easy to code later on. If your code requires all the custom nodes to interact, create a 
// custom map scene node, or world scene node that contains the MySceneNodes as children.
} 

I hope this answers your question.

Posted: Wed Mar 02, 2011 1:18 am
by LookingForAPath
greenya, you answered my question perfectly, it seems I might have to set up a individual pointer to each one. Maybe I can do this by setting a Name for each one in the class.

Also, I've always done my arrays like that, so no worries.

On the other hand, you did explain somethings I was interested in.

Thanks guys.