Esempio n. 1
0
void CIGLoader::load()
{
	for (int i=sizeof(IGFiles)/sizeof(IGFiles[0]);i--;)
	{
		try
		{
			uint k;
			CInstanceGroup ig;
			CIFile inputStream;
			if (!inputStream.open(IGFiles[i]))
			{
				nlinfo("unable to open %s", argv[1]);
				return -1;
			}
			ig.serial(inputStream);
			CVector gpos = ig.getGlobalPos(); // the origin for the ig
			for(k = 0; k < ig._InstancesInfos.size(); ++k)
			{
				nlinfo("instance %s : x = %.1f, y = %.1f, z = %.1f, sx = %.1f, sy = %.1f, sz = %.1f", ig._InstancesInfos[k].Name.c_str(), ig._InstancesInfos[k].Pos.x + gpos.x, ig._InstancesInfos[k].Pos.y + gpos.y, ig._InstancesInfos[k].Pos.z + gpos.z, ig._InstancesInfos[k].Scale.x, ig._InstancesInfos[k].Scale.y, ig._InstancesInfos[k].Scale.z);
			}
		}
		catch (std::exception &e)
		{
			nlinfo(e.what());
		}
	}
}
Esempio n. 2
0
// ***************************************************************************
CInstanceGroup* LoadInstanceGroup (const char* sFilename)
{
	CIFile file;
	CInstanceGroup *newIG = new CInstanceGroup;

	if( file.open( sFilename ) )
	{
		try
		{
			newIG->serial (file);
		}
		catch (const Exception &)
		{
			// Cannot save the file
			delete newIG;
			return NULL;
		}
	}
	else
	{
		delete newIG;
		return NULL;
	}
	return newIG;
}
Esempio n. 3
0
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
CInstanceGroup* LoadInstanceGroup(const char* sFilename)
{
	CIFile file;
	CInstanceGroup *newIG = new CInstanceGroup;

	if( file.open( CPath::lookup( string(sFilename) ) ) )
	{
		try
		{
			// Serial the skeleton
			newIG->serial (file);
			// All is good
		}
		catch (const Exception &)
		{
			// Cannot save the file
			delete newIG;
			return NULL;
		}
	}
	return newIG;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
	if (argc != 2)
	{
		printf("usage : %s file.ig\n", argv[0]);
		return -1;
	}
	try
	{
		uint k;
		CInstanceGroup ig;
		CIFile inputStream;
		if (!inputStream.open(string(argv[1])))
		{
			printf("unable to open %s\n", argv[1]);
			return -1;
		}
		ig.serial(inputStream);
		printf("Origine\n");
		printf("---------\n");
		CVector gpos = ig.getGlobalPos();
		printf("global pos : x = %.1f, y = %.1f, z =%.1f\n", gpos.x, gpos.y, gpos.z);
		printf("Instances\n");
		printf("---------\n");
		for(k = 0; k < ig._InstancesInfos.size(); ++k)
		{
			printf("instance %s : x = %.1f, y = %.1f, z = %.1f, sx = %.1f, sy = %.1f, sz = %.1f\n", ig._InstancesInfos[k].Name.c_str(), ig._InstancesInfos[k].Pos.x + gpos.x, ig._InstancesInfos[k].Pos.y + gpos.y, ig._InstancesInfos[k].Pos.z + gpos.z, ig._InstancesInfos[k].Scale.x, ig._InstancesInfos[k].Scale.y, ig._InstancesInfos[k].Scale.z);
		}
		printf("\n");
		printf("Lights\n");
		printf("---------\n");
		for(k = 0; k < ig.getNumPointLights(); ++k)
		{
			const CPointLightNamed &pl = ig.getPointLightNamed(k);
			printf("light group = %d, anim = \"%s\" x = %.1f, y = %.1f, z = %.1f\n", pl.LightGroup, pl.AnimatedLight.c_str(), pl.getPosition().x + gpos.x, pl.getPosition().y + gpos.y, pl.getPosition().z + gpos.z);
		}
		printf("---------\n");
		printf("Realtime sun contribution = %s\n", ig.getRealTimeSunContribution() ? "on" : "off");
		printf("---------\n");
		// IGSurfaceLight info.
		const CIGSurfaceLight::TRetrieverGridMap	&rgm= ig.getIGSurfaceLight().getRetrieverGridMap();
		printf("IGSurfaceLighting: CellSize: %f. NumGridRetriever: %u\n", 
			ig.getIGSurfaceLight().getCellSize(), (uint)rgm.size() );
		uint	rgmInst= 0;
		uint	totalCells= 0;
		CIGSurfaceLight::TRetrieverGridMap::const_iterator	it= rgm.begin();
		for(;it!=rgm.end();it++)
		{
			for(uint i=0;i<it->second.Grids.size();i++)
			{
				printf("grid(%d, %d): %dx%d\n", rgmInst, i, it->second.Grids[i].Width, it->second.Grids[i].Height );
				totalCells+= it->second.Grids[i].Cells.size();
			}
			rgmInst++;
		}
		printf("TotalCells: %d\n", totalCells);

	}
	catch (const std::exception &e)
	{
		printf("%s\n", e.what());
	}
}
//=======================================================================================
// ryzom specific build bbox of a village in a zone
static void computeBBoxFromVillage(const NLGEORGES::UFormElm *villageItem, 
								   const std::string &continentName,
								   uint villageIndex,
								   TShapeMap &shapeMap,
								   CLightingBBox &result
								  )
{	
	result = CLightingBBox();
	const NLGEORGES::UFormElm *igNamesItem;
	if (! (villageItem->getNodeByName (&igNamesItem, "IgList") && igNamesItem) )
	{
		nlwarning("No list of IGs was found in the continent form %s, village #%d", continentName.c_str(), villageIndex);
		return;
	}	
	// Get number of village
	uint numIgs;
	nlverify (igNamesItem->getArraySize (numIgs));
	const NLGEORGES::UFormElm *currIg;
	for(uint l = 0; l < numIgs; ++l)
	{														
		if (!(igNamesItem->getArrayNode (&currIg, l) && currIg))
		{
			nlwarning("Couldn't get ig #%d in the continent form %s, in village #%d", l, continentName.c_str(), villageIndex);
			continue;
		}			
		const NLGEORGES::UFormElm *igNameItem;
		currIg->getNodeByName (&igNameItem, "IgName");
		std::string igName;
		if (!igNameItem->getValue (igName))
		{
			nlwarning("Couldn't get ig name of ig #%d in the continent form %s, in village #%d", l, continentName.c_str(), villageIndex);
			continue;
		}
		if (igName.empty())
		{
			nlwarning("Ig name of ig #%d in the continent form %s, in village #%d is an empty string", l, continentName.c_str(), villageIndex);
			continue;
		}

		igName = CFile::getFilenameWithoutExtension(igName) + ".ig";
		string nameLookup = CPath::lookup (igName, false, true);
		if (!nameLookup.empty())
		{		
			CIFile inputFile;
			// Try to open the file
			if (inputFile.open (nameLookup))
			{				
				CInstanceGroup group;
				try
				{
					CLightingBBox currBBox;
					group.serial (inputFile);
					computeIGBBox(group, currBBox, shapeMap);											
					result.makeUnion(currBBox);					
				}
				catch(NLMISC::Exception &)
				{
					nlwarning ("Error while loading instance group %s\n", igName.c_str());	
					continue;
				}								
				inputFile.close();				
			}
			else
			{
				// Error
				nlwarning ("Can't open instance group %s\n", igName.c_str());
			}
		}								
	}	
}
Esempio n. 6
0
/*********************************************************\
					displayZone()
\*********************************************************/
void displayZones()
{
	const float zFarStep = 5.0f;
	const float	tileNearStep = 10.0f;
	const float thresholdStep = 0.005f;

	ViewerCfg.TextContext.setHotSpot(CComputedString::MiddleMiddle);
	ViewerCfg.TextContext.setColor(CRGBA(255,255,255));
	ViewerCfg.TextContext.setFontSize(20);
	
	CNELU::clearBuffers(CRGBA(0,0,0));
	CNELU::swapBuffers();



	// Create landscape
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Creating landscape...");
	CNELU::swapBuffers();
	
	Landscape = (CLandscapeModel*)CNELU::Scene->createModel(LandscapeModelId);
	Landscape->Landscape.setNoiseMode (ViewerCfg.LandscapeNoise);
	Landscape->Landscape.setTileNear(ViewerCfg.LandscapeTileNear);
	Landscape->Landscape.setThreshold(ViewerCfg.LandscapeThreshold);

	Landscape->Landscape.enableAutomaticLighting (ViewerCfg.AutoLight);
	Landscape->Landscape.setupAutomaticLightDir (ViewerCfg.LightDir);

	// Enable Additive Tiles.
	Landscape->enableAdditive(true);

	// HeightField.
	CBitmap		heightBitmap;
	CIFile file(ViewerCfg.HeightFieldName);

	if( ViewerCfg.HeightFieldName!="" && heightBitmap.load(file) )
	{
		CHeightMap	heightMap;
		heightMap.buildFromBitmap(heightBitmap);
		heightMap.MaxZ= ViewerCfg.HeightFieldMaxZ;
		heightMap.OriginX= ViewerCfg.HeightFieldOriginX;
		heightMap.OriginY= ViewerCfg.HeightFieldOriginY;
		heightMap.SizeX = ViewerCfg.HeightFieldSizeX;
		heightMap.SizeY = ViewerCfg.HeightFieldSizeY;
		Landscape->Landscape.setHeightField(heightMap);
	}

	
	// Init TileBank.
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Initializing TileBanks...");
	CNELU::swapBuffers();

	try 
	{
		CIFile bankFile (ViewerCfg.BanksPath + "/" + ViewerCfg.Bank);
		Landscape->Landscape.TileBank.serial(bankFile);
	}
	catch(Exception)
	{
		string tmp = string("Cant load bankfile ")+ViewerCfg.BanksPath + "/" + ViewerCfg.Bank;
		nlerror (tmp.c_str());
	}

	if ((Landscape->Landscape.TileBank.getAbsPath ()=="")&&(ViewerCfg.TilesPath!=""))
		Landscape->Landscape.TileBank.setAbsPath (ViewerCfg.TilesPath + "/");

	if (ViewerCfg.UseDDS)
	{
		Landscape->Landscape.TileBank.makeAllExtensionDDS();
	}

	if (ViewerCfg.AllPathRelative)
		Landscape->Landscape.TileBank.makeAllPathRelative();

	sint idx = (sint)ViewerCfg.Bank.find(".");
	string farBank = ViewerCfg.Bank.substr(0,idx);
	farBank += ".farbank";

	try
	{
		CIFile farbankFile(ViewerCfg.BanksPath + "/" + farBank);
		Landscape->Landscape.TileFarBank.serial(farbankFile);
	}
	catch(Exception)
	{
		string tmp = string("Cant load bankfile ")+ViewerCfg.BanksPath + "/" + farBank;
		nlerror (tmp.c_str());
	}
	
	if ( ! Landscape->Landscape.initTileBanks() )
	{
		nlwarning( "You need to recompute bank.farbank for the far textures" );
	}
	
	// Init light color
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Initializing Light...");
	CNELU::swapBuffers();
	
	Landscape->Landscape.setupStaticLight (ViewerCfg.LandDiffuse, ViewerCfg.LandAmbient, 1.1f);

	// Init collision manager
	CollisionManager.init( &(Landscape->Landscape), 200);
	

	// Preload of TileBank
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Loading TileBank...");
	CNELU::swapBuffers();
	
	for (int ts=0; ts<Landscape->Landscape.TileBank.getTileSetCount (); ts++)
	{
		CTileSet *tileSet=Landscape->Landscape.TileBank.getTileSet (ts);
		sint tl;
		for (tl=0; tl<tileSet->getNumTile128(); tl++)
			Landscape->Landscape.flushTiles (CNELU::Scene->getDriver(), (uint16)tileSet->getTile128(tl), 1);
		for (tl=0; tl<tileSet->getNumTile256(); tl++)
			Landscape->Landscape.flushTiles (CNELU::Scene->getDriver(), (uint16)tileSet->getTile256(tl), 1);
		for (tl=0; tl<CTileSet::count; tl++)
			Landscape->Landscape.flushTiles (CNELU::Scene->getDriver(), (uint16)tileSet->getTransition(tl)->getTile (), 1);
	}
		

	// Build zones.
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Loading zones...");
	CNELU::swapBuffers();
	uint32 i;
	for(i =0; i<ViewerCfg.Zones.size(); i++)
	{
		CZone zone;
		try
		{
			CIFile file(CPath::lookup(ViewerCfg.Zones[i]));
			zone.serial(file);
			file.close();

			// Add it to landscape.
			Landscape->Landscape.addZone(zone);

			// Add it to collision manager.
			CollisionManager.addZone(zone.getZoneId());
		}
		catch(Exception &e)
		{
			printf(e.what ());
		}		
	}
	

		

	// Load instance group.
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Loading objects...");
	CNELU::swapBuffers();
	for(i =0; i<ViewerCfg.Igs.size(); i++)
	{
		CInstanceGroup *group = new CInstanceGroup;
		try
		{
			CIFile file(CPath::lookup(ViewerCfg.Igs[i]));
			group->serial(file);
			file.close();

			// Add it to the scene.
			group->addToScene (*CNELU::Scene);
		}
		catch(Exception &e)
		{
			printf(e.what ());
		}		
	}
	
	// Init collision Manager.
	CNELU::clearBuffers(CRGBA(0,0,0));
	ViewerCfg.TextContext.printfAt(0.5f,0.5f,"Initializing collision manager...");
	CNELU::swapBuffers();
	
	
	CollisionManager.setCenter(ViewerCfg.Position);
	ViewerCfg.Position.z = 0.0f;
	CollisionManager.snapToGround( ViewerCfg.Position, 1000.0f );

	

	// hide mouse cursor
	CNELU::Driver->showCursor(false);
#ifdef NL_RELEASE
	CNELU::Driver->setCapture(true);
#endif

	
		
	// Events management
	CNELU::EventServer.addEmitter(CNELU::Driver->getEventEmitter());
	CNELU::AsyncListener.addToServer(CNELU::EventServer);
	
	MoveListener.init(CNELU::Scene, ViewerCfg.Width, ViewerCfg.Height, *CNELU::Camera);
	MoveListener.addToServer(CNELU::EventServer);
	MoveListener.setPos( ViewerCfg.Position );	

	CNELU::Camera->setPerspective (float(80.0*Pi/180.0), 1.33f, 0.1f, 1000.0f);
	
	bool showInfos = true;


	// initializing Z-Clip Far
	float left;
	float right;
	float bottom;
	float top; 
	float znear;
	float zfar;
	CNELU::Camera->getFrustum(left, right, bottom, top, znear, zfar);
	zfar = ViewerCfg.ZFar;
	CNELU::Camera->setFrustum(left, right, bottom, top, znear, zfar);
	
	
	do
	{
		// Time mgt.
		//==========
		static sint64 t0 = (sint64)CTime::getLocalTime();
		static sint64 t1 = (sint64)CTime::getLocalTime();
		static sint64 ts = 0;

		t0 = t1;
		t1 = (sint64)CTime::getLocalTime();
		sint64 dt64 = t1-t0;
		ts += dt64;
		float	dt= ((float)dt64)*0.001f;

	
		CNELU::EventServer.pump();

		
		// Manage movement and collision
		MoveListener.setLocalTime(CTime::getLocalTime());
		CVector oldpos = MoveListener.getPos();
		MoveListener.changeViewMatrix();
		if(MoveListener.getMode()==CMoveListener::WALK)
		{
			CVector pos = MoveListener.getPos();
			CollisionManager.snapToGround( pos , 1000.0f );
			MoveListener.setPos( pos );
		}
		CollisionManager.setCenter(MoveListener.getPos()); 

				
		// Change move mode
		if(CNELU::AsyncListener.isKeyPushed(KeySPACE))
		{
			MoveListener.swapMode();	
		}
		
		
		// Change displaying infos state
		if(CNELU::AsyncListener.isKeyPushed(KeyF1))
		{
			showInfos = !showInfos;
		}


		// Change eyes height
		float eh = MoveListener.getEyesHeight();
		if(CNELU::AsyncListener.isKeyPushed(KeyADD))
		{
			ViewerCfg.EyesHeight.z += 0.1f;
			eh += 0.1f;
		}
		if(CNELU::AsyncListener.isKeyPushed(KeySUBTRACT))
		{
			ViewerCfg.EyesHeight.z -= 0.1f;
			eh -= 0.1f;
		}
		if(ViewerCfg.EyesHeight.z<0.1f) ViewerCfg.EyesHeight.z = 0.1f;
		if(eh<0.1f) eh = 0.1f;
		MoveListener.setEyesHeight(eh);


		// Change TileNear
		float tileNear = Landscape->Landscape.getTileNear();
		if(CNELU::AsyncListener.isKeyPushed(KeyHOME))
			tileNear += tileNearStep;
		if(CNELU::AsyncListener.isKeyPushed(KeyEND))
			tileNear -= tileNearStep;
		if(tileNear<0) tileNear = 0;
		Landscape->Landscape.setTileNear(tileNear);


		// Change Z-Far
		CNELU::Camera->getFrustum(left, right, bottom, top, znear, zfar);
		if(CNELU::AsyncListener.isKeyDown(KeyPRIOR))
			zfar += zFarStep;
		if(CNELU::AsyncListener.isKeyDown(KeyNEXT))
			zfar -= zFarStep;
		if(zfar<0) zfar = 0;
		CNELU::Camera->setFrustum(left, right, bottom, top, znear, zfar);


		// Change Threshold
		float threshold = Landscape->Landscape.getThreshold();
		if(CNELU::AsyncListener.isKeyPushed(KeyINSERT))
			threshold += thresholdStep;
		if(CNELU::AsyncListener.isKeyPushed(KeyDELETE))
			threshold -= thresholdStep;
		if(threshold<0.001f) threshold = 0.001f;
		if(threshold>0.1f) threshold = 0.1f;
		Landscape->Landscape.setThreshold(threshold);


		// Switch between wired and filled scene display
		if(CNELU::AsyncListener.isKeyPushed(KeyF3))
		{
			if (CNELU::Driver->getPolygonMode ()==IDriver::Filled)
				CNELU::Driver->setPolygonMode (IDriver::Line);
			else
				CNELU::Driver->setPolygonMode (IDriver::Filled);
		}

		
		// Switch between mouse move and keyboard-only move
		if(CNELU::AsyncListener.isKeyPushed(KeyRETURN))
		{
			MoveListener.changeControlMode();
		}
		

		
		// Render
		//=======
		CNELU::clearBuffers(ViewerCfg.Background);
		CNELU::Driver->clearZBuffer();
		CNELU::Scene->render();

				
		if(showInfos)
		{
			
			// black top quad
			CDRU::drawQuad(0,0.97f,1.0f,1.0f,*CNELU::Driver,CRGBA(0,0,0),CNELU::Scene->getViewport());

			// black bottom quad
			CDRU::drawQuad(0,0,1.0f,0.03f,*CNELU::Driver,CRGBA(0,0,0),CNELU::Scene->getViewport());

			
			ViewerCfg.TextContext.setFontSize(12);
			ViewerCfg.TextContext.setColor(CRGBA(255,255,255));

			// Display fps.
			ViewerCfg.TextContext.printfAt(0.05f,0.98f,"%.1f fps",1/dt);

			// Display ms
			ViewerCfg.TextContext.printfAt(0.12f,0.98f,"%d ms",dt64);

			// Display Tile Near
			ViewerCfg.TextContext.printfAt(0.75f,0.98f,"Tile Near : %.1f",tileNear);

			//Display moving mode
			ViewerCfg.TextContext.setColor(CRGBA(255,0,0));
			switch(MoveListener.getMode())
			{
				case CMoveListener::WALK :
					ViewerCfg.TextContext.printfAt(0.5f,0.98f,"Walk Mode");
					break;
				case CMoveListener::FREE :
					ViewerCfg.TextContext.printfAt(0.5f,0.98f,"Free-Look Mode");
					break;
				default:
					break;
			}
			ViewerCfg.TextContext.setColor(CRGBA(255,255,255));

			// Display Threshold
			ViewerCfg.TextContext.printfAt(0.3f,0.98f,"Threshold : %.3f",threshold);

			// Display Clip Far
			ViewerCfg.TextContext.printfAt(0.92f,0.98f,"Clip Far : %.1f",zfar);

			
			ViewerCfg.TextContext.setHotSpot(CComputedString::MiddleBottom);
			
			// Display current zone name
			CVector pos = MoveListener.getPos();
			string zoneName = getZoneNameByCoord(pos.x, pos.y);
			ViewerCfg.TextContext.printfAt(0.3f,0.01f,"Zone : %s",zoneName.c_str());

			// Position
			ViewerCfg.TextContext.printfAt(0.1f,0.01f,"Position : %d %d %d",(sint)pos.x,(sint)pos.y,(sint)pos.z);

			// Eyes height
			ViewerCfg.TextContext.printfAt(0.7f,0.01f,"Eyes : %.2f m",ViewerCfg.EyesHeight.z);

			// Display speed in km/h
			ViewerCfg.TextContext.setColor(CRGBA(255,0,0));
			ViewerCfg.TextContext.printfAt(0.5f,0.01f,"Speed : %d km/h",(sint)(MoveListener.getSpeed()*3.6f));
			ViewerCfg.TextContext.setColor(CRGBA(255,255,255));

			// Heading
			sint heading = -(sint)(MoveListener.getRotZ()*180/Pi)%360;
			if(heading<0) heading += 360;
			ViewerCfg.TextContext.printfAt(0.9f,0.01f,"Heading : %d degrees",heading);

			// Display the cool compass.
			displayOrientation();
		}

		
		CNELU::swapBuffers();
		CNELU::screenshot();
	}
	while(!CNELU::AsyncListener.isKeyPushed(KeyESCAPE));





	ViewerCfg.Position = MoveListener.getPos();
	
	CNELU::AsyncListener.removeFromServer(CNELU::EventServer);
	MoveListener.removeFromServer(CNELU::EventServer);

	CNELU::Driver->showCursor(true);
#ifdef NL_RELEASE
	CNELU::Driver->setCapture(false);
#endif
}