Extrait de la fonction Create
{
...
CreateHeightMap(FileHeightMap.c_str());
CreateMiniTerrain();
...
}
void CTerrain::CreateHeightMap(const char* FileName)
{
...
// Création du tableau d'altitude du terrain
m_pfHeightMap = new float[m_uiNbPointX * m_uiNbPointY];
for(unsigned int Y=0; Y < m_uiNbPointY; ++Y)
{
for(unsigned int X = 0; X < m_uiNbPointX; ++X)
{
m_pfHeightMap[ X + m_uiNbPointX*Y ] = (float)piSurfaceBuffer[X + Y*(LockRect.Pitch)]);
}
}
if( FAILED( TexHeightMap->UnlockRect(0) ) )
{
std::string Message = "Erreur lors du Unlock de la texture de densite pour le miniterrain X=";
LogDbg(Message)<<"\n";
}
SAFE_RELEASE(TexHeightMap);
//Lissage
float* HeightMap = new float[m_uiNbPointX * m_uiNbPointY];
for(unsigned int Y=0; Y<m_uiNbPointY; Y++)
for(unsigned int X=0; X<m_uiNbPointX; X++)
HeightMap[X + m_uiNbPointX*Y ] = m_pfHeightMap[X + m_uiNbPointX*Y ];
//idem pour les bords et les coins
...
delete[] HeightMap;
//LogDbg("\n");
}
void CTerrain::CreateMiniTerrain()
{
//Calcul des normals
D3DXVECTOR3 vX;
vX.x = 0;
D3DXVECTOR3 vZ;
vZ.x = 0;
D3DXVECTOR3 vY0, vY1, vY2, vY3;
D3DXVECTOR3 tpNormal;
D3DXVECTOR3* Normals;
Normals = new D3DXVECTOR3[m_uiNbPointX * m_uiNbPointY];
//Coin bas gauche
vX.x = 1;
vZ.z = 1;
vX.y = (GetAltitude(1, 0) - GetAltitude(0, 0));
vZ.y = (GetAltitude(0, 1) - GetAltitude(0, 0));
D3DXVec3Cross(&vY0, &vZ, &vX);
D3DXVec3Normalize(&vY0, &vY0);
//Calcul
Normals[ 0 ].x = vY0.x;
Normals[ 0 ].y = vY0.y;
Normals[ 0 ].z = vY0.z;
...
}
float CTerrain::GetAltitude(int TileX, int TileY)
{
if(m_pfHeightMap != NULL)
{
if( TileX<0 )
TileX=0;
if( TileX>m_uiNbPointX-1)
TileX=m_uiNbPointX-1;
if( TileY<0 )
TileY=0;
if( TileY>m_uiNbPointY-1)
TileY=m_uiNbPointY-1;
return (m_pfHeightMap[TileX + m_uiNbPointX * TileY]);
}
LogDbg("Erreur GetAltitude")<<"\n";
return 0;
}
Partager