#include "irrAR.h"
#include "irrString.h"

#define WIN_WIDTH 352
#define WIN_HEIGHT 288

IrrlichtDevice *device;
IVideoDriver* driver;
ISceneManager* smgr;
IARManager* armgr;

class MyEventReceiver : public IEventReceiver
{
	public:
		virtual bool OnEvent(const SEvent &event)
		{ 
			if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
			{
				switch(event.KeyInput.Key)
				{
					case KEY_ESCAPE:
						device->closeDevice();
						return true;
				}
			}
			return false;
		}
};

void verifyMarkers(IARManager* armgr)
{
	if(armgr->patt_node[0]->isVisible()){
		//vector3df vect = armgr->multi_node[0]->getScale();

		//vect.X = vect.X + 5;
		//vect.Y = vect.Y + 5;
		//vect.Z = vect.Z + 5;

		//armgr->multi_node[0]->setScale(vect );
		//armgr->addARMultiSceneNode(armgr->addARMultiSceneNode("ardata/multi/marker.dat", node);
	}
}

int main()
{
	string<c8> BaseDir = "C:\\Users\\Felipe\\Projetos\\CG\\AR\\Castle\\";
	bool fullscreen = false;
	MyEventReceiver receiver;

	
	device = createDevice( video::EDT_OPENGL, dimension2d<s32>(WIN_WIDTH, WIN_HEIGHT), 16, fullscreen, false, false, &receiver);
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	device->setWindowCaption(L"Castle");

	//something for 3d reference
	//IAnimatedMesh* mesh = smgr->getMesh((BaseDir + "media\\sydney.md2").c_str() );
	//IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
	//if (node)
	//{
	//	node->setMaterialTexture( 0, driver->getTexture( (BaseDir + "media\\sydney.bmp").c_str()) );
	//	node->setMaterialFlag(EMF_LIGHTING, false);
	//	node->setMD2Animation(scene::EMAT_POINT);
	//	node->setPosition(vector3df(0,50,0));
	//	node->setScale(vector3df(2,2,2));
	//}

	IAnimatedMesh* mesh = smgr->getMesh((BaseDir + "media\\sydney.md2").c_str() );
	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
	if (node)
	{
		node->setMaterialTexture( 0, driver->getTexture( (BaseDir + "media\\sydney.bmp").c_str()) );
		node->setMaterialFlag(EMF_LIGHTING, false);
		node->setMD2Animation(scene::EMAT_POINT);
		node->setPosition(vector3df(0,50,0));
		node->setScale(vector3df(2,2,2));
	}

	//fairy
	IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(smgr->getMesh((BaseDir + "media\\faerie.md2").c_str()));
	if (fairy)
	{
		fairy->setMaterialTexture(0, driver->getTexture((BaseDir +"media\\faerie2.bmp").c_str()));
		fairy->setMaterialFlag(video::EMF_LIGHTING, false);
		fairy->setMD2Animation(scene::EMAT_POINT);
		fairy->setScale(vector3df(2,2,2));
		fairy->setPosition(vector3df(0,45,0));
		fairy->setRotation(vector3df(0,180,0));
	}
	
	//dwarf
	IAnimatedMeshSceneNode* dwarf = smgr->addAnimatedMeshSceneNode(smgr->getMesh((BaseDir + "media\\dwarf.x").c_str()));
	dwarf->setRotation(vector3df(0,180,0));
	dwarf->setMaterialFlag(video::EMF_LIGHTING, false);
	dwarf->setAnimationSpeed(15);
	
	//water
	mesh = smgr->addHillPlaneMesh("myHill", dimension2d<f32>(80,80), dimension2d<u32>(3,2), 0, 0, dimension2d<f32>(0,0), dimension2d<f32>(10,10));
	ISceneNode* water = smgr->addWaterSurfaceSceneNode(mesh->getMesh(0), 3.0f, 300.0f, 30.0f);
	water->setMaterialTexture(0, driver->getTexture((BaseDir +"media\\stones.jpg").c_str()));
	water->setMaterialTexture(1, driver->getTexture((BaseDir +"media\\water.jpg").c_str()));
	water->setMaterialType(video::EMT_REFLECTION_2_LAYER);
	water->setMaterialFlag(video::EMF_LIGHTING, false);
	water->setPosition(vector3df(100,0,-50));
	water->setParent(dwarf);

	//camera 
	vector3df camera_pos = vector3df(0,0,0);
	vector3df camera_target = vector3df(0,0,1);
	ICameraSceneNode* camera = smgr->addCameraSceneNode(0, camera_pos, camera_target);
	
	//AR 
	IARManager* armgr = new IARManager(device);
	//init the camera
	armgr->beginCamera( (char *)(BaseDir +"ardata\\camera_para.dat").c_str(), (char *)(BaseDir +"ardata\\WDM_camera_flipV.xml").c_str(), "-dev=/dev/video0");

	//node for the lady, attached the the hiro pattern
	armgr->addARSceneNode("ardata/patt.hiro", node);

    //node for the lady, attached the the hiro pattern
	armgr->addARSceneNode("ardata/patt.kanji", node);

	//node for the fairy and water, attached the the sample1 pattern
	armgr->addARSceneNode("ardata/patt.sample1", fairy);

	//node for the dwarf, attached to a multi-pattern
	armgr->addARMultiSceneNode("ardata/multi/marker.dat", dwarf);

	//now fix our camera to display right
	armgr->fixCamera(camera);

	
	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));
		
		armgr->run();
		
		armgr->drawBackground();

		smgr->drawAll();

		driver->endScene();

		verifyMarkers(armgr);
	}

	device->drop();

	return 0;
}


