| 194 | } |
| 195 | |
| 196 | void LoadImage(const std::string& filename, int& texWidth, int& texHeight, std::vector<std::uint8_t>& imageData) |
| 197 | { |
| 198 | // Print information about current texture |
| 199 | std::cout << "load image: \"" << filename << "\"" << std::endl; |
| 200 | |
| 201 | // Load image data from file (using STBI library, see http://nothings.org/stb_image.h) |
| 202 | int w = 0, h = 0, n = 0; |
| 203 | unsigned char* buf = stbi_load(filename.c_str(), &w, &h, &n, 4); |
| 204 | if (!buf) |
| 205 | throw std::runtime_error("failed to load image: \"" + filename + "\""); |
| 206 | |
| 207 | // Check if image size is compatible |
| 208 | if (texWidth == 0) |
| 209 | { |
| 210 | texWidth = w; |
| 211 | texHeight = h; |
| 212 | } |
| 213 | else if (w != texWidth || h != texHeight) |
| 214 | throw std::runtime_error("size mismatch for texture array while loading image: \"" + filename + "\""); |
| 215 | |
| 216 | // Copy into array |
| 217 | auto offset = imageData.size(); |
| 218 | auto bufSize = static_cast<std::size_t>(w*h*4); |
| 219 | |
| 220 | imageData.resize(offset + bufSize); |
| 221 | ::memcpy(&(imageData[offset]), buf, bufSize); |
| 222 | |
| 223 | // Release image data |
| 224 | stbi_image_free(buf); |
| 225 | } |
| 226 | |
| 227 | void FillImage(int& texWidth, int& texHeight, std::vector<std::uint8_t>& imageData) |
| 228 | { |