Example #1
0
void TerrainLattice::ModelViewMatrixChanged()
{
	if(!m_useGeoMipmap)
	{
		SetLowestVisibleHeight(FLOAT_POS_INFINITY);

		m_BoundingBox.SetEmpty();
		/// tessellation according to the new view matrix
		for (int dir = 0; dir < 9 ; dir++)
		{
			TerrainTileCacheMap_type::iterator iter = m_pCachedTerrains.find(m_CurrentTerrainIndex[dir]);
			if(iter != m_pCachedTerrains.end())
			{
				//PERF_BEGIN("tesse");
				if( ((*iter).second.pTerrain->Tessellate()) > 0 )
				{
					CShapeBox box = (*iter).second.pTerrain->GetBoundingBoxW();
					if(box.IsValid())
						m_BoundingBox.Extend(&box,1);
				}
				//PERF_END("tesse");
				float fHeight = (*iter).second.pTerrain->GetLowestVisibleHeight();
				if(fHeight < GetLowestVisibleHeight())
					SetLowestVisibleHeight(fHeight);
			}
		}
	
		/// Update neighbor, in case the shorelines are not uniformly tessellated.
		// TODO: no need to update so many neighbors 
		for (int dir = 0; dir < 9 ; dir++)
		{
			TerrainTileCacheMap_type::iterator iter = m_pCachedTerrains.find(m_CurrentTerrainIndex[dir]);
			if(iter != m_pCachedTerrains.end())
			{
				for (int direction = 0; direction < 9; direction++)
				{
					if (direction != ParaTerrain::DIR_CENTER)
					{
						Terrain *pTerrain = GetTerrainRelative((*iter).second.pTerrain, (ParaTerrain::DIRECTION) direction);
						if (pTerrain != NULL)
						{
							(*iter).second.pTerrain->UpdateNeighbor(pTerrain, (ParaTerrain::DIRECTION) direction);
							pTerrain->UpdateNeighbor((*iter).second.pTerrain, GetOppositeDirection((ParaTerrain::DIRECTION) direction));
						}
					}
				}
			}
		}
		/// repair cracks and rebuild render buffer
		for (int dir = 0; dir < 9 ; dir++)
		{
			TerrainTileCacheMap_type::iterator iter = m_pCachedTerrains.find(m_CurrentTerrainIndex[dir]);
			if(iter != m_pCachedTerrains.end())
			{
				Terrain* pTerrain = (*iter).second.pTerrain;
				//PERF_BEGIN("repair");
				pTerrain->RepairCracks();
				//PERF_END("repair");
				//PERF_BEGIN("Buff");
				pTerrain->RebuildRenderBuffer();
				//PERF_END("Buff");
			}
		}
	}
	else
	{
		SetLowestVisibleHeight(FLOAT_POS_INFINITY);

		m_BoundingBox.SetEmpty();
		/// tessellation according to the new view matrix
		for (int dir = 0; dir < 9 ; dir++)
		{
			TerrainTileCacheMap_type::iterator iter = m_pCachedTerrains.find(m_CurrentTerrainIndex[dir]);
			if(iter != m_pCachedTerrains.end())
			{
				//PERF_BEGIN("tesse");
				if( ((*iter).second.pTerrain->Tessellate()) > 0 )
				{
					CShapeBox box = (*iter).second.pTerrain->GetBoundingBoxW();
					if(box.IsValid())
						m_BoundingBox.Extend(&box,1);
				}
				//PERF_END("tesse");
				float fHeight = (*iter).second.pTerrain->GetLowestVisibleHeight();
				if(fHeight < GetLowestVisibleHeight())
					SetLowestVisibleHeight(fHeight);
			}
		}

		CalcGeoMipmapLod(m_camPosX,m_camPosY);
	}
}