You are getting this code from the same book I have. You are destroying the memory. Plain and simple and this is your problem for the most part.
My guess is that if you have destroyed the memory for the array, you have probably fragged it elsewhere too.
Here is the values you will need:
Code:...
BYTE *pTempMap=new BYTE[iWidth*iDepth];
BYTE *pHeightMap=new WORD[iWidth*iDepth];
int handle=_open("Heightmap.dat",_O_RDONLY);
if (handle==-1)
{
::MessageBox(0,TEXT("CTerrain::Create() - Failed to load heightmap",0,0);
return;
}
DWORD dwSizeBytes=iWidth*iDepth*sizeof(WORD);
_read(handle,pTempMap,dwSizeBytes);
_close(handle);
int iNumVertsPerCol=iWidth;
int iNumVertsPerRow=iDepth;
int iNumVerts=iWidth*iDepth;
int iNumCellsPerCol=iNumVertsPerCol-1;
int iNumCellsPerRow=iNumVertsPerRow-1;
int iNumCells=iNumVertsPerCol*iNumVertsPerRow;
int iNumTris=iNumCells*2;
int iNumIndices=iNumTris*3;
for (int i=0;i<iNumVerts;i++)
{
pHeightMap[i]=pTempMap[i]*iScale;
}
delete [] pTempMap;
...
I have more source for the terrain but I have done far more than the book ever showed so it may confuse you. My version has a quadtree, separates the heightmap from the terrain class (they are both sep objects), uses a sky scattering shader, and employs billboards for trees on the terrain.