Пример #1
0
//a l'interieur de chaque texture, on tient compte des variations d'hauteur
//.. pour ne pas avoir des regions de couleur identique bien que la hauteur varie
//.. pour cela, on doit savoir la hauteur interpole a chaque position
// heightToTexRatio: relation taille de map d'hauteur / taille de texture
unsigned char TERRAIN::InterpolateHeight( int x, int z, float heightToTexRatio )
{
  unsigned char  Low,  HighX,  HighZ;
  float  X,  Z;
  float scaledX= x*heightToTexRatio;		//echelle d'interpolation necessaire
  float scaledZ= z*heightToTexRatio;
  float interpolation;

  //borne inf
  Low= GetTrueHeightAtPoint( ( int )scaledX, ( int )scaledZ );

  //borne sup x
  if( ( scaledX+1 )>sizeHeightMap )
    return  Low;
  else
    HighX= GetTrueHeightAtPoint( ( int )scaledX+1, ( int )scaledZ );

  //valeur interpolee x
  interpolation= ( scaledX-( int )scaledX );
  X= ( (  HighX- Low )*interpolation )+ Low;

  //borne sup z
  if( ( scaledZ+1 )>sizeHeightMap )
    return  Low;
  else
    HighZ= GetTrueHeightAtPoint( ( int )scaledX, ( int )scaledZ+1 );

  //valeur interpolee z
  interpolation= ( scaledZ-( int )scaledZ );
  Z= ( (  HighZ- Low )*interpolation )+ Low;

  //moyenne des deux ~= approx. de la vraie hauteur
  return ( ( unsigned char )( (  X+ Z )/2 ) );
}
Пример #2
0
//--------------------------------------------------------------
// Name:			CBRUTE_FORCE::Render - public
// Description:		Render the terrain height field
// Arguments:		None
// Return Value:	None
//--------------------------------------------------------------
void CBRUTE_FORCE::Render( void )
{
	unsigned char ucColor;
	int	iZ;
	int iX;

	//reset the counting variables
	m_iVertsPerFrame= 0;
	m_iTrisPerFrame = 0;

	//cull non camera-facing polygons
	glEnable( GL_CULL_FACE );

	//loop through the Z-axis of the terrain
	for( iZ=0; iZ<m_iSize-1; iZ++ )
	{
		//begin a new triangle strip
		glBegin( GL_TRIANGLE_STRIP );

			//loop through the X-axis of the terrain
			//this is where the triangle strip is constructed
			for( iX=0; iX<m_iSize-1; iX++ )
			{
				//use height-based coloring (high-points are
				//light, low points are dark)
				ucColor= GetTrueHeightAtPoint( iX, iZ );

				//set the color with OpenGL, and render the point
				glColor3ub( ucColor, ucColor, ucColor );
				glVertex3f( ( float )iX, GetScaledHeightAtPoint( iX, iZ ), ( float )iZ );

				//use height-based coloring (high-points are
				//light, low points are dark)
				ucColor= GetTrueHeightAtPoint( iX, iZ+1 );

				//set the color with OpenGL, and render the point
				glColor3ub( ucColor, ucColor, ucColor );
				glVertex3f( ( float )iX, GetScaledHeightAtPoint( iX, iZ+1 ), ( float )iZ+1 );

				//increase the vertex count by two (which is how many we sent to the API)
				m_iVertsPerFrame+= 2;

				//there are no triangles being rendered on the first X-loop, they just start the
				//triangle strip off
				if( iX!= 0 )
					m_iTrisPerFrame+= 2;
			}

		//end the triangle strip
		glEnd( );
	}
}
Пример #3
0
//on calcule les positions d'ombrages
//..idee de la methode: si entre un point et la source de lumiere, dans la direction
//..de la lumiere, il y a un autre element du terrain, ce point se trouve dans l'ombre
//.. pour cela, on soustrait l'hauteur du vertex voisin de l'hauteur du vertex actuel
//.. CEPENDANT, on est limite a des changements de position de lumiere par pas de 45°
//.. et on ne tient pas compte de l'hauteur de la source de lumiere! (seulement le vertex directement a cote compte)
void TERRAIN::CalculateLighting( void )
{
  float shade;
  int x, z;

  if( lightMap.sizeLightMap!=sizeHeightMap || lightMap.arrayLightMap==NULL )
    {
      delete[] lightMap.arrayLightMap;
      lightMap.arrayLightMap= new unsigned char [sizeHeightMap*sizeHeightMap];
      lightMap.sizeLightMap= sizeHeightMap;
    }

  //pour chaque vertex
  for( z=0; z<sizeHeightMap; z++ )
    {
      for( x=0; x<sizeHeightMap; x++ )
	{
	  //pour ne pas depasser des bornes
	  if( z>=directionZ && x>=directionX )
	    {
	      //comparer les hauteurs, et on rend plus doux les frontieres
	      //ici, on ne fait PAS de calcul genre "tracer les rayons"...
	      shade= 1.0f-( GetTrueHeightAtPoint( x-directionX, z-directionZ ) -
			    GetTrueHeightAtPoint( x, z ) )/lightSoftness;
	    }
	  else
	    shade= 1.0f;

	  if( shade<minBrightness )
	    shade= minBrightness;
	  if( shade>maxBrightness )
	    shade= maxBrightness;

	  SetBrightnessAtPoint( x, z, ( unsigned char )( shade*255 ) );
	}
    }
}