Loading textures at runtime?

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!
Post Reply
tabrax
Posts: 6
Joined: Wed Jul 21, 2004 5:41 pm
Location: Germany

Loading textures at runtime?

Post by tabrax »

Hi

i try to load textures at runtime but it doesn't work :(

Code: Select all

IrrlichtDevice*device	=	0;	 
int mdl_id				=100000;
int m_a_n				=0;


class Eventhandle : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
	{
...
	break;
	case EGET_FILE_SELECTED:
							if (id == 107)
								{
									if (model_select_change== 1 )
										{
										model_select_change= 0;
										}
									if (model_select_change== 0 && mdl_id > 100000)
										{
										model_select_change= 1;
										mdl_id-=1;
										}
									IGUIFileOpenDialog* dialog =  (IGUIFileOpenDialog*)event.GUIEvent.Caller;
									loadModel(core::stringc(dialog->getFilename()).c_str());
									debug_tab_fb_m->setText(debug_tab_fo_m ->getFilename());
								}
							else
							if (id == 108)
								{
									IGUIFileOpenDialog* dialog =  (IGUIFileOpenDialog*)event.GUIEvent.Caller;
									loadTexture1(core::stringc(dialog->getFilename()).c_str());
  									debug_tab_fb_t1->setText(debug_tab_fo_t1 ->getFilename());
								}
							else
							if (id == 109)
								{
									IGUIFileOpenDialog* dialog =  (IGUIFileOpenDialog*)event.GUIEvent.Caller;
									//loadTexture1(core::stringc(dialog->getFilename()).c_str());
  								debug_tab_fb_t2->setText(debug_tab_fo_t2 ->getFilename());
								}
							return true;
          ...
          }

void loadModel(const c8* filename)
	{
			scene::ISceneManager* sm = device->getSceneManager();
			scene::ICameraSceneNode* camera = sm->getActiveCamera();
			core::vector3df camera_pos =camera->getPosition();	 
			scene::IAnimatedMesh* mesh[99999];
			scene::ISceneNode *node[99999];
			//device->getFileSystem()->addZipFileArchive(filename);
			mesh[m_a_n]	= sm->getMesh(filename);
			if (!mesh[m_a_n]) 
				{
					// model konnte nicht geladen werden
					device->getGUIEnvironment()->addMessageBox(L"Nein, nein, nein! SO geht das nicht",
					L"Das Model konnte nicht geladen werden. Kein unterstütztes Format (.x, .3ds, .ms3d, .md2). ");
					return;
				}
			node[m_a_n] = device->getSceneManager()->addAnimatedMeshSceneNode(mesh[m_a_n],0,mdl_id);
			node[m_a_n]->setPosition(vector3df(camera_pos));
			node[m_a_n]->setVisible(true);
			node[m_a_n]->setMaterialFlag(video::EMF_LIGHTING, false);
			debug_tab_fb_m->setEnabled(false);
			node[m_a_n]->setMaterialType(video::EMT_SOLID_2_LAYER);						//Lightmaps? SOLID deaktiviert die Lightmaps
			//mdl_id+=1; 
			//m_a_n+=1;
			return;
		
	}


void loadTexture1(const c8*filename)
	{
			scene::ISceneManager* sm = device->getSceneManager();
			scene::ISceneNode* temp_node= sm->getSceneNodeFromId(mdl_id);
			IGUIEnvironment	*guienv	= device->getGUIEnvironment();
			if (!temp_node)
			{
					guienv->addMessageBox(L"Nein, nein, nein! SO geht das nicht",
					L"Die Textur konnte nicht geladen werden, weil noch kein Model ausgewählt wurde!. ");
					return;
			}
			else
			{
				temp_node->	setMaterialTexture(0, device->getVideoDriver()->getTexture(filename));
			}
	}
...
};


i can load the texture (it was printed at shell) but after load the texture crashs the application. if i run at debugmodus the debugger got a beak and drops me to assambling area.


can anyone help me?
Guest

Post by Guest »

You might define your mesh and node arrays as global rather than local (on the stack and thus temporary) in your loadmodel function.
I'm not saying that's all, but that's what a quick view would suggest
timmy
Posts: 34
Joined: Wed Jul 14, 2004 2:48 am

Post by timmy »

it sounds like your problem is with getSceneNodeFromId .There is a bug in this that causes your app to just crash

here r a few threads that mentions this bug:

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=3318

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=1636 (niko himself comments on this one)

http://irrlicht.sourceforge.net/phpBB2/ ... php?t=3268
Post Reply