Skip to content

Loading textures from image files

Shawn Hargreaves edited this page Jun 16, 2016 · 4 revisions

Compressed texture formats (e.g. ETC, ASTC, BC, DXT)

Please see our wiki page about compressed textures for more info.

Other texture formats (e.g. JPEG, PNG, BMP)

If you wish to load images from formats such as JPEG or PNG into ANGLE, then you have several options:

  • Cross-platform solutions such as libJPEG or libPNG
  • Microsoft technologies such as Windows Imaging Component (WIC)

Libraries such as libJPEG and libPNG have been successfully used in many Windows Store applications and frameworks, and provide a cross-platform solution to image loading. However, you may wish to consider using WIC in your application, which can have many benefits (for example: better performance, reduced app download size).

WIC and WICTextureLoader

WIC is a powerful image decoding library distributed with Windows. It supports many standard image formats (for example: TIFF, JPEG, PNG, GIF, BMP), and is used by most in-box and many out-of-box components to decode images on Microsoft platforms.

The DirectX Tool Kit (DirectXTK) includes WICTextureLoader, which is an easy-to-use helper for developers wanting to load images into DirectX textures using WIC. We have created a version of this helper for Windows Store apps to use with ANGLE.

Obtaining WICTextureLoader

A sample demonstrating how to use WICTextureLoader can be found in our GitHub samples directory.

To use WICTextureLoader in your own project, simply copy WICTextureLoader.h and WICTextureLoader.cpp from the samples directory and use it like this:

// Load the texture
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

HRESULT hr = WICTexImage2DFromFile(GL_TEXTURE_2D, 0, L"sample.ext");
if (FAILED(hr))
{
    // Error
}

Vertically flipping images

Due to coordinate system differences, the 'WICTexImage2DFromFile' helper usually has to vertically flip the contents of image files at runtime before passing their data to ANGLE. This is an unnecessary overhead which you can avoid by manually flipping the contents of your image files and setting WICTexImage2DFromFile's optional 'verticalFlip' parameter to false.