Page 1 of 1

I Need To Create Function To Load A Mesh

Posted: Tue Jun 19, 2007 5:49 pm
by tharshan
Hello,

i need help with creating my own function..
so it can be called later..

i am using : Microsoft Visual C++ 2005 Express Edition.

i need a function to load mesh.. without writing the addsceneNode ,addMesh ,etc commands again & again.

so..it will be like as : LoadObject(nodeName,"Model.X")

so i can use it like this.

LoadObject(player1,"player1.X")
LoadObject(enemy,"ghost.X")
LoadObject(player2,"opn.X")

PS : is it possible to do like this . can nodes be created at runtime ?

Thank You.

Posted: Tue Jun 19, 2007 5:58 pm
by GuerillaSoftworks
Am I mistaken in saying that

Code: Select all

ISceneNode* player1 = scenemgr->addAnimatedMeshSceneNode(
                scenemgr->getMesh("player1.X"));
Produces the same result as what you are trying to do and that you seem to be too used to a BASIC-style language?

If you were to use your 'own function' it would have to return the name of your node. Therefore why would 'nodeName' be a parameter in your function? Alternitvly you could have created the node names as global variables and then change there value within your function but this isnt a very clever thing to do.

I don't think I understand your logic here? Or have I misunderstood your question?

Posted: Tue Jun 19, 2007 6:06 pm
by tharshan
thank you,

you are correct.
but.

ex: if i create the function / call it like this..

LoadObject(player1,"opn.X")

player1 : node name
opn.x (string) : file name to load..

if we create a function it will be easier than normal.
example :

if you want to assign the same materials for your meshes.
Or you can set the scale of mesh in function.


no need to write it like :


ISceneNode* player1 = scenemgr->addAnimatedMeshSceneNode(
scenemgr->getMesh("player1.X"));
player1->setScale(1,1,......
player1->setMaterialFlag.....
etc..

i want that function to do this thing automaticly.

Thank You.

Posted: Tue Jun 19, 2007 6:33 pm
by GuerillaSoftworks
Surely though, the nodes 'player1' and 'player2' would have different settings. So a function to automate this would be useless.

But if you would want to do this then the following should work if ive understood you correctly. It's untested though so I cant say i will just work.


function declaration:

Code: Select all

ISceneNode LoadObject(wchar_t fileName)
{

ISceneNode* node = scenemgr->addAnimatedMeshSceneNode(
                scenemgr->getMesh(fileName));

node->setScale(1,1,...... );
node->setMaterialFlag.....);

// ...other irrlicht functions follow as approiate...

return node;

};
function use (e.g. in 'int main()' function):

Code: Select all

ISceneNode *player1 = LoadObject("player1.x");
If you want it to look more BASIC-like, i.e. without 'ISceneNode *player1 = ' before the function call, then declare the node in a separate statement or use global variables for your nodes. If you use global variables then use this function instead.

Code: Select all

void LoadObject(nodeName, wchar_t fileName)
{

nodeName = scenemgr->addAnimatedMeshSceneNode(
                scenemgr->getMesh(fileName));

nodeName->setScale(1,1,...... );
nodeName->setMaterialFlag.....);

// ...other irrlicht functions follow as approiate...

};
And this function call.

Code: Select all

LoadObject(player1,"player1.x");
Note that you would have to declare all nodes you are going to use, globally before all this.

I'd would advise strongly against this method though.


Good luck.

Posted: Tue Jun 19, 2007 6:50 pm
by tharshan
thank you for your reply & example..

its enough.

but i have a question..

ISceneNode *player1..

do we need to declare that node pointer in global.. ? before using the LoadObject() function ?

if yes.. its better to do it in normal way without functions..


cannot a node created from inside a function ?


void LoadObject(ISceneNode* nodeName, wchar_t fileName)
{

>>> nodeName = scenemgr->addAnimatedMeshSceneNode(
can we declare the Node pointer here ?


Thanks !! :D

Posted: Tue Jun 19, 2007 7:03 pm
by tharshan
when i use this iam getting error.. in the line where it point to sceneManager..

nodeName = smngr->addAnimatedMeshSceneNode(smngr->getMesh(fileName));

it says the smngr is not declared.

do i need to declare smngr as Global ?

Posted: Tue Jun 19, 2007 7:09 pm
by GuerillaSoftworks
Try changing

Code: Select all

nodeName = smngr->addAnimatedMeshSceneNode(smngr->getMesh(fileName));
to

Code: Select all

nodeName = scenemgr->addAnimatedMeshSceneNode(scenemgr->getMesh(fileName));
and make sure you have

Code: Select all

ISceneManager* scenemgr = device->getSceneManager();
at the beginning of your 'int main()' or further back if required.

Ill reply to your previous post soon.

Posted: Tue Jun 19, 2007 7:18 pm
by GuerillaSoftworks
A node can be created from within a function but it is does not exist outside that function that's a pointer to the node is returned from the function.

However, if you must do it the global way, then I think you could declare a global from within a function like so:

Code: Select all

static ISceneNode node;
but im not sure about this as ive never tryed to do it.

Also I have changed some code i posted. Check the 'void LoadObject()' declaration.

Posted: Tue Jun 19, 2007 10:18 pm
by tharshan
Thanks a lot..

it really helped me ! :P

Posted: Tue Jun 19, 2007 10:41 pm
by GuerillaSoftworks
No problems.

It would be interesting to see just how your using this code. So if possible is there any chance you could post your project when complete. I'm interested in seeing what would require this somewhat uncommon method of doing things. Thanks.

yes.

Posted: Tue Jun 19, 2007 10:44 pm
by tharshan
Yes.

Sure !


i will post that soon..

my idea is..

read a text file & create objects from that..

so it can be used as simple level data for games ,etc

Thank You For Your Support

Posted: Wed Jun 20, 2007 4:53 am
by vitek
my idea is.. read a text file & create objects from that.. so it can be used as simple level data for games ,etc
Sounds a heck of a lot like what is done with smgr->loadScene() and smgr->saveScene(). In case you didn't know, you can generate XML files that describe an entire scene using irrEdit, and you can load them with a single line of code.

Travis

Posted: Wed Jun 20, 2007 8:45 am
by GuerillaSoftworks
@ vitek... yeh this is something I was wondering about also. There must be some reason why hes doing things this way. Though I cant see what Im sure itll become apparent when hes done.