//laddar heightmap från en rawfil. void HeigthMap::Load(std::string filename, int Width, int Height) { m_Width = Width; m_Height = Height; m_Map = new float*[Height]; for (int z = 0; z < Height; z++) m_Map[z] = new float[Width]; std::vector<unsigned char> vertexHeights(Width * Height); //läser in data från raw filen. std::ifstream fin; fin.open(filename, std::ios_base::binary); if (!fin) HRESULT(E_FAIL); fin.read((char *)&vertexHeights[0], (std::streamsize)vertexHeights.size()); fin.close(); //sparar den inladdade informationen i heigtmappen. for (int z = 0; z < Height; z++) { //Inverterar data i Z axeln. int Z = Height - 1 - z; for (int x = 0; x < Width; x++) { m_Map[Z][x] = (float) vertexHeights[z*Width + x]; } } //filtrerar höjdmappen 20 gånger. //Filter(20); }
//Laddar BlendMap från RAW-fil void BlendMap::Load(std::string filename, int Width, int Height) { m_Width = Width; m_Height = Height; m_Map = new XMFLOAT4*[Height]; for (int z = 0; z < Height; z++) m_Map[z] = new XMFLOAT4[Width]; std::vector<unsigned char> vertexHeights(4 * Width * Height); //Läser in data från fil. std::ifstream fin; fin.open(filename, std::ios_base::binary); if (!fin) HRESULT(E_FAIL); fin.read((char *)&vertexHeights[0], (std::streamsize)vertexHeights.size()); fin.close(); int k = 0; for (int z = 0; z < Height; z++) { //Inverterar data i Z axeln. int Z = Height - 1 - z; for (int x = 0; x < Width; x++) { float r = (float) vertexHeights[k]; //Rött värde för pixeln. float g = (float) vertexHeights[k + 1]; //Grönt värde för pixeln. float b = (float) vertexHeights[k + 2]; //Blått värde för pixeln. float a = (float) vertexHeights[k + 3]; //Alpha värde för pixeln. float sum = r + g + b + a; //Summan av färgkomponenterna för pixeln. r = r / sum; //Andel rött i pixeln. g = g / sum; //Andel grönt i pixeln. b = b / sum; //Andel blått i pixeln. a = a / sum; //Andel alpha i pixeln. m_Map[Z][x] = XMFLOAT4(r,g,b, a); //Sparar värderna. k += 4; //Räknar fram. } } }
void HeightMap::LoadRaw(int rows, int cols, const std::string& filename, const float& heightScale, const float& heightOffset) { this->zHeightMapName = filename; this->zHeightOffset = heightOffset; this->zHeightScale = heightScale; this->zCols = cols; this->zRows = rows; std::vector<unsigned char> vertexHeights(rows * cols); std::ifstream fin; fin.open(filename.c_str(), std::ios::binary); if (!fin.is_open()) throw(""); fin.read((char*)&vertexHeights[0], (std::streamsize)vertexHeights.size()); fin.close(); this->zHeightMap.resize(rows * cols); for (int i = 0; i < cols; i++) { for(int j = 0; j < rows; j++) { int index = i * rows + j; this->zHeightMap[index] = (float)vertexHeights[index] * this->zHeightScale + this->zHeightOffset; } } //Filter the table to smooth it out. We do this because x height //steps may be rough. And now that we copied the data into a //float-table, we have more precision. (Boxfilter) this->Filter3x3(); }