MCPcopy Create free account
hub / github.com/Apress/Ray-Tracing-Gems-II / LoadTexture

Function LoadTexture

Chapter_14/src/Textures.cpp:250–285  ·  view source on GitHub ↗

* Load an image from disk using STB library. */

Source from the content-addressed store, hash-verified

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

Callers 2

ParseGLFTexturesFunction · 0.85

Calls 8

FormatTextureFunction · 0.85
ProcessTextureFunction · 0.85
c_strMethod · 0.80
stbi_loadFunction · 0.50
appendMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected