Ejemplo n.º 1
0
void HexMapTest::SetModelMatrix() {
	s_ModelMatrix.SetIdentity();
	CIwVec3 vectCenter = getWorldCoords(IwGxGetScreenWidth()/2, IwGxGetScreenHeight()/2);
	s_ModelMatrix.t.x = vectCenter.x;
	s_ModelMatrix.t.y = vectCenter.y;

	//Zoom
	s_ModelMatrix.Scale(IW_FIXED_FROM_FLOAT(zoom));
	CIwVec3 vect = getWorldCoords(0, 0);
	CIwVec3 vect2 = getWorldCoords(screenTranslationX, screenTranslationY);

	int32 dx = int32((vect2.x-vect.x) * zoom);
	int32 dy = int32((vect2.y-vect.y) * zoom);

	//s_ModelMatrix.t.x = -dx;
	//s_ModelMatrix.t.y = -dy;

	iwfixed sinTheta = IwGeomSin(IW_ANGLE_FROM_DEGREES(-rotation));
	iwfixed cosTheta = IwGeomCos(IW_ANGLE_FROM_DEGREES(-rotation));

	s_ModelMatrix.t.x = -vectCenter.x-(IW_FIXED_MUL(dx, cosTheta) - IW_FIXED_MUL(dy, sinTheta));
	s_ModelMatrix.t.y = -vectCenter.y-(IW_FIXED_MUL(dx, sinTheta) + IW_FIXED_MUL(dy, cosTheta));
	//Rotate
	CIwMat rotationMat = CIwMat::g_Identity;
	rotationMat.SetRotZ(IW_ANGLE_FROM_DEGREES(rotation), true, true);
	s_ModelMatrix *= rotationMat;

}
Ejemplo n.º 2
0
void GameStateManager::handlePlayerMove(int32_t eid)
{
  const PlayerState & player = *m_states[eid];

  sendRawToAllExceptOne(rawPacketSCEntityTeleport(eid, getFractionalCoords(player.position), player.yaw, player.pitch), eid);

  auto interesting_blocks = m_map.blockAlerts().equal_range(getWorldCoords(player.position));

  for (auto it = interesting_blocks.first; it != interesting_blocks.second; ++it)
  {
    if (it->second.type == Map::BlockAlert::CONTAINS_SPAWN_ITEM)
    {
      const auto jt = m_map.items().find(it->second.data);
      if (jt != m_map.items().end())
      {
        sendToAll(MAKE_CALLBACK(packetSCCollectItem, jt->first, eid));
        sendToAll(MAKE_CALLBACK(packetSCDestroyEntity, jt->first));

        // Add 1 undamaged unit to the player's inventory.
        updatePlayerInventory(eid, jt->second, 1, 0);

        m_map.items().erase(jt);
      }
    }
  }
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------
void HexMapTest::Render()
{
//	if (mouse_mode == MOUSE_MODE_CHECKING) 
	{
//		int32 px = IwGxGetScreenWidth() - s3ePointerGetX();
//		int32 py = IwGxGetScreenHeight() - s3ePointerGetY();
		int32 closestX = -1, closestY = -1;

		CIwVec3 vect = getWorldCoords(s3ePointerGetX(), s3ePointerGetY());
//		hexGrid->findClosestArray(origin, dir, closestX, closestY);
		//WORKING!!!
		hexGrid->findClosestSimple(vect.x, vect.y, closestX, closestY);
		DebugPrint(closestX, closestY);
	}
//		s_PickSurface->MakeCurrent();
    // Clear the screen
    IwGxClear(IW_GX_COLOUR_BUFFER_F | IW_GX_DEPTH_BUFFER_F);
	// Set the model matrix
    IwGxSetModelMatrix(&s_ModelMatrix);
	hexGrid->render();


    // End drawing
    IwGxFlush();

	IwGxPrintSetScale(2);
	IwGxPrintFrameRate(0, 0); 
	// Swap buffers
	IwGxSwapBuffers();
}
Ejemplo n.º 4
0
int Terrain::createMapObj(int x, int y, std::string meshname, int dir)
{
	stTileEntityData* entitydata = new stTileEntityData;
	entitydata->mTileEntity = Core::getSingleton().mSceneMgr->createEntity(meshname);
	entitydata->mTileNode = mTerrainNode->createChildSceneNode();
	entitydata->mTileNode->attachObject(entitydata->mTileEntity);
	float xx,yy;
	getWorldCoords(x,y,xx,yy);
	entitydata->mTileNode->setPosition(xx ,getHeight(xx,yy),yy);

	Ogre::Quaternion q;
	switch(dir)
	{
	case North:
		q.FromAngleAxis(Ogre::Degree(180),Ogre::Vector3(0,1,0));
		break;
	case South:
		q.FromAngleAxis(Ogre::Degree(360),Ogre::Vector3(0,1,0));
		break;
	case West:
		q.FromAngleAxis(Ogre::Degree(270),Ogre::Vector3(0,1,0));
		break;
	case East:
		q.FromAngleAxis(Ogre::Degree(90),Ogre::Vector3(0,1,0));
		break;
	}
	entitydata->mTileNode->setOrientation(q);
	mObjId ++;
	mMapObjMap.insert(std::map<int, stTileEntityData*>::value_type(mObjId,entitydata ));
	return mObjId;
}
osg::Vec3d GPSSensor::getMeasurement() {
	//Should get world coords and then transform to the localizedWorld
	osg::Matrixd *rMs=getWorldCoords(node_);
	osg::Matrixd lMs=*rMs*osg::Matrixd::inverse(rMl_);

	//Now add some gaussian noise
	static boost::normal_distribution<> normal(0,std_);
	static boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor(rng_, normal);

	return lMs.getTrans()+osg::Vec3d(var_nor(), var_nor(), var_nor());
}
Ejemplo n.º 6
0
void Mouse::OnHardwareMouseEvent( int iX,int iY,EHARDWAREMOUSEEVENT eHardwareMouseEvent, int wheelDelta )
{	
	m_x = iX;
	m_y = iY;
	
	if(eHardwareMouseEvent == HARDWAREMOUSEEVENT_LBUTTONDOWN)
	{
		CryLogAlways("LEFT BUTTON PRESSED");
		CryLogAlways("requesting movement...");

		CPlayer* pPlayer = CPlayer::GetHero();
		Vec3 pos;
		getWorldCoords(pos.x, pos.y, pos.z);		
		m_clickFeedback.Start(pos);
		CMovementRequest request;
		request.SetMoveTarget(pos);		
		pPlayer->GetMovementController()->RequestMovement(request);
		setCursor();
	}
	if (eHardwareMouseEvent == HARDWAREMOUSEEVENT_LBUTTONUP)
	{

	}

	if(eHardwareMouseEvent == HARDWAREMOUSEEVENT_RBUTTONDOWN)
	{
		CryLogAlways("RIGHT BUTTON PRESSED");
		getEntityUnderCursor();
	}
	if(eHardwareMouseEvent == HARDWAREMOUSEEVENT_RBUTTONUP)
	{
		CryLogAlways("RIGHT BUTTON RELEASED");
	}

	if (eHardwareMouseEvent == HARDWAREMOUSEEVENT_WHEEL)
	{
		CryLogAlways("WHEEL MOVED");
		m_wheelDelta -= static_cast<float>(wheelDelta) * gEnv->pTimer->GetFrameTime();
		if (m_wheelDelta > 0)
			m_wheelDelta = 0;
		else if (m_wheelDelta < -10)
			m_wheelDelta = -10;

		getEntityUnderCursor();
	}
}
Ejemplo n.º 7
0
void Terrain::createGrid()
{
	//float startpos = - mMapData->getMapSize() * TILESIZE / 2.0f  -TILESIZE;//公式有错
	float startpos;
	getWorldCoords(0,0,startpos,startpos);
	startpos -= TILESIZE /2.0f;
	mGridNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("GridNode");
	mGrid = Core::getSingleton().mSceneMgr->createManualObject("GridObject");
	mGrid->begin("GridMat",Ogre::RenderOperation::OT_LINE_LIST);
	for(int y = 0; y < mMapData->getMapSize(); y++)
		for(int x = 0; x < mMapData->getMapSize(); x++)
		{
			if(mMapData->getPassable(x,y,-1))
			{
				float xx = startpos + x * TILESIZE;
				float yy = startpos + y * TILESIZE;
				if(!mMapData->getPassable(x ,y -1,-1))
				{
					mGrid->position(xx,getHeight(xx,yy),yy);
					mGrid->colour(1.0f,1.0f,1.0f);
					mGrid->position(xx+TILESIZE,getHeight(xx+TILESIZE,yy),yy);
					mGrid->colour(1.0f,1.0f,1.0f);
				}
				if(!mMapData->getPassable(x - 1,y,-1))
				{
					mGrid->position(xx,getHeight(xx,yy+TILESIZE),yy+TILESIZE);
					mGrid->colour(1.0f,1.0f,1.0f);
					mGrid->position(xx,getHeight(xx,yy),yy);
					mGrid->colour(1.0f,1.0f,1.0f);
				}
				mGrid->position(xx+TILESIZE,getHeight(xx+TILESIZE,yy),yy);
				mGrid->colour(1.0f,1.0f,1.0f);
				mGrid->position(xx+TILESIZE,getHeight(xx+TILESIZE,yy+TILESIZE),yy+TILESIZE);
				mGrid->colour(1.0f,1.0f,1.0f);
				mGrid->position(xx,getHeight(xx,yy+TILESIZE),yy+TILESIZE);
				mGrid->colour(1.0f,1.0f,1.0f);
				mGrid->position(xx+TILESIZE,getHeight(xx+TILESIZE,yy+TILESIZE),yy+TILESIZE);
				mGrid->colour(1.0f,1.0f,1.0f);
			}
		}
		mGrid->end();
		mGrid->setQueryFlags(QUERYMASK_GRID);
		mGrid->setVisibilityFlags(VISMASK_TRANSPARENT);
		mGridNode->attachObject(mGrid);
		mGridNode->setPosition(0,PLANEHEIGHT+ 0.5f,0);
}
  virtual void setWorldTransform(const btTransform &worldTrans){
    //Object initial position
    osg::Matrixd * mat= getWorldCoords(transf->getParent(0));

    //Get object position in matrixd
    osg::Matrixd worldMatrix;
    btQuaternion rot = worldTrans.getRotation();
    btVector3 pos = worldTrans.getOrigin();
    worldMatrix.setTrans(osg::Vec3d(pos.x(),pos.y(),pos.z()));
    worldMatrix.setRotate(osg::Quat(rot.x(),rot.y(),rot.z(),rot.w()));
    
    //matrix transform from object initial position to final position
    osg::Matrixd rootmat=worldMatrix*(mat->inverse(*mat));

    //Apply transform to object matrix
    rootmat.setTrans(rootmat.getTrans());
    rootmat.setRotate(rootmat.getRotate());
    transf->setMatrix(rootmat);

  }
Ejemplo n.º 9
0
int Terrain::createMapParticle(int x, int y,std::string particlename)
{
	stTilePUData* pudata = new stTilePUData;
	pudata->mTileNode = mTerrainNode->createChildSceneNode();
	pudata->mTileParticle = Core::getSingleton().createPUSystem(pudata->mTileNode->getName() + "_PU", particlename);
	pudata->mTileNode->attachObject(pudata->mTileParticle);
	pudata->mTileParticle->prepare();
	pudata->mTileParticle->start();
	float xx,yy;
	getWorldCoords(x,y,xx,yy);
	pudata->mTileNode->setPosition(xx ,getHeight(xx,yy),yy);
	mPUId ++;
	mMapPUMap.insert(std::map<int, stTilePUData*>::value_type(mPUId,pudata ));

	std::string datapath = "GameData/BattleData/MapData/MapParticleInfo";
	DataLibrary::getSingletonPtr()->getData(datapath + std::string("/MP") + Ogre::StringConverter::toString(mPUId) + std::string("/Script"),particlename);
	DataLibrary::getSingletonPtr()->getData(datapath + std::string("/MP") + Ogre::StringConverter::toString(mPUId) + std::string("/GridX"),x);
	DataLibrary::getSingletonPtr()->getData(datapath + std::string("/MP") + Ogre::StringConverter::toString(mPUId) + std::string("/GridY"),y);

	return mPUId;
}
Ejemplo n.º 10
0
void GameStateManager::sendMoreChunksToPlayer(int32_t eid)
{
  std::lock_guard<std::recursive_mutex> lock(m_gs_mutex);

  auto it = m_states.find(eid);
  if (it == m_states.end()) return;

  PlayerState & player = *it->second;

  // Someone can go and implement more overloads if this looks too icky.
  const ChunkCoords pc = getChunkCoords(getWorldCoords(getFractionalCoords(player.position)));

  std::vector<ChunkCoords> ac = ambientChunks(pc, PLAYER_CHUNK_HORIZON);
  std::sort(ac.begin(), ac.end(), L1DistanceFrom(pc));

  /// Here follows the typical chunk update acrobatics in three rounds.

  // Round 1: Load all relevant chunks to memory

  for (auto i = ac.cbegin(); i != ac.cend(); ++i)
  {
    if (player.known_chunks.count(*i) > 0) continue;

    //std::cout << "Player #" << std::dec << eid << " needs chunk " << *i << "." << std::endl;
    m_map.ensureChunkIsReadyForImmediateUse(*i);
  }

  // Round 2: Spread light to all chunks in memory. Light only spreads to loaded chunks.

  for (auto i = ac.cbegin(); i != ac.cend(); ++i)
  {
    if (player.known_chunks.count(*i) > 0) continue;

    m_map.chunk(*i).spreadAllLight(m_map);
    m_map.chunk(*i).spreadToNewNeighbours(m_map);
  }

  // Round 3: Send the fully updated chunks to the client.
  // 3a: Prechunks
  for (auto i = ac.cbegin(); i != ac.cend(); ++i)
  {
    if (player.known_chunks.count(*i) > 0) continue;

    Chunk & chunk = m_map.chunk(*i);
    packetSCPreChunk(eid, *i, true);
  }
  // 3b: Actual chunks
  for (auto i = ac.cbegin(); i != ac.cend(); ++i)
  {
    if (player.known_chunks.count(*i) > 0) continue;

    Chunk & chunk = m_map.chunk(*i);

    // Not sure if the client has a problem with data coming in too fast...
    //sleepMilli(5);

#define USE_ZCACHE 0   // The local chache doesn't seem to work reliably.
#if USE_ZCACHE > 0
    // This is using a chunk-local zip cache.
    std::pair<const unsigned char *, size_t> p = chunk.compress_beefedup();

    if (p.second > 18)
      packetSCMapChunk(eid, p);
    else
      packetSCMapChunk(eid, *i, chunk.compress());
#else
    // This is the safe way.
    packetSCMapChunk(eid, *i, chunk.compress());
#endif
#undef USE_ZCACHE

    player.known_chunks.insert(*i);
  }
}
Ejemplo n.º 11
0
double GPSSensor::depthBelowWater() {
	osg::Matrixd *rMs=getWorldCoords(node_);
	return -(rMs->getTrans().z()-oscene_->getOceanScene()->getOceanSurfaceHeight());
}
 virtual void getWorldTransform(btTransform &worldTrans) const {
       worldTrans = osgbCollision::asBtTransform(*getWorldCoords(object));
 }
Ejemplo n.º 13
0
//-----------------------------------------------------------------------------
bool HexMapTest::Update()
{
	int16 sprite1_pos_x,sprite1_pos_y;
	int16 sprite2_pos_x,sprite2_pos_y;
	if (mouse_mode == MOUSE_MODE_CHECKING)
		mouse_mode  = MOUSE_MODE_DOWN;

	// UI processing
	if (((s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_PRESSED)) && mouse_mode == MOUSE_MODE_IDLE)
		mouse_mode = MOUSE_MODE_IDLE;
	if ((s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_RELEASED))
		mouse_mode = MOUSE_MODE_CHECKING;

//	UpdateKey();

	IwGxSetViewMatrix(&s_viewMatrix);

    // Generate a ray pointing to the view plane from the camera
    CIwVec3 dir(s3ePointerGetX() - IwGxGetScreenWidth()/2, 
        s3ePointerGetY() - IwGxGetScreenHeight()/2, 
        IwGxGetPerspMul());
    
    // Rotate into camera space
    dir = s_viewMatrix.RotateVec(dir);

	// Update pointer system
	g_Input.Update();
	if (g_Input.finger1IsDown()) {
		if (g_Input.overThreshold()) {
			if (!g_Input.finger1Continuing()) {
				s_ModelMatrix_initial = s_ModelMatrix;
				CIwVec3 vectCenter = getWorldCoords(0, 0);
				zoom_initial = zoom;
				rotation_initial = rotation;
				screenTranslationX_initial = screenTranslationX;
				screenTranslationY_initial = screenTranslationY;
				if (!g_Input.isMultiTouch()) {
					if (g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) {
						if (sprite1_pos_x > int(IwGxGetScreenWidth()) - SCREEN_MARGIN) {
							zooming = true;
						}
						if (sprite1_pos_y > int(IwGxGetScreenHeight()) - SCREEN_MARGIN) {
							rotating = true;
						}
					}
				}
			}
			if (!g_Input.finger2IsDown() && g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) {
				if (g_Input.finger1Continuing()) {
					if (zooming) {
						if (sprite1_pos_x > int(IwGxGetScreenWidth()) - SCREEN_MARGIN) {
							int16 dsprite1_pos_x, dsprite1_pos_y;
							if (g_Input.finger1MovementDelta(dsprite1_pos_x, dsprite1_pos_y)) {
								float deltaZoom = 1.0f - 1.0f*(dsprite1_pos_y)/IwGxGetScreenHeight();
								SetZoom(deltaZoom, 8);
							}
						}
					} else if (rotating) {
						if (sprite1_pos_y > int(IwGxGetScreenHeight()) - SCREEN_MARGIN) {
							int16 dsprite1_pos_x, dsprite1_pos_y;
							if (g_Input.finger1MovementDelta(dsprite1_pos_x, dsprite1_pos_y)) {
								float deltaRotation = 360.0f*(dsprite1_pos_x)/IwGxGetScreenWidth();
								SetRotation(deltaRotation);
							}
						}
					} else {
						SetTranslation();
					}
				}
			} else {
				if (g_Input.finger1MovedTo(sprite1_pos_x, sprite1_pos_y)) {
					if (g_Input.finger2MovedTo(sprite2_pos_x, sprite2_pos_y)) {
						if (!g_Input.finger2Continuing()) {
							screenTranslationX_initial = screenTranslationX;
							screenTranslationY_initial = screenTranslationY;
							g_Input.resetInitial(0);
						} else {
							int16 sprite1_pos_x_initial,sprite1_pos_y_initial,sprite2_pos_x_initial,sprite2_pos_y_initial;
							g_Input.finger1Initial(sprite1_pos_x_initial,sprite1_pos_y_initial);
							g_Input.finger2Initial(sprite2_pos_x_initial,sprite2_pos_y_initial);
							int d12x=sprite1_pos_x-sprite2_pos_x;
							int d12y=sprite1_pos_y-sprite2_pos_y;
							int d12x_initial=sprite1_pos_x_initial-sprite2_pos_x_initial;
							int d12y_initial=sprite1_pos_y_initial-sprite2_pos_y_initial;
							int Delta_initial = d12x_initial*d12x_initial+d12y_initial*d12y_initial;
							int Delta = d12x*d12x+d12y*d12y;
							float newZoom = float(sqrt(1.0*Delta)/sqrt(Delta_initial));
							SetZoom(newZoom, 1);
							float oldRotation = float(atan2(float(d12y_initial), float(d12x_initial)));
							float newRotation = float(atan2(float(d12y), float(d12x)));
							SetRotation(180.0f*(oldRotation-newRotation)/PI);
							{
								char string[256];
								//sprintf(string, "`1`a  del %04d, %04d",d1x,d2x);
								//IwGxPrintString(2, 96, string);
								sprintf(string, "`1`a  zoo %5.2f, %5.2f, %5.2f",newZoom,oldRotation,newRotation);
								IwGxPrintString(2, 96, string);
							}
						}
					}
				}
			}
		}
	} else {
		zooming = false;
		rotating = false;
		//		s_ModelMatrix = CIwMat::g_Identity;
	}
	return true;
}
Ejemplo n.º 14
0
osg::Vec3 OSGObjectBase::getWorldPosition()
{
    return getWorldCoords()->getTrans();
}
Ejemplo n.º 15
0
bool Terrain::createTerrain()
{

	if(mMainViewport == NULL) 
		mMainViewport = Core::getSingleton().mCamera->getViewport();
	Ogre::CompositorManager::getSingleton().addCompositor(mMainViewport, "DemoCompositor");
	Ogre::CompositorManager::getSingleton().setCompositorEnabled(mMainViewport, "DemoCompositor", true);

	mMapData = MapDataManager::getSingletonPtr();
	DataLibrary* datalib = DataLibrary::getSingletonPtr();
	int terrainszie = mMapData->getMapSize() + 2 * MAPBOLDER + 1;

	Core::getSingleton().mSceneMgr->setSkyBox(true, "SkyBox",200);

	Ogre::GpuSharedParametersPtr sharedparams = Ogre::GpuProgramManager::getSingleton().getSharedParameters("TestSharedParamsName");
	float border = mMapData->getMapSize() * 12.0f;
	sharedparams->setNamedConstant("border", border);

	//创建灯光
	Core::getSingleton().mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
	mLight = Core::getSingleton().mSceneMgr->createLight("TerrainLight");
	mLight->setType(Ogre::Light::LT_DIRECTIONAL);
	mLight->setPosition(-500.0f,500.0f, 500.0f);
	mLight->setDirection(1.0f, -1.0f, -1.0f);
	mLight->setDiffuseColour(Ogre::ColourValue(0.5f, 0.5f,0.5f));
	mLight->setSpecularColour(Ogre::ColourValue(0.8f, 0.8f,0.8f));

	//设置深度图投影
	Ogre::TexturePtr tex = Ogre::TextureManager::getSingleton().getByName("shadowdepthmap");
	if(tex.isNull())
		tex = Ogre::TextureManager::getSingleton().createManual("shadowdepthmap",
			Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 2048, 2048, 0, Ogre::PF_FLOAT16_R, Ogre::TU_RENDERTARGET);
	mShadowDepthMapTarget = tex->getBuffer()->getRenderTarget();
	Ogre::Viewport* vp = mShadowDepthMapTarget->addViewport(CameraContral::getSingleton().getShadowMapCamera());
	vp->setSkiesEnabled(false);
	vp->setOverlaysEnabled(false);
	vp->setVisibilityMask(VISMASK_OPAQUE);
	vp->setMaterialScheme("WriteDepthMap");
	vp->setBackgroundColour(Ogre::ColourValue(1.0f,1.0f,1.0f));
	mShadowDepthMapTarget->addListener(this);
	//弱爆了……
	Ogre::MaterialPtr mat;
	mat = Ogre::MaterialManager::getSingleton().getByName("TerrainTile");
	Ogre::AliasTextureNamePairList texAliasList;
	std::string texname;
	datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse1",texname));
	datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse2",texname));
	datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse3",texname));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat1");
	datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat2");
	datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat3");
	datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat4");
	datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("BankMat1");
	datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("BankMat2");
	datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("BankMat3");
	datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	mat = Ogre::MaterialManager::getSingleton().getByName("BankMat4");
	datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname);
	texAliasList.insert(std::make_pair("Diffuse",texname));
	texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga"));
	mat->applyTextureAliases(texAliasList);
	texAliasList.clear();

	//创建地面Mesh
	mTerrainNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("TerrainNode");

	int numVertices = terrainszie * terrainszie * VERTEX_QUAD;
	int numIndex = terrainszie * terrainszie * VERTEX_PREQUAD;
	Ogre::MeshPtr mTerrainMesh = Ogre::MeshManager::getSingleton().createManual("TerrianMesh",
		Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	Ogre::SubMesh* subMesh  = mTerrainMesh->createSubMesh();
	subMesh->useSharedVertices=false;
	subMesh->setMaterialName("TerrainTile");

	// 创建顶点数据结构
	subMesh->vertexData = new Ogre::VertexData();
	subMesh->vertexData->vertexStart = 0;
	subMesh->vertexData->vertexCount = numVertices;

	//顶点声明与缓冲区绑定
	Ogre::VertexDeclaration* vdecl = subMesh->vertexData->vertexDeclaration;
	Ogre::VertexBufferBinding* vbind = subMesh->vertexData->vertexBufferBinding;

	//设置顶点数据结构
	size_t offsetUV = 0;
	vdecl->addElement(VERTEX_POS_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_POSITION);//向顶点添加一个位置元素
	vdecl->addElement(VERTEX_NOM_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_NORMAL);
	for(int i = 0 ; i < TEXTURE_COUNT ; i ++)
	{
		offsetUV += vdecl->addElement (VERTEX_UV_BINDING, offsetUV, Ogre::VET_FLOAT2,  Ogre::VES_TEXTURE_COORDINATES , i).getSize();
	}

	// 创建世界坐标顶点缓冲区
	Ogre::HardwareVertexBufferSharedPtr vbufPos =
		Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
		vdecl->getVertexSize(VERTEX_POS_BINDING),
		numVertices,
		Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
	vbind->setBinding(VERTEX_POS_BINDING, vbufPos);

	Ogre::HardwareVertexBufferSharedPtr vbufNOM =
		Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
		vdecl->getVertexSize(VERTEX_NOM_BINDING),
		numVertices,
		Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
	vbind->setBinding(VERTEX_NOM_BINDING, vbufNOM);

	// 创建纹理坐标顶点缓冲区
	Ogre::HardwareVertexBufferSharedPtr vbufUV =
		Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
		vdecl->getVertexSize(VERTEX_UV_BINDING),
		numVertices,
		Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
	vbind->setBinding(VERTEX_UV_BINDING, vbufUV);

	// 创建索引缓冲区
	Ogre::HardwareIndexBufferSharedPtr indexBuffer =
		Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(
		Ogre::HardwareIndexBuffer::IT_16BIT ,
		numIndex,
		Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);

	//创建地形
	float* pBufferPos = (float*)vbufPos->lock(Ogre::HardwareBuffer::HBL_DISCARD);
	float* pBufferUV = (float*)vbufUV->lock(Ogre::HardwareBuffer::HBL_DISCARD);
	float* pBufferNom = (float*)vbufNOM->lock(Ogre::HardwareBuffer::HBL_DISCARD);

	float startpos = - terrainszie * TILESIZE / 2;
	for(int y = 0 ; y < terrainszie; y ++)
	{
		for(int x = 0 ; x < terrainszie; x ++)
		{
			createTile(x, y, startpos + x * TILESIZE, startpos + y * TILESIZE, pBufferPos, pBufferUV, pBufferNom);
			pBufferPos += 3 * VERTEX_QUAD ;
			pBufferNom += 3 * VERTEX_QUAD ;
			pBufferUV += 2 * VERTEX_QUAD * 4;
		}
	}

	vbufNOM->unlock();
	vbufUV->unlock();
	vbufPos->unlock();

	//写入索引信息
	// 锁定索引缓冲区
	Ogre::ushort* pIdx = (Ogre::ushort*)indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
	for(int y = 0 ; y < terrainszie ; y ++)
	{
		for(int x = 0 ; x < terrainszie ; x ++)
		{
			Ogre::ushort iIndexTopLeft = (x + y * terrainszie) * VERTEX_QUAD;
			Ogre::ushort iIndexTopRight = iIndexTopLeft + 1;
			Ogre::ushort iIndexBottomLeft = iIndexTopLeft + 2;
			Ogre::ushort iIndexBottomRight = iIndexTopLeft + 3;
			*pIdx++ = iIndexBottomLeft;
			*pIdx++ = iIndexBottomRight;
			*pIdx++ = iIndexTopLeft;

			*pIdx++ = iIndexBottomRight;
			*pIdx++ = iIndexTopRight;
			*pIdx++ = iIndexTopLeft;
		}
	}
	indexBuffer->unlock();
	//设置模型的的索引数据
	subMesh->indexData->indexBuffer = indexBuffer;
	subMesh->indexData->indexStart = 0;
	subMesh->indexData->indexCount =numIndex;

	Ogre::AxisAlignedBox meshBounds(startpos,0,startpos,
		-startpos,5,-startpos);
	mTerrainMesh->_setBounds(meshBounds);

	mTerrainEntity = Core::getSingleton().mSceneMgr->createEntity("TerrianMesh");
	mTerrainNode->attachObject(mTerrainEntity);
	mTerrainEntity->setQueryFlags(QUERYMASK_TERRAIN);
	mTerrainNode->setPosition(0,0,0);

	//创建水面
	tex = Ogre::TextureManager::getSingleton().getByName("reflection");
	if(tex.isNull())
		tex = Ogre::TextureManager::getSingleton().createManual("reflection",
			Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
	mReflectionTarget = tex->getBuffer()->getRenderTarget();
	mReflectionTarget->addViewport(Core::getSingleton().mCamera)->setOverlaysEnabled(false);
	mReflectionTarget->addListener(this);
// 	mat = Ogre::MaterialManager::getSingleton().getByName("ReflectionWater");
// 	tech = mat->getTechnique(0);
// 	pass = tech->getPass(0);
// 	tu =  pass->getTextureUnitState(1);
// 	tu->setTextureName(tex->getName());

	mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, WATERHEIGHT);

	mWaterNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("WaterNode");
	mWaterObject = Core::getSingleton().mSceneMgr->createManualObject("WaterObject");

	mWaterObject->begin("DemoWater",Ogre::RenderOperation::OT_TRIANGLE_LIST);
	startpos += TILESIZE/2;
	for(int y = 0; y < terrainszie; y++)
		for(int x = 0; x < terrainszie; x++)
		{
			if(mMapData->getTerrainType(x -MAPBOLDER, y -MAPBOLDER ) == Water)
			{
				mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(0.0f,0.0f);
				mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(1.0f,1.0f);
				mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + y * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(1.0f,0.0f);
				mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(1.0f,1.0f);
				mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(0.0f,0.0f);
				mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE);
				mWaterObject->colour(1.0f,1.0f,1.0f);
				mWaterObject->normal(0.0f,1.0f,0.0f);
				mWaterObject->textureCoord(0.0f,1.0f);
			}
		}
	mWaterObject->end();

	mWaterNode->attachObject(mWaterObject);
	mWaterNode->setPosition(0,WATERHEIGHT,0);


	//设置摄像机移动范围
	
	float minx = 0.0f;// = ( - (float)(terrainszie - 2 * MAPBOLDER) / 2.0f - 1.0f) * TILESIZE ;
	getWorldCoords(0,0,minx,minx);
	minx -= TILESIZE/2;
	CameraContral::getSingleton().setMoveRect(minx, minx);
	CameraContral::getSingleton().resetCamera();

	//深度投影测试
// 	Ogre::MeshManager::getSingleton().createPlane("testplane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
// 		mWaterPlane, 64, 64, 1, 1, true, 1, 1, 1, Ogre::Vector3::UNIT_Z);
// 	Ogre::Entity* testent = Core::getSingleton().mSceneMgr->createEntity("testplaneent", "testplane");
// 	testent->setMaterialName("DepthTest");
// 	Ogre::SceneNode* testnode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode();
// 	testnode->attachObject(testent);
// 	testnode->setPosition(0.0f,10.0f,0.0f);
	return true;
}