Example #1
0
void CollisionModel::create()
{
	data->model = boost::shared_ptr<IStorm3D_Model> (data->storm.storm->CreateNewModel());
	data->model->CastShadows(false);
	
	IStorm3D_Material *material = data->storm.storm->CreateNewMaterial("ah");
	IStorm3D_Model_Object *object = data->model->Object_New("uh");
	IStorm3D_Mesh *mesh = data->storm.storm->CreateNewMesh();

	material->SetSpecial(true, true);
	material->SetColor(data->color);
	material->SetSelfIllumination(COL(1.f,1.f,1.f));

	mesh->UseMaterial(material);
	data->createMesh(*mesh);
	object->SetMesh(mesh);

	QUAT rotation = getRotation(data->rotation);
	//rotation.MakeFromAngles(0, data->yAngle, 0);

	data->model->SetPosition(data->position);
	data->model->SetRotation(rotation);

	data->addScene();
}
Example #2
0
	void Muzzleflasher::updateMuzzleflash(IPointableObject *unit, VisualObject *muzzleflash, const std::string &name, const std::string &helper, const VC3 &pos, const VC3 &scale)
	{
		game::Unit *u = (game::Unit *)unit;

		VisualObject *vo = u->getVisualObject();
		if (vo == NULL || vo->getStormModel() == NULL)
		{
			return;
		}

		IStorm3D_Model_Object *object = vo->getStormModel()->SearchObject(name.c_str());
		if(object == NULL)
		{
			// didn't find it - add it
			createMuzzleflash(unit, muzzleflash, name, helper);
			object = vo->getStormModel()->SearchObject(name.c_str());
			if(object == NULL)
			{
				return;
			}
		}

		object->SetPosition(pos);
		object->SetScale(scale);
		//object->SetRotation(rot);
	}
		SharedModel(shared_ptr<IStorm3D_Model> model_, shared_ptr<IStorm3D_Model> fadeModel_, const std::string &bones_, const std::string &idleAnimation_)
		:	model(model_),
			fadeModel(fadeModel_),
			bones(bones_),
			idleAnimation(idleAnimation_),
			radius(0),
			radius2d(0)
		{
			boost::scoped_ptr<Iterator<IStorm3D_Model_Object *> > objectIterator(model->ITObject->Begin());
			for(; !objectIterator->IsEnd(); objectIterator->Next())
			{
				IStorm3D_Model_Object *object = objectIterator->GetCurrent();
				if(!object)
					continue;

				IStorm3D_Mesh *mesh = object->GetMesh();
				if(!mesh)
					continue;

				VC3 objectPosition = object->GetPosition();
				float objectDistance = objectPosition.GetLength();
				float distance = objectDistance + mesh->GetRadius();
				if(distance > radius)
					radius = distance;

				float meshRadius = mesh->GetRadius();
				if(meshRadius + objectDistance > radius2d)
					radius2d = meshRadius + objectDistance;
			}

			if(!bones.empty())
				model->LoadBones(bones.c_str());
		}
/*!
	\param modelId model ID
	\param instanceId instance ID
	\param factor fade factor
*/
void Storm3D_TerrainGroup::setInstanceFade(int modelId, int instanceId, float factor)
{
	assert(modelId >= 0 && instanceId >= 0);
	if(modelId < 0 || instanceId < 0 || modelId >= int(data->models.size()))
		return;

	SharedModel &original = data->models[modelId];
	if(instanceId >= int(original.instances.size()))
		return;

	Instance &instance = *original.instances[instanceId];
	if(factor < 0.02f)
		factor = 0.02f;

	if(instance.fadeModel)
	{
		boost::scoped_ptr<Iterator<IStorm3D_Model_Object *> > objectIterator(instance.fadeModel->ITObject->Begin());
		for(; !objectIterator->IsEnd(); objectIterator->Next())
		{
			IStorm3D_Model_Object *object = objectIterator->GetCurrent();
			if(!object)
				continue;

			object->SetForceAlpha(factor);
		}

		data->terrainModels.addModel(*instance.fadeModel);
	}
}
Example #5
0
		void tick()
		{
			if(storm.scene)
			{
				std::string id = getDialogItemText(dialog, IDC_FOG_ID);
				fogApplier.setActiveFog(id);

				fogApplier.setScene(*storm.scene);
				fogApplier.update(storm.scene->GetCamera()->GetPosition().y, getTimeDelta());

				// Hacky hacky
				bool shouldShowEditorOnly = isCheckEnabled(dialog, IDC_SHOW_EDITORONLY);
				if(showEditorOnly != shouldShowEditorOnly)
				{
					if(storm.storm)
					{
						boost::scoped_ptr<Iterator<IStorm3D_Model *> > itm(storm.storm->ITModel->Begin());
						for(; !itm->IsEnd();  itm->Next())
						{
							IStorm3D_Model *model = itm->GetCurrent();
							if(!model)
								continue;

							boost::scoped_ptr<Iterator<IStorm3D_Model_Object *> > ito(model->ITObject->Begin());
							for(; !ito->IsEnd(); ito->Next())
							{
								IStorm3D_Model_Object *object = ito->GetCurrent();
								if(!object)
									continue;

								std::string name = object->GetName();
								if(name.find("EditorOnly") != name.npos)
								{
									if(shouldShowEditorOnly)
										object->SetNoRender(false);
									else
										object->SetNoRender(true);
								}
							}
						}
					}

					showEditorOnly = shouldShowEditorOnly;
				}
			}
		}
Example #6
0
void CollisionModel::clone(CollisionModel &rhs)
{
	data->model = boost::shared_ptr<IStorm3D_Model> (data->storm.storm->CreateNewModel());
	IStorm3D_Model_Object *object = data->model->Object_New("uh");

	IStorm3D_Model_Object *rhsObject = rhs.data->model->SearchObject("uh");
	if(rhsObject)
		object->SetMesh(rhsObject->GetMesh());

	QUAT rotation = getRotation(data->rotation);
	//rotation.MakeFromAngles(0, data->yAngle, 0);

	data->model->SetPosition(data->position);
	data->model->SetRotation(rotation);

	data->model->CastShadows(false);
	data->addScene();
}
Example #7
0
	bool Muzzleflasher::getMuzzleflash(IPointableObject *unit, VisualObject *muzzleflash, const std::string &name, const std::string &helper, VC3 &pos, VC3 &scale, QUAT &rot)
	{
		game::Unit *u = (game::Unit *)unit;

		VisualObject *vo = u->getVisualObject();
		if (vo == NULL || vo->getStormModel() == NULL)
		{
			return false;
		}

		IStorm3D_Model_Object *object = vo->getStormModel()->SearchObject(name.c_str());
		if(object == NULL)
		{
			return false;
		}
		
		pos = object->GetPosition();
		scale = object->GetScale();
		rot = object->GetRotation();
		return true;
	}
  void BuildingAdder::createVisualForBuilding(Game *game, Building *building)
  {
    // another quick haxor

    // maybe should check that the visual does not already exist

    //VisualObjectModel *vom = new VisualObjectModel(building->getModelFilename());
		VisualObjectModel *vom = game->visualObjectModelStorage->getVisualObjectModel(building->getModelFilename());

    VisualObject *vo = vom->getNewObjectInstance();
    building->setVisualObject(vo);
    vo->setInScene(true);
    vo->setDataObject(building);

		// disable collision to "firethrough" collision layer...
		IStorm3D_Model *model = vo->getStormModel();
		if(model)
		{
			const VC3 &sunDir = game->getGameUI()->getTerrain()->getSunDirection();
			model->SetDirectional(sunDir, 1.f);
		}

		Iterator<IStorm3D_Model_Object *> *objectIterator;
		for(objectIterator = model->ITObject->Begin(); !objectIterator->IsEnd(); objectIterator->Next())
		{
			IStorm3D_Model_Object *object = objectIterator->GetCurrent();

			const char *name = object->GetName();

			if (strstr(name, "FireThrough") != NULL)
			{
				object->SetNoCollision(true);
			}
		}

		delete objectIterator;

		// add building for self-illum (lightmap brightness) changes...
		game->gameUI->getLightManager()->getSelfIlluminationChanger()->addModel(vo->getStormModel());
  }
void ChangeToUncompressed(IStorm3D *storm3d, IStorm3D_Model *model)
{
	Iterator<IStorm3D_Model_Object *> *object_iterator;
	for(object_iterator = model->ITObject->Begin(); !object_iterator->IsEnd(); object_iterator->Next())
	{
		IStorm3D_Model_Object *object = object_iterator->GetCurrent();
		IStorm3D_Material *material = object->GetMesh()->GetMaterial();
		if(material == NULL)
			continue;

		IStorm3D_Texture *t = material->GetBaseTexture();
		if(t == NULL)
			continue;

		const char *fname = t->GetFilename();
		IStorm3D_Texture *nt = storm3d->CreateNewTexture(fname, TEXLOADFLAGS_NOCOMPRESS);

		t->Release();
		material->SetBaseTexture(nt);
		material->SetSelfIllumination(Color(1,1,1));
	}
	
	delete object_iterator;
}
Example #10
0
	VisualObject *VisualObjectModel::getNewObjectInstance()
	{
		if (visualStorm == NULL)
		{
			// must initialize first
			abort();
		}

		// DEBUG
		//Logger::getInstance()->error("Creating model:");
		//Logger::getInstance()->error(filename);
		//Timer::update();
		//int starttime = Timer::getTime();

		if (sharedModel == NULL)
		{
			Logger::getInstance()->debug("Creating a shared model.");
			Logger::getInstance()->debug(filename);

			sharedModel = visualStorm->CreateNewModel();
			if (filename != NULL)
			{
				// ubermagic...
				int slen = strlen(filename);
				bool hasObjectName = false;
				int objNamePart = 0;
				for (int i = 0; i < slen; i++)
				{
					if (filename[i] == ':')
					{
						hasObjectName = true;
						objNamePart = i;
						break;	
					}
				}

				// do some magic :)
				if (slen > 4 && strcmp(&filename[slen - 4], ".b3d") == 0)
				{
					// bones should not have object name.
					if (hasObjectName)
					{
						Logger::getInstance()->error("VisualObjectModel::getNewObjectInstance - Bonefilename cannot specify object.");
						return NULL;
					}
					if (!sharedModel->LoadBones(filename))
					{
						Logger::getInstance()->error("VisualObjectModel::getNewObjectInstance - Failed to load bones.");
						Logger::getInstance()->debug(filename);
					}
				} else {
					char real_fname[256];
					if (slen < 256)
					{
						strcpy(real_fname, filename);
					}
					if (hasObjectName)
					{
						if (objNamePart > 0)
							real_fname[objNamePart] = '\0';
						// else assert(0);
					}

					if (!sharedModel->LoadS3D(real_fname))
					{
						Logger::getInstance()->error("VisualObjectModel::getNewObjectInstance - Failed to load model.");
						Logger::getInstance()->debug(real_fname);
					}

					if (hasObjectName)
					{
						// delete all objects except the one we want...
						LinkedList objlist;
						Iterator<IStorm3D_Model_Object *> *object_iterator;
						for(object_iterator = sharedModel->ITObject->Begin(); !object_iterator->IsEnd(); object_iterator->Next())
						{
							IStorm3D_Model_Object *object = object_iterator->GetCurrent();
							objlist.append(object);
						}
						delete object_iterator;
						objlist.resetIterate();
						bool foundObject = false;
						while (objlist.iterateAvailable())
						{
							IStorm3D_Model_Object *object = (IStorm3D_Model_Object *)objlist.iterateNext();
							const char *objname = object->GetName();
							if (objname == NULL
								|| strcmp(&filename[objNamePart + 1], objname) != 0)
							{
								//delete object->GetMesh();
								sharedModel->Object_Delete(object);
							} else {
								if (objname != NULL)
									foundObject = true;
							}
						}
						if (!foundObject)
						{
							Logger::getInstance()->warning("VisualObjectModel::getNewObjectInstance - Model did not contain requested object.");
						}
					}

				}
			} else {
				Logger::getInstance()->debug("VisualObjectModel::getNewObjectInstance - Created empty visual object.");
			}
		}
		refCount++;

		assert(sharedModel != NULL);

		//IStorm3D_Model *model = visualStorm->CreateNewModel();
		IStorm3D_Model *model = NULL;

		// TODO: don't reload the model!! just copy the bones and stuff
		// from the shared model!!!

		// a quick hack method for getting helpers and stuff created...
		// just load the model again, and then clear the meshes...
		if (filename != NULL)
		{
			//Logger::getInstance()->error("Loading model.");
			// do some magic :)
			if (strlen(filename) > 4 
				&& strcmp(&filename[strlen(filename) - 4], ".b3d") == 0)
			{
				/*
				model = visualStorm->CreateNewModel();
				if (!model->LoadBones(filename))
				{
					Logger::getInstance()->error("VisualObjectModel::getNewObjectInstance - Failed to load bones.");
					Logger::getInstance()->debug(filename);
				}
				*/			
				model = sharedModel->GetClone(true, true, true);
			} else {
				//assert(0);
				// FIXME: ????????????????????????????????????????????
				/*
				model = visualStorm->CreateNewModel();
				model->LoadS3D(filename);
				*/
				model = sharedModel->GetClone(true, true, true);
			}
		} else {
			//Logger::getInstance()->debug("VisualObjectModel::getNewObjectInstance - Created empty visual object.");
			model = visualStorm->CreateNewModel();
		}

		assert(model != NULL);

		/*
		// delete loaded objects...
		LinkedList objlist;
		Iterator<IStorm3D_Model_Object *> *object_iterator;
		for(object_iterator = model->ITObject->Begin(); !object_iterator->IsEnd(); object_iterator->Next())
		{
			IStorm3D_Model_Object *object = object_iterator->GetCurrent();
			assert(object != NULL);
			objlist.append(object);
		}
		delete object_iterator;

		objlist.resetIterate();
		//while (objlist.iterateAvailable())
		while (!objlist.isEmpty())
		{
			//IStorm3D_Model_Object *object = (IStorm3D_Model_Object *)objlist.iterateNext();
			IStorm3D_Model_Object *object = (IStorm3D_Model_Object *)objlist.popLast();
			//delete object->GetMesh();
			model->Object_Delete(object);
		}

		// copy shared data to new model...
		Iterator<IStorm3D_Model_Object *> *del_object_iterator;
		for (del_object_iterator = sharedModel->ITObject->Begin(); !del_object_iterator->IsEnd(); del_object_iterator->Next())
		{
			IStorm3D_Model_Object *object = del_object_iterator->GetCurrent();
			IStorm3D_Mesh *mesh = object->GetMesh();
			if(mesh == NULL)
				continue;

			IStorm3D_Model_Object *objCopy = model->Object_New(object->GetName());
			objCopy->SetNoCollision(object->GetNoCollision());
			objCopy->SetNoRender(object->GetNoRender());
			objCopy->SetMesh(object->GetMesh());
			objCopy->SetPosition(object->GetPosition());
			objCopy->SetRotation(object->GetRotation());
			if(object->IsLightObject())
				objCopy->SetAsLightObject();
		} 		 
		delete del_object_iterator;
		*/

		VisualObject *ret = new VisualObject();
		ret->model = model;
		ret->animation = NULL;
		ret->storm3d = visualStorm;
		ret->scene = visualStormScene;
		ret->visualObjectModel = this;
		model->SetCustomData(ret);

		// DEBUG
		//Timer::update();
		//int timeelapsed = Timer::getTime() - starttime;
		//Logger::getInstance()->error("Time elapsed:");
		//Logger::getInstance()->error(int2str(timeelapsed));

		return ret;
	}
/*!
	\param modelId model ID
	\param position instance position
	\param rotation instance rotation
	\param color instance color
	\return index of added instance
*/
int Storm3D_TerrainGroup::addInstance(int modelId, const VC3 &position, const QUAT &rotation, const COL &color)
{
	assert(modelId >= 0 && modelId < int(data->models.size()));
	SharedModel &original = data->models[modelId];

	shared_ptr<IStorm3D_Model> m(original.model->GetClone(true, false, true));

	shared_ptr<IStorm3D_Model> mf;
	if(original.fadeModel)
		mf.reset(original.fadeModel->GetClone(true, false, true));

	if(!original.idleAnimation.empty() && !original.animation)
	{
		IStorm3D_BoneAnimation *ba = data->storm.CreateNewBoneAnimation(original.idleAnimation.c_str());
		if(ba)
			original.animation.reset(ba, ModelAnimationDeleter());
	}

	m->SetPosition(const_cast<VC3 &> (position));
	m->SetRotation(const_cast<QUAT &> (rotation));
	m->SetSelfIllumination(color);
#ifdef PHYSICS_NONE
	m->SetNoCollision(true);
#endif

	static_cast<Storm3D_Model *> (m.get())->terrain_object = true;

	if(mf)
	{
		mf->SetPosition(const_cast<VC3 &> (position));
		mf->SetRotation(const_cast<QUAT &> (rotation));
		mf->SetSelfIllumination(color);
		mf->SetNoCollision(true);
		static_cast<Storm3D_Model *> (mf.get())->terrain_object = true;

		boost::scoped_ptr<Iterator<IStorm3D_Model_Object *> > objectIterator(mf->ITObject->Begin());
		for(; !objectIterator->IsEnd(); objectIterator->Next())
		{
			IStorm3D_Model_Object *object = objectIterator->GetCurrent();
			if(!object)
				continue;

			object->SetForceAlpha(0);
		}

	}

	if(original.animation)
	{
		m->SetRandomAnimation(original.animation.get());
		if(mf)
			mf->SetRandomAnimation(original.animation.get());
	}

	data->terrainModels.addModel(*m);

	int index = original.instances.size();
	shared_ptr<Instance> instance(new Instance(m, mf, color));
	original.instances.push_back(instance);

	// ToDo:
	// Add a flag which controls whether to insert instance to collision tree.
	// A lot of objects don't care about collisions, anyway

	instance->radius2d = original.radius2d;
	instance->position = position;
	instance->rotation = rotation;
	instance->modelId = modelId;
	instance->instanceId = index;
	instance->entity = data->tree->insert(instance.get(), position, original.radius);

	// give the storm3d model some info about the terrain object instance/model ids
	static_cast<Storm3D_Model *> (m.get())->terrainInstanceId = index;
	static_cast<Storm3D_Model *> (m.get())->terrainModelId = modelId;
	// --jpk

	data->instanceMap[m.get()] = InstanceInfo(modelId, index);
	return index;
}
Example #12
0
int WINAPI WinMain(HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  // initialize...

  int scr_width = 0;
  int scr_height = 0;

	char *cmdline = lpCmdLine;

	std::string fileName = cmdline;
	//MessageBox(0, cmdline, "Command line", MB_OK);

	std::string parsedLine;
	if(strlen(cmdline) > 2)
	{
		char buffer[1024] = { 0 };
		GetModuleFileName(GetModuleHandle(0), buffer, 1023);
		std::string dir = buffer;
		int end = dir.find_last_of('\\');
		dir = dir.substr(0, end);

//#ifdef NDEBUG
		if (strstr(dir.c_str(), "source") != NULL)
		{
			MessageBox(0, "NOTE: Seem to be running under source dir, so not changing dir.", "Running from source.", MB_OK);
		} else {
			//MessageBox(0, dir.c_str(), "Changing dir to", MB_OK);
			SetCurrentDirectory(dir.c_str());
		}
//#endif

		{
			int start = 0;
			if (fileName[0] == '\"')
				start++;
			int end = fileName.find_last_of('\"');
			fileName = fileName.substr(start, end - start);
		}
	}

	using namespace frozenbyte::filesystem;
	boost::shared_ptr<IFilePackage> standardPackage(new StandardPackage());
	//boost::shared_ptr<IFilePackage> zipPackage(new ZipPackage("data.zip"));

	FilePackageManager &manager = FilePackageManager::getInstance();
	manager.addPackage(standardPackage, 0);
	//manager.addPackage(zipPackage, 2);

	parse_commandline(fileName.c_str());

	if (binFilename == NULL)
	{
		return 1;
	}

	//MessageBox(0, binFilename, "Bin", MB_OK);
	//MessageBox(0, modelFilename, "Model", MB_OK);

  Timer::init();

	IStorm3D *s3d = IStorm3D::Create_Storm3D_Interface(true, NULL);
	s3d->SetUserWindowMessageProc(&customMessageProc);

	s3d->SetApplicationName("BinEditor", "BinEditor");

  scr_width = 1024;
  scr_height = 768;

	s3d->SetWindowedMode(scr_width, scr_height);
	ShowWindow(s3d->GetRenderWindow(), SW_MAXIMIZE);

  // keyb3 controller devices
	int ctrlinit = 0;
	ctrlinit |= KEYB3_CAPS_MOUSE;
  ctrlinit |= KEYB3_CAPS_KEYBOARD;
  Keyb3_Init(s3d->GetRenderWindow(), ctrlinit);
  Keyb3_SetActive(1);

	float mouse_sensitivity = 1.0f;
  
  Storm3D_SurfaceInfo screenInfo = s3d->GetScreenSize();
  Keyb3_SetMouseBorders((int)(screenInfo.width / mouse_sensitivity), 
    (int)(screenInfo.height / mouse_sensitivity));
  Keyb3_SetMousePos((int)(screenInfo.width / mouse_sensitivity) / 2, 
    (int)(screenInfo.height / mouse_sensitivity) / 2);

	Keyb3_UpdateDevices();

  // make a scene
  COL bgCol = COL(0.0f, 0.0f, 0.0f);

  disposable_scene = s3d->CreateNewScene();
  disposable_scene->SetBackgroundColor(bgCol);

  disposable_scene->GetCamera()->SetVisibilityRange(100.0f);

  // create and initialize ogui
  Ogui *ogui = new Ogui();
  OguiStormDriver *ogdrv = new OguiStormDriver(s3d, disposable_scene);
  ogui->SetDriver(ogdrv);
  ogui->SetScale(OGUI_SCALE_MULTIPLIER * scr_width / 1024, 
    OGUI_SCALE_MULTIPLIER * scr_height / 768); 
  ogui->SetMouseSensitivity(mouse_sensitivity, mouse_sensitivity);
  ogui->Init();

  // set default font
#ifdef LEGACY_FILES
  ogui->LoadDefaultFont("Data/Fonts/default.ogf");
#else
  ogui->LoadDefaultFont("data/gui/font/common/default.ogf");
#endif

  // set default ui (ogui and storm) for visual objects (pictures and models)
  ui::createUIDefaults(ogui);

	LoadingMessage::setManagers(s3d, disposable_scene, ogui);

  // create cursors
  ogui->SetCursorController(0, OGUI_CURSOR_CTRL_MOUSE);

  // cursors images for controller 0,1,2,3
  loadDHCursors(ogui, 0); 

  ogui->SetCursorImageState(0, DH_CURSOR_ARROW);

  // do the loop...

  Timer::update();
  DWORD startTime = Timer::getTime(); 
  DWORD curTime = startTime;
  DWORD movementTime = startTime;
  DWORD frameCountTime = startTime;
  DWORD lastOguiUpdateTime = startTime;
  bool quitRequested = false;

  int frames = 0;
  int polys = 0;
  int fps = 0;
  int polysps = 0;
	int precount = 0;

	VC3 campos = VC3(0,0,0);

	Keyb3_UpdateDevices();

	IStorm3D_Model *model = NULL;

	if (modelFilename != NULL)
	{
	  model = s3d->CreateNewModel();
		model->LoadS3D(modelFilename);
		disposable_scene->AddModel(model);

		/*
    QUAT rot = QUAT(
      UNIT_ANGLE_TO_RAD(modelAngleX), 
      UNIT_ANGLE_TO_RAD(modelAngle), 
      UNIT_ANGLE_TO_RAD(modelAngleZ));
		*/
		QUAT qx;
		qx.MakeFromAngles(modelAngleX*3.1415f/180.0f, 0, 0);
		QUAT qy;
		qy.MakeFromAngles(0, modelAngle*3.1415f/180.0f, 0);
		QUAT qz;
		qz.MakeFromAngles(0, modelAngleZ*3.1415f/180.0f, 0);
		QUAT rot = qz * qx * qy;

    model->SetRotation(rot);

		// ----------------------------
		// HIDE ROOF...

		Iterator<IStorm3D_Model_Object *> *objectIterator;
		for(objectIterator = model->ITObject->Begin(); !objectIterator->IsEnd(); objectIterator->Next())
		{
			IStorm3D_Model_Object *object = objectIterator->GetCurrent();
			const char *name = object->GetName();

			int namelen = strlen(name);

			if(namelen < 12)
				continue;

			// Test name tag
			for(int i = 0; i < namelen - 12 + 1; ++i)
			{
				if (strncmp(&name[i], "BuildingRoof", 12) == 0)
				{
					object->SetNoRender(true);
				}
			}

		}

		delete objectIterator;

		// END OF HIDE ROOF
		// ----------------------------

	}

	/*
	IStorm3D_Line *gridlines[GRID_AREA_SIZE *2+1][GRID_AREA_SIZE *2+1][4] = { 0 };
	*/
	IStorm3D_Model *gridmodels[GRID_AREA_SIZE *2+1][GRID_AREA_SIZE *2+1][3] = { 0 };
	for (int i = 0; i < GRID_AREA_SIZE *2+1; i++)
	{
		for (int j = 0; j < GRID_AREA_SIZE *2+1; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				gridmodels[i][j][k] = s3d->CreateNewModel();
#ifdef LEGACY_FILES
				if (k < 1)
					gridmodels[i][j][k]->LoadS3D("Data/Models/Pointers/grid_green.s3d");
				else if (k == 2)
					gridmodels[i][j][k]->LoadS3D("Data/Models/Pointers/grid_red.s3d");
				else
					gridmodels[i][j][k]->LoadS3D("Data/Models/Pointers/grid_blue.s3d");
#else
				if (k < 1)
					gridmodels[i][j][k]->LoadS3D("data/model/pointer/grid_green.s3d");
				else if (k == 2)
					gridmodels[i][j][k]->LoadS3D("data/model/pointer/grid_red.s3d");
				else
					gridmodels[i][j][k]->LoadS3D("data/model/pointer/grid_blue.s3d");
#endif
				disposable_scene->AddModel(gridmodels[i][j][k]);
			}
		}
	}


	VC3 targ = campos;
	VC3 pos = targ - VC3(zoom/2,-zoom,0);
	disposable_scene->GetCamera()->SetPosition(pos);
	disposable_scene->GetCamera()->SetTarget(targ);

	unsigned short buffer[32*32] = { 0 };
	IStorm3D_Terrain *terrain = s3d->CreateNewTerrain(32);
	terrain->setHeightMap(buffer, VC2I(32,32), VC3(500,.01f,500), 4, 0, 1, 1);
	//terrain->getRenderer().setRenderMode(IStorm3D_TerrainRenderer::TexturesOnly);
	terrain->getRenderer().setFloatValue(IStorm3D_TerrainRenderer::ForceAmbient, 0.5f);
	disposable_scene->AddTerrain(terrain);
	disposable_scene->SetAmbientLight(COL(0.5f,0.5f,0.5f));

	frozenbyte::BuildingMap *bmap = NULL;

	if (binFilename != NULL
		&& modelFilename != NULL)
	{
		bmap = new frozenbyte::BuildingMap(modelFilename, model, modelAngleX, modelAngle, modelAngleZ);
	}

	if (bmap == NULL)
	{
		return 2;
	}

	float resolution = bmap->getMapResolution();
	const std::vector<std::vector<unsigned char> > &obstmap = bmap->getObstacleMap();
	const std::vector<std::vector<unsigned char> > &heightmap = bmap->getHeightMap();
	const std::vector<std::vector<char> > &floormap = bmap->getFloorHeightMap();

	int lastcammapx = -1;
	int lastcammapz = -1;

	bool wireframe = false;
	bool collision = false;

  while (!quitRequested)
  {
    // read input
    
    Keyb3_UpdateDevices();

		if (Keyb3_IsKeyPressed(KEYCODE_ESC))
		{
			quitRequested = true;
		}

    Timer::update();

    Timer::update();
    curTime = Timer::getTime();

    if (curTime - movementTime > 0)
    {
      // VEEERY jerky... 
      // attempt to fix that...
      float delta;
      delta = 100.0f;
      if (fps > 0) delta = 1000.0f/fps;
      if (delta < 1.0f) delta = 1.0f;
      if (delta > 100.0f) delta = 100.0f;
      float camera_time_factor = 1.0f;
      delta = (delta * camera_time_factor);

			float movespeed = 0.01f;
			if (Keyb3_IsKeyDown(KEYCODE_SHIFT_RIGHT)
				|| Keyb3_IsKeyDown(KEYCODE_SHIFT_LEFT))
				movespeed = 0.03f;

			IStorm3D_TerrainRenderer &renderer = terrain->getRenderer();
			if (Keyb3_IsKeyPressed(KEYCODE_1))
			{
				wireframe = !wireframe;
				if (wireframe)
				{
					renderer.enableFeature(IStorm3D_TerrainRenderer::Wireframe, true);
				} else {
					renderer.enableFeature(IStorm3D_TerrainRenderer::Wireframe, false);
				}
			}
			if (Keyb3_IsKeyPressed(KEYCODE_2))
			{
				collision = !collision;
				if (collision)
				{
					renderer.enableFeature(IStorm3D_TerrainRenderer::Collision, true);
				} else {
					renderer.enableFeature(IStorm3D_TerrainRenderer::Collision, false);
				}
			}

			if (Keyb3_IsKeyDown(KEYCODE_Q))
			{
				camrot -= 0.1f * delta;
			}
			if (Keyb3_IsKeyDown(KEYCODE_E))
			{
				camrot += 0.1f * delta;
			}
			if (camrot >= 360.0f) camrot -= 360.0f;
			if (camrot < 0.0f) camrot += 360.0f;

			if (Keyb3_IsKeyDown(KEYCODE_UP_ARROW)
				|| Keyb3_IsKeyDown(KEYCODE_W))
			{
				campos.x += (movespeed * delta)* cosf(camrot / 180.0f * 3.1415f);
				campos.z += (movespeed * delta)* -sinf(camrot / 180.0f * 3.1415f);
			}
			if (Keyb3_IsKeyDown(KEYCODE_DOWN_ARROW)
				|| Keyb3_IsKeyDown(KEYCODE_S))
			{
				campos.x -= (movespeed * delta)* cosf(camrot / 180.0f * 3.1415f);
				campos.z -= (movespeed * delta)* -sinf(camrot / 180.0f * 3.1415f);
			}
			if (Keyb3_IsKeyDown(KEYCODE_LEFT_ARROW)
				|| Keyb3_IsKeyDown(KEYCODE_A))
			{
				campos.z += (movespeed * delta)* cosf(camrot / 180.0f * 3.1415f);
				campos.x += (movespeed * delta)* sinf(camrot / 180.0f * 3.1415f);
			}
			if (Keyb3_IsKeyDown(KEYCODE_RIGHT_ARROW)
				|| Keyb3_IsKeyDown(KEYCODE_D))
			{
				campos.z -= (movespeed * delta)* cosf(camrot / 180.0f * 3.1415f);
				campos.x -= (movespeed * delta)* sinf(camrot / 180.0f * 3.1415f);
			}
			if (Keyb3_IsKeyDown(KEYCODE_KEYPAD_PLUS))
			{
				zoom -= movespeed * delta;
				if (zoom < 1.5f)
					zoom = 1.5f;
			}
			if (Keyb3_IsKeyDown(KEYCODE_KEYPAD_MINUS))
			{
				zoom += movespeed * delta;
				if (zoom > 80.0f)
					zoom = 80.0f;
			}
			if (Keyb3_IsKeyDown(KEYCODE_KEYPAD_DIVIDE))
			{
				camBeta -= movespeed*2 * delta;
				if (camBeta < -45.0f)
					camBeta = -45.0f;
			}
			if (Keyb3_IsKeyDown(KEYCODE_KEYPAD_MULTIPLY))
			{
				camBeta += movespeed*2 * delta;
				if (camBeta > 45.0f)
					camBeta = 45.0f;
			}

			VC3 targ = campos;
			float dirx = zoom*cosf((camBeta+45)/ 180.0f * 3.1415f) * cosf(camrot / 180.0f * 3.1415f);
			float dirz = zoom*cosf((camBeta+45)/ 180.0f * 3.1415f) * -sinf(camrot / 180.0f * 3.1415f);
			VC3 pos = targ - VC3(dirx,-zoom*cosf((camBeta-45)/ 180.0f * 3.1415f),dirz);

			disposable_scene->GetCamera()->SetPosition(pos);
			disposable_scene->GetCamera()->SetTarget(targ);

      movementTime = curTime;
    }

		// height/floor/obstacle visualization...
		
		int cammapx = int(campos.x / resolution);
		int cammapz = int(campos.z / resolution);

		if (cammapx != lastcammapx || cammapz != lastcammapz)
		{
			lastcammapx = cammapx;
			lastcammapz = cammapz;
			for (int mz = -GRID_AREA_SIZE; mz < GRID_AREA_SIZE+1; mz++)
			{
				for (int mx = -GRID_AREA_SIZE; mx < GRID_AREA_SIZE+1; mx++)
				{
					int tx = heightmap.size() / 2 + cammapx + mx;
					int tz = heightmap[0].size() / 2 + cammapz + mz;
					for (int i = 0; i < 3; i++)
					{
						gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetPosition(VC3(0,0,0));
						//gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetPosition(VC3(0,-2000,0));
						/*
						if (gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i] != NULL)
						{
							disposable_scene->RemoveLine(gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]);
							while (gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->GetPointCount() > 0)
							{
								gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->RemovePoint(0);
							}
							delete gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i];
							gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i] = NULL;
						}
						*/
					}
					if (tx >= 0 && tz >= 0
						&& tx < (int)heightmap.size() && tz < (int)heightmap[0].size())
					{
						for (int i = 0; i < 3; i++)
						{
							VC3 origin = VC3(cammapx*resolution,0,cammapz*resolution) + VC3(mx*resolution, 0, mz*resolution);

							if (floormap[tx][tz] != BUILDINGMAP_NO_FLOOR_BLOCK)
							{
								origin.y = (float)floormap[tx][tz] * bmap->getHeightScale() + 0.01f;
								if (!wireframe)
									origin.y += 0.05f;
							}

							if (i >= 2)
							{
								origin.y += (float)heightmap[tx][tz] * bmap->getHeightScale();
								origin.y += 0.02f;
								if (obstmap[tx][tz] == 0)
									origin = VC3(0,-2000,0);
							}
							//if (i == 1)
							//{
							//	gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetScale(VC3(0.5f,0.5f,0.5f));
							//}
							if (i == 1 && obstmap[tx][tz] == 0)
							{
								origin = VC3(0,-2000,0);
							}
							if (i == 0 && obstmap[tx][tz] != 0)
							{
								origin = VC3(0,-2000,0);
							}

							gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetPosition(origin);

							if (obstmap[tx][tz] == 2)
								gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetScale(VC3(0.8f,1,0.8f));
							else
								gridmodels[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i]->SetScale(VC3(1,1,1));


							/*
							IStorm3D_Line *lineObject = s3d->CreateNewLine();
							unsigned int alpha = 0x30000000;
							unsigned int color = 0x0000ff00;
							float thickness = 0.03f;

							if (obstmap[tx][tz] != 0)
								color = 0x00ff0000;

							if (i < 2 || obstmap[tx][tz] != 0)
							{
								lineObject->SetThickness(thickness);
								lineObject->SetColor(color | alpha);

								VC3 origin = campos + VC3(mx*resolution, 0, mz*resolution);

								origin.y = floormap[tx][tz] * bmap->getHeightScale() + 0.001f;

								if (i >= 2)
									origin.y += (float)heightmap[tx][tz] * bmap->getHeightScale();

								lineObject->AddPoint(origin);
								VC3 line_endpoint;
								if ((i % 2) == 0)
									line_endpoint = origin + VC3(resolution,0,0);
								else
									line_endpoint = origin + VC3(0,0,resolution);
								lineObject->AddPoint(line_endpoint);
								disposable_scene->AddLine(lineObject, true);

								gridlines[GRID_AREA_SIZE+mx][GRID_AREA_SIZE+mz][i] = lineObject;
							}
							*/
						}
					}
				}
			}
		}

    // frame/poly counting
    frames++;
    {
      if (curTime - frameCountTime >= 200) 
      {
       float seconds = (curTime - frameCountTime) / 1000.0f;
       fps = (int)(frames / seconds);
       polysps = (int)(polys / seconds);
       frameCountTime = curTime;
       frames = 0;
       polys = 0;
      }
    }

    // run the gui
		int oguiTimeDelta = curTime - lastOguiUpdateTime;
		if (oguiTimeDelta > 200)
			oguiTimeDelta = 200;

    ogui->Run(oguiTimeDelta);

		lastOguiUpdateTime = curTime;

    // render stats

		bool show_polys = true;
		bool show_fps = true;
		// WARNING: unsafe cast!
		IStorm3D_Font *fpsFont = ((OguiStormFont *)ui::defaultIngameFont)->fnt;
    if (show_polys || show_fps)
    {
      float polyoffset = 0;
      float terroffset = 0;
      char polytextbuf[40];
      char polyframetextbuf[40];
      char fpstextbuf[40];
      if (show_fps)
      {
        polyoffset = 16;
				terroffset = 16;
        sprintf(fpstextbuf, "FPS:%d", fps);
        disposable_scene->Render2D_Text(fpsFont, VC2(0,0), VC2(16, 16), fpstextbuf);
      }
      if (show_polys)
      {
				terroffset += 32;
        sprintf(polytextbuf, "POLYS PER S:%d", polysps);
        int polyspf = 0;
        if (fps > 0) polyspf = polysps / fps;
        sprintf(polyframetextbuf, "POLYS PER F:%d", polyspf);
        disposable_scene->Render2D_Text(fpsFont, VC2(0,polyoffset), VC2(16, 16), polytextbuf);
        disposable_scene->Render2D_Text(fpsFont, VC2(0,polyoffset+16), VC2(16, 16), polyframetextbuf);
      }
    }

		int tx = heightmap.size() / 2 + cammapx;
		int tz = heightmap[0].size() / 2 + cammapz;
    char coordtextbuf[64];
    sprintf(coordtextbuf, "%d,%d", tx,tz);
    disposable_scene->Render2D_Text(fpsFont, VC2(0,768-16), VC2(16, 16), coordtextbuf);

    // Render scene
    polys += renderfunc(disposable_scene);

		//DebugDataView::getInstance(game)->run();

		// wait here if minimized...
		while (true)
		{
			// psd -- handle windows messages
			MSG msg = { 0 };
			while(PeekMessage(&msg, s3d->GetRenderWindow(), 0, 0, PM_NOREMOVE))
			{
				if(GetMessage(&msg, s3d->GetRenderWindow(), 0, 0) <= 0)
				{
					quitRequested = true;
				}
				else
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
			}
			if (lostFocusPause)
			{
				Sleep(500);
			} else {
				break;
			}
		}
  }

	if (bmap != NULL)
	{
		delete bmap;
		bmap = NULL;
	}

  // clean up

  unloadDHCursors(ogui, 0); 

  deleteUIDefaults();

  ogui->Uninit();
  delete ogui;
  delete ogdrv;

  Keyb3_Free();
  
  delete s3d;

	SystemRandom::cleanInstance();

	uninitLinkedListNodePool();

  Timer::uninit();

	Logger::cleanInstance();

  return 0;
}