| 69 | } |
| 70 | |
| 71 | bool HeightmapImage::LoadData(const std::string& rel_path, HMScale scaled) { |
| 72 | height_data_.clear(); |
| 73 | |
| 74 | rel_path_ = rel_path; |
| 75 | |
| 76 | std::string cache_suffix = "_hmcache"; |
| 77 | if (scaled == DOWNSAMPLED) { |
| 78 | cache_suffix += "_scaled"; |
| 79 | } |
| 80 | |
| 81 | std::string load_path; |
| 82 | ModID load_modsource; |
| 83 | if (CacheFile::CheckForCache(rel_path_, cache_suffix, &load_path, &load_modsource, &checksum_)) { |
| 84 | if (ReadCacheFile(load_path, checksum_)) { |
| 85 | modsource_ = load_modsource; |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | char abs_path[kPathSize]; |
| 91 | ModID modsource; |
| 92 | FindFilePath(rel_path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths, true, NULL, &modsource); |
| 93 | modsource_ = modsource; |
| 94 | |
| 95 | int img_width = 0, img_height = 0, num_comp = 0; |
| 96 | stbi_us* data = stbi_load_16(abs_path, &img_width, &img_height, &num_comp, 0); |
| 97 | |
| 98 | if (data == NULL) { |
| 99 | FatalError("Error", "Could not load heightmap: %s", rel_path.c_str()); |
| 100 | } else { |
| 101 | if (scaled) { |
| 102 | width_ = kTerrainDimX; |
| 103 | depth_ = kTerrainDimY; |
| 104 | } else { |
| 105 | width_ = img_width; |
| 106 | depth_ = img_height; |
| 107 | } |
| 108 | |
| 109 | int heightDataSize = width_ * depth_; |
| 110 | |
| 111 | if (0 < heightDataSize) { |
| 112 | // ...allocate enough space for the height data... |
| 113 | height_data_.resize(heightDataSize, 0); |
| 114 | // m_flowData.resize(heightDataSize,vecf(2,0)); |
| 115 | |
| 116 | float scale_factor = kTerrainHeightScale; |
| 117 | float x_scale = img_width / (float)width_; |
| 118 | float z_scale = img_height / (float)depth_; |
| 119 | |
| 120 | if (num_comp == 1) { // monochrome texture |
| 121 | for (int z = 0; z < depth_; z++) { // flipped |
| 122 | stbi_us* bits = &data[((int)(z * z_scale)) * img_height]; |
| 123 | for (int x = 0; x < width_; x++) { |
| 124 | // Convert unsigned shorts to floats |
| 125 | height_data_[x + (((depth_ - 1) - z) * width_)] = (float)(bits[(int)(x * x_scale)]) / scale_factor; |
| 126 | } |
| 127 | } |
| 128 | } else { |
no test coverage detected