[Fixed]How to draw a mesh from a file without the file..

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

@hybrid:
Ok, lets say I've put that static mesh into a zip, bin2h-ed it, made it like the built in font.
Now, how can I load that zip file?
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

MasterGod wrote:1. I've added the methods you said, now which IMeshLoader should I use? (at index 0?)
You use the one that matches the format of the data you've provided. You need to iterate over the list of mesh loaders and see which one it is. Don't hardcode the index, because the index can change depending on how the Irrlicht library is compiled.
MasterGod wrote:2. I still prefer to use bin2h :wink:
I don't understand why. I still get the feeling that you're trying to 'hide' your meshes in the binary. It may seem less readable in source form, but I can guarantee you that opening the binary in a hex editor will show ascii data. i.e. const c8* msg = "hello world!" is exactly the same as const c8 msg[] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21 } in a hex editor.
MasterGod wrote:3. That's really ain't a problem for me :)
Is that because you never make mistakes, or the meshes are so primitive that they'd never need to be updated?

Travis
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I'm sure he's not trying to hide anything from anyone, not for security reasons.

He probably wants a basic mesh type of some sort, say a quad for use in his game engine and instead of making that file necessary for the person using his engine to actually have in their folder structure he wants to include it as part of his .exe or .lib so that it's just a bit easier to use.

It's something i've been doing in my basic engine i've been using. For example i have some basic shaders that my engine uses to render anything and everything and instead of the application using the engine having to have those files in their folder i just include them in the actual library.
Image Image Image
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

vitek wrote:
MasterGod wrote:1. I've added the methods you said, now which IMeshLoader should I use? (at index 0?)
You use the one that matches the format of the data you've provided. You need to iterate over the list of mesh loaders and see which one it is. Don't hardcode the index, because the index can change depending on how the Irrlicht library is compiled.
Thanks I'll do that. While writing my post I've tried and it doesn't work:

Code: Select all

// Getting the right mesh loader from irrlicht
	IAnimatedMesh* mesh = 0;
	for(u32 meshLoader = 0; meshLoader < smgr->getMeshLoaderCount(); meshLoader++)
	{
		if(mesh = smgr->getMeshLoader(meshLoader)->createMesh(memData))
			break;
	}
When meshLoader gets to 4 it still doesn't load the mesh, the console also say that it cant load it but if I replace that index in 4 like this line it works:

Code: Select all

if(mesh = smgr->getMeshLoader(4)->createMesh(memData))
			break;
EDIT: I see now in console that it tries the MD2 loader instead of each and every one of them only when I run the file from the bin folder, when I use VC to run it, the first time it tries the MD2 loader and then the second time (using menu item to display mesh again) I get bad_alloc exception but I think it because in some other part I'm trying to use that "loaded mesh"..
EDIT2: The exception is thrown because one of the mesh loaders (6th I think) throws it, still, only the second time and only when I run it with VC..
EDIT3:To be more exact when the meshLoader == 6, I'm not sure it actually tries the 6th loader cause I guess I would have seen something on the console, like the MD2's loader did and the 4th (3ds) should have already load it right..
vitek wrote:
MasterGod wrote:2. I still prefer to use bin2h :wink:
I don't understand why. I still get the feeling that you're trying to 'hide' your meshes in the binary. It may seem less readable in source form, but I can guarantee you that opening the binary in a hex editor will show ascii data. i.e. const c8* msg = "hello world!" is exactly the same as const c8 msg[] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21 } in a hex editor.
Exactly what JP said, I also provide the actual mesh as .3ds file in the media directory and I note that in the generated .h file so I really don't try to hide anything..
vitek wrote:
MasterGod wrote:3. That's really ain't a problem for me :)
Is that because you never make mistakes, or the meshes are so primitive that they'd never need to be updated?

Travis
Kinda, yes (being so primitive)
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

Hybrid, I think the point of what hes trying to do is only have one .exe file without any external resource files. In some programming contests, or when trying to distribute a program to non-technical people this can be useful.
a screen cap is worth 0x100000 DWORDS
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I don't understand why this is so complicated...

Code: Select all

// this function creates an animated mesh from an IReadFile. requires
// access to the mesh loaders shown above
scene::IAnimatedMesh*
ISceneManager_createMeshFromFile(scene::ISceneManager* smgr, io::IReadFile* file)
{
  const c8* fileName = file->getFileName();

  for (u32 n = 0; n < smgr->getMeshLoaderCount(); ++n)
  {
    scene::IMeshLoader* loader = smgr->getMeshLoader(n);
    if (loader && loader->isALoadableFileExtension(fileName))
    {
      scene::IAnimatedMesh* mesh = loader->createMesh(file);
      if (mesh) {
        //// you could put the mesh into the mesh cache...
        //// this would allow a call to smgr->getMesh() to find the mesh
        //scene::IMeshCache* cache = smgr->getMeshCache();
        //if (cache)
        //  cache->addMesh (fileName, mesh);

        return mesh;
      }
    }
  }

  return 0;
}

// this is a 3ds mesh as binary data
u8 myMeshData[] = {
  // mesh data as constructed with bin2h
  // if you don't have access to bin2h source, you can write it pretty easily.
};

// load a bunch of data into a memory read file. this would go in your main or whatever
int main ()
{
  // do regular irrlicht stuff

  io::IReadFile* memoryFile = fileSystem->createMemoryReadFile(myMeshData, sizeof(myMeshData), "myMesh.3ds");

  scene::IAnimatedMesh* mesh = ISceneManager_createMeshFromFile(smgr, memoryFile);

  memoryFile->drop();


  scene::IMeshSceneNode* node = 0;
  if (mesh)
    node = smgr->addMeshSceneNode(mesh->getMesh(0));

  // do more irrlicht stuff...

  return 0;
}
Travis
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Thanks vitek. It works now.
I just didn't know of isALoadableFileExtension() method and the cache thing is a good idea too, thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

MasterGod wrote:I just didn't know of isALoadableFileExtension() method
That is why you need to look at the documentation or the class declaration. Everything you need to know is there, you just have to open the eyes so you can see it.

Travis
Post Reply