bool TerrainLighting::CreateSlopeLighting(unsigned char *heightmap, int mapSize) { float fShade; int x, z; int count = 0; //if using light map or height map empty if(!heightmap || m_lightingType == LIGHTMAP) return false; // if light map exists delete existing if(m_lightmapData) delete[] m_lightmapData; //allocate memory for the new lightmap data buffer m_lightmapSize = mapSize; m_lightmapData= new unsigned char [m_lightmapSize*m_lightmapSize]; //loop through all vertices for( z=0; z<m_lightmapSize; z++ ) { for( x=0; x<m_lightmapSize; x++ ) { int xl=x-m_directionX; int zl=z-m_directionZ; //position of the nearby vertex if(!Inbounds(xl, zl)) { fShade = 1.0f; // (x,z) is on the edge of heightmap, not blocked } else { fShade= 1.0f - (GetValueAtPoint(heightmap, mapSize, xl, zl) - GetValueAtPoint(heightmap, mapSize, x, z)) / m_softness; } if( fShade < m_minBrightness ) { fShade= m_minBrightness; } if( fShade>m_maxBrightness ) fShade= m_maxBrightness; SetBrightnessAtPoint( x, z, ( unsigned char )( fShade*255 ) ); count++; } } return true; }
//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 ) ); } } }