Example #1
0
bool Terrain::GenFaultFormation(int iterations, int size, int minHeight, 
	int maxHeight, float weight, bool random)
{
	int x1, x2, z1, z2;
	float* heights = nullptr;
	int displacement;

	if(size <= 0)
		return false;
	if(random) // create true random map
		srand(time(nullptr));

	// terrain exists delete existing
	if(m_terrainData)
		delete[] m_terrainData;
	// allocate heightfield array memory
	m_size = size;
	heights = new float[m_size*m_size];
	m_terrainData = new unsigned char[m_size*m_size];
	if(heights == nullptr || m_terrainData == nullptr)
		return false;

	// initialise heightfiled array to zeros
	for(int i = 0; i < m_size*m_size; i++)
		heights[i] = 0;

	// generate heightfield
	for(int j = 0; j < iterations; j++) {
		// calculate reducing displcement value is how much to alter the hight
		displacement = maxHeight - ((maxHeight-minHeight)*j) / iterations;

		// pick the first point P1(x1, z1) at random from the entire heigh map
		x1 = (rand() % m_size);
		z1 = (rand() % m_size);

		// pickup second random point & ensure it's different from first
		do {
			x2 = (rand() % m_size);
			z2 = (rand() % m_size);
		}
		while (x2 == x1 && z2 == z1);

		// for each point P(x, z) in the filed calculate the new height values
		for(int z = 0; z < m_size; z++) {
			for(int x = 0; x < m_size; x++) {
				// determine which side of the line P1P2 point P lies on
				if((x-x1) * (z2-z1) - (x2-x1) * (z-z1) > 0) {
					heights[(z*m_size)+x] += (float)displacement;
				}
			}
		}
		AddFilter(heights, weight);

		// normalis heightfield
		NormaliseTerrain(heights);

		// copy the float heightfield to terrainData (in unsigned char)
		for(int z = 0; z < m_size; z++) {
			for(int x = 0; x < m_size; x++) {
				SetHeightAtPoint((unsigned char)heights[(z*m_size) + x], x, z);
			}
		}
	}
	delete[] heights;

	return true;
}
Example #2
0
//--------------------------------------------------------------
// Name:			CTERRAIN::MakeTerrainFault - public
// Description:		Create a height data set using the "Fault Formation"
//					algorithm.  Thanks a lot to Jason Shankel for this code!
// Arguments:		-iSize: Desired size of the height map
//					-iIterations: Number of detail passes to make
//					-iMinDelta, iMaxDelta: the desired min/max heights
//					-iIterationsPerFilter: Number of passes per filter
//					-fFilter: Strength of the filter
// Return Value:	A boolean value: -true: successful creation
//									 -false: unsuccessful creation
//--------------------------------------------------------------
bool CTERRAIN::MakeTerrainFault( int iSize, int iIterations, int iMinDelta, int iMaxDelta, float fFilter )
{
    float* fTempBuffer;
    int iCurrentIteration;
    int iHeight;
    int iRandX1, iRandZ1;
    int iRandX2, iRandZ2;
    int iDirX1, iDirZ1;
    int iDirX2, iDirZ2;
    int x, z;
    int i;

    if( m_heightData.m_ucpData )
        UnloadHeightMap( );

    m_iSize= iSize;

    //allocate the memory for our height data
    m_heightData.m_ucpData= new unsigned char [m_iSize*m_iSize];
    fTempBuffer= new float [m_iSize*m_iSize];

    //check to see if memory was successfully allocated
    if( m_heightData.m_ucpData==NULL )
    {
        //something is seriously wrong here
        g_log.Write( LOG_FAILURE, "Could not allocate memory for height map" );
        return false;
    }

    //check to see if memory was successfully allocated
    if( fTempBuffer==NULL )
    {
        //something is seriously wrong here
        g_log.Write( LOG_FAILURE, "Could not allocate memory for height map" );
        return false;
    }

    //clear the height fTempBuffer
    for( i=0; i<m_iSize*m_iSize; i++ )
        fTempBuffer[i]= 0;

    for( iCurrentIteration=0; iCurrentIteration<iIterations; iCurrentIteration++ )
    {
        //calculate the height range (linear interpolation from iMaxDelta to
        //iMinDelta) for this fault-pass
        iHeight= iMaxDelta - ( ( iMaxDelta-iMinDelta )*iCurrentIteration )/iIterations;

        //pick two points at random from the entire height map
        iRandX1= rand( )%m_iSize;
        iRandZ1= rand( )%m_iSize;

        //check to make sure that the points are not the same
        do
        {
            iRandX2= rand( )%m_iSize;
            iRandZ2= rand( )%m_iSize;
        } while ( iRandX2==iRandX1 && iRandZ2==iRandZ1 );


        //iDirX1, iDirZ1 is a vector going the same direction as the line
        iDirX1= iRandX2-iRandX1;
        iDirZ1= iRandZ2-iRandZ1;

        for( z=0; z<m_iSize; z++ )
        {
            for( x=0; x<m_iSize; x++ )
            {
                //iDirX2, iDirZ2 is a vector from iRandX1, iRandZ1 to the current point (in the loop)
                iDirX2= x-iRandX1;
                iDirZ2= z-iRandZ1;

                //if the result of ( iDirX2*iDirZ1 - iDirX1*iDirZ2 ) is "up" (above 0),
                //then raise this point by iHeight
                if( ( iDirX2*iDirZ1 - iDirX1*iDirZ2 )>0 )
                    fTempBuffer[( z*m_iSize )+x]+= ( float )iHeight;
            }
        }

        //erode terrain
        FilterHeightField( fTempBuffer, fFilter );
    }

    //normalize the terrain for our purposes
    NormalizeTerrain( fTempBuffer );

    //transfer the terrain into our class's unsigned char height buffer
    for( z=0; z<m_iSize; z++ )
    {
        for( x=0; x<m_iSize; x++ )
            SetHeightAtPoint( ( unsigned char )fTempBuffer[( z*m_iSize )+x], x, z );
    }

    //delete temporary buffer
    if( fTempBuffer )
    {
        //delete the data
        delete[] fTempBuffer;
    }

    return true;
}
Example #3
0
//--------------------------------------------------------------
// Name:			CTERRAIN::MakeTerrainPlasma - public
// Description:		Create a height data set using the "Midpoint
//					Displacement" algorithm.  Thanks a lot to
//					Jason Shankel for this code!
//					Note: this algorithm has limited use, since
//					CLOD algorithms usually require a height map
//					size of (n^2)+1 x (n^2)+1, and this algorithm
//					can only generate (n^2) x (n^2) maps
// Arguments:		-iSize: Desired size of the height map
//					-fRoughness: Desired roughness of the created map
// Return Value:	A boolean value: -true: successful creation
//									 -false: unsuccessful creation
//--------------------------------------------------------------
bool CTERRAIN::MakeTerrainPlasma( int iSize, float fRoughness )
{
    float* fTempBuffer;
    float fHeight, fHeightReducer;
    int iRectSize= iSize;
    int ni, nj;
    int mi, mj;
    int pmi, pmj;
    int i, j;
    int x, z;

    if( m_heightData.m_ucpData )
        UnloadHeightMap( );

    if( fRoughness<0 )
        fRoughness*= -1;

    fHeight		  = ( float )iRectSize/2;
    fHeightReducer= ( float )pow(2, -1*fRoughness);

    m_iSize= iSize;

    //allocate the memory for our height data
    m_heightData.m_ucpData= new unsigned char [m_iSize*m_iSize];
    fTempBuffer= new float [m_iSize*m_iSize];

    //check to see if memory was successfully allocated
    if( m_heightData.m_ucpData==NULL )
    {
        //something is seriously wrong here
        g_log.Write( LOG_FAILURE, "Could not allocate memory for height map" );
        return false;
    }

    //check to see if memory was successfully allocated
    if( fTempBuffer==NULL )
    {
        //something is seriously wrong here
        g_log.Write( LOG_FAILURE, "Could not allocate memory for height map" );
        return false;
    }

    //set the first value in the height field
    fTempBuffer[0]= 0.0f;

    //being the displacement process
    while( iRectSize>0 )
    {
        /*Diamond step -

        Find the values at the center of the retangles by averaging the values at
        the corners and adding a random offset:


        a.....b
        .     .
        .  e  .
        .     .
        c.....d

        e  = (a+b+c+d)/4 + random

        In the code below:
        a = (i,j)
        b = (ni,j)
        c = (i,nj)
        d = (ni,nj)
        e = (mi,mj)   */
        for( i=0; i<m_iSize; i+=iRectSize )
        {
            for( j=0; j<m_iSize; j+=iRectSize )
            {
                ni= ( i+iRectSize )%m_iSize;
                nj= ( j+iRectSize )%m_iSize;

                mi= ( i+iRectSize/2 );
                mj= ( j+iRectSize/2 );

                fTempBuffer[mi+mj*m_iSize]= ( float )( ( fTempBuffer[i+j*m_iSize] + fTempBuffer[ni+j*m_iSize] + fTempBuffer[i+nj*m_iSize] + fTempBuffer[ni+nj*m_iSize] )/4 + RangedRandom( -fHeight/2, fHeight/2 ) );
            }
        }

        /*Square step -

        Find the values on the left and top sides of each rectangle
        The right and bottom sides are the left and top sides of the neighboring rectangles,
          so we don't need to calculate them

        The height m_heightData.m_ucpData wraps, so we're never left hanging.  The right side of the last
        	rectangle in a row is the left side of the first rectangle in the row.  The bottom
        	side of the last rectangle in a column is the top side of the first rectangle in
        	the column

              .......
              .     .
              .     .
              .  d  .
              .     .
              .     .
        ......a..g..b
        .     .     .
        .     .     .
        .  e  h  f  .
        .     .     .
        .     .     .
        ......c......

        g = (d+f+a+b)/4 + random
        h = (a+c+e+f)/4 + random

        In the code below:
        	a = (i,j)
        	b = (ni,j)
        	c = (i,nj)
        	d = (mi,pmj)
        	e = (pmi,mj)
        	f = (mi,mj)
        	g = (mi,j)
        	h = (i,mj)*/
        for( i=0; i<m_iSize; i+=iRectSize )
        {
            for( j=0; j<m_iSize; j+=iRectSize )
            {

                ni= (i+iRectSize)%m_iSize;
                nj= (j+iRectSize)%m_iSize;

                mi= (i+iRectSize/2);
                mj= (j+iRectSize/2);

                pmi= (i-iRectSize/2+m_iSize)%m_iSize;
                pmj= (j-iRectSize/2+m_iSize)%m_iSize;

                //Calculate the square value for the top side of the rectangle
                fTempBuffer[mi+j*m_iSize]= ( float )( ( fTempBuffer[i+j*m_iSize]	  +
                                                        fTempBuffer[ni+j*m_iSize]	  +
                                                        fTempBuffer[mi+pmj*m_iSize]	  +
                                                        fTempBuffer[mi+mj*m_iSize] )/4+
                                                      RangedRandom( -fHeight/2, fHeight/2 ) );

                //Calculate the square value for the left side of the rectangle
                fTempBuffer[i+mj*m_iSize]= ( float )( ( fTempBuffer[i+j*m_iSize]	  +
                                                        fTempBuffer[i+nj*m_iSize]	  +
                                                        fTempBuffer[pmi+mj*m_iSize]	  +
                                                        fTempBuffer[mi+mj*m_iSize] )/4+
                                                      RangedRandom( -fHeight/2, fHeight/2 ) );
            }
        }

        //reduce the rectangle size by two to prepare for the next
        //displacement stage
        iRectSize/= 2;

        //reduce the height by the height reducer
        fHeight*= fHeightReducer;
    }

    //normalize the terrain for our purposes
    NormalizeTerrain( fTempBuffer );

    //transfer the terrain into our class's unsigned char height buffer
    for( z=0; z<m_iSize; z++ )
    {
        for( x=0; x<m_iSize; x++ )
            SetHeightAtPoint( ( unsigned char )fTempBuffer[( z*m_iSize )+x], x, z );
    }

    //delete temporary buffer
    if( fTempBuffer )
    {
        //delete the data
        delete[] fTempBuffer;
    }

    return true;
}
Example #4
0
bool TERRAIN::MakeTerrainFault( int size, int iterations, int min, int max, int smooth )
{
  float* tempBuffer;
  int currentIteration;
  int height;
  int randX1, randZ1;
  int randX2, randZ2;
  int dirX1, dirZ1;
  int dirX2, dirZ2;
  int x, z;
  int i;
  srand( time(NULL) );

  if( heightMap.arrayHeightMap )
    UnloadHeightMap( );

  sizeHeightMap= size;

  //reserver de la memoire
  heightMap.arrayHeightMap = new unsigned char [sizeHeightMap*sizeHeightMap];
  tempBuffer= new float [sizeHeightMap*sizeHeightMap];

  if( heightMap.arrayHeightMap==NULL )
    {
      return false;
    }

  if( tempBuffer==NULL )
    {
      return false;
    }

  for( i=0; i<sizeHeightMap*sizeHeightMap; i++ )
    tempBuffer[i]= 0;

  for( currentIteration=0; currentIteration<iterations; currentIteration++ )
    {
      //choisir la hauteur pour cette traversee avec interp. lineaire,
      //..on reduit la taille des changements effectues avec chaque iteration (grossier->detaille)
      //.. meme si toujours le meme point est choisi, on ne depasse pas la hauteur max.
      height= max - ( ( max-min )*currentIteration )/iterations;

      //choisir deux points de la carte aleatoirement
      randX1= rand( )%sizeHeightMap;
      randZ1= rand( )%sizeHeightMap;

      //...tant qu'ils sont differents
      do
	{
	  randX2= rand( )%sizeHeightMap;
	  randZ2= rand( )%sizeHeightMap;
	} while ( randX2==randX1 && randZ2==randZ1 );


      //le vecteur de la ligne aleatoire qui divise la carte en deux
      dirX1= randX2-randX1;
      dirZ1= randZ2-randZ1;

      for( z=0; z<sizeHeightMap; z++ )
	{
	  for( x=0; x<sizeHeightMap; x++ )
	    {
	      //vecteur de position du point traite actuellement
	      dirX2= x-randX1;
	      dirZ2= z-randZ1;
	      //utiliser le signe du produit croise pour decide si on exhausse la hauteur de ce point
	      if( ( dirX2*dirZ1 - dirX1*dirZ2 )>0 )
		tempBuffer[( z*sizeHeightMap )+x]+= ( float )height;
	    }
	}

      //effectuer erosion
      SmoothTerrain(tempBuffer,smooth);	//MOYENNE
      //Smooth1DTerrain(tempBuffer,smooth);
    }

  //adapter l'echelle de valeurs
  NormalizeTerrain( tempBuffer );

  //sauvegarder le terrain cree dans la variable de la classe TERRAIN,
  //.. on n'a plus besoin de la precision float
  for( z=0; z<sizeHeightMap; z++ )
    {
      for( x=0; x<sizeHeightMap; x++ )
	SetHeightAtPoint( ( unsigned char )tempBuffer[( z*sizeHeightMap )+x], x, z );
    }

  if( tempBuffer )
    {
      delete[] tempBuffer;
    }
  return true;
}