* Load an image from disk using STB library. */
| 248 | * Load an image from disk using STB library. |
| 249 | */ |
| 250 | bool LoadTexture(Texture& texture, DirectX::XMFLOAT2& uvAdjustment) |
| 251 | { |
| 252 | // Initialize adjustment to identity |
| 253 | uvAdjustment.x = 1.0f; |
| 254 | uvAdjustment.y = 1.0f; |
| 255 | |
| 256 | // Load the texture with stb_image (require 4 component RGBA) |
| 257 | TextureData textureData; |
| 258 | textureData.texels = stbi_load(texture.filepath.c_str(), &textureData.width, &textureData.height, &textureData.stride, STBI_rgb_alpha); |
| 259 | textureData.stride = 4; |
| 260 | textureData.rowPitch = UINT64(textureData.width) * textureData.stride; |
| 261 | textureData.texelBytes = textureData.rowPitch * UINT64(textureData.height); |
| 262 | textureData.stbAllocation = true; |
| 263 | |
| 264 | if (!textureData.texels) |
| 265 | { |
| 266 | std::string msg = "Error: failed to load texture: \'"; |
| 267 | msg.append(texture.name); |
| 268 | msg.append("\'"); |
| 269 | MessageBox(NULL, std::wstring(msg.begin(), msg.end()).c_str(), L"Error", MB_OK); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | // Prep the texture for compression and use on the GPU (alignment to 4x4) |
| 274 | FormatTexture(textureData, uvAdjustment); |
| 275 | |
| 276 | // Store raw texture data as mip level 0 |
| 277 | texture.mips.push_back(textureData); |
| 278 | |
| 279 | #if ENABLE_GPU_COMPRESSION |
| 280 | // Compress texture on GPU and generate mips |
| 281 | ProcessTexture(texture); |
| 282 | #endif |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Releases all memory used by texture |
no test coverage detected