Book Image

3D Graphics Rendering Cookbook

By : Sergey Kosarevsky, Viktor Latypov
4 (2)
Book Image

3D Graphics Rendering Cookbook

4 (2)
By: Sergey Kosarevsky, Viktor Latypov

Overview of this book

OpenGL is a popular cross-language, cross-platform application programming interface (API) used for rendering 2D and 3D graphics, while Vulkan is a low-overhead, cross-platform 3D graphics API that targets high-performance applications. 3D Graphics Rendering Cookbook helps you learn about modern graphics rendering algorithms and techniques using C++ programming along with OpenGL and Vulkan APIs. The book begins by setting up a development environment and takes you through the steps involved in building a 3D rendering engine with the help of basic, yet self-contained, recipes. Each recipe will enable you to incrementally add features to your codebase and show you how to integrate different 3D rendering techniques and algorithms into one large project. You'll also get to grips with core techniques such as physically based rendering, image-based rendering, and CPU/GPU geometry culling, to name a few. As you advance, you'll explore common techniques and solutions that will help you to work with large datasets for 2D and 3D rendering. Finally, you'll discover how to apply optimization techniques to build performant and feature-rich graphics applications. By the end of this 3D rendering book, you'll have gained an improved understanding of best practices used in modern graphics APIs and be able to create fast and versatile 3D rendering frameworks.
Table of Contents (12 chapters)

Getting started with Etc2Comp 

One significant drawback of high-resolution textured data is that it requires a lot of GPU memory to store and process. All modern, real-time rendering APIs provide some sort of texture compression, allowing us to store textures in compressed formats on the GPU. One such format is ETC2. This is a standard texture compression format for OpenGL and Vulkan.

Etc2Comp is an open source tool that converts bitmaps into ETC2 format. The tool is built with a focus on encoding performance to reduce the amount of time required to package asset-heavy applications and reduce the overall application size. In this recipe, you will learn how to integrate this tool within your own applications to construct tools for your custom graphics preparation pipelines.

Getting ready

The project can be downloaded using this Bootstrap snippet:

{
  "name": "etc2comp",
  "source": {
    "type": "git",
    "url": "https://github.com/google/etc2comp",
    "revision": "9cd0f9cae0f32338943699bb418107db61bb66f2"
  }
}

Etc2Comp was released by Google "as-is" with the intention of being used as a command-line tool. Therefore, we will need to include some additional .cpp files within the CMake target to use it as a library that is linked to our application. The Chapter2/08_ETC2Comp/CMakeLists.txt file contains the following lines to do this:

target_sources(Ch2_Sample08_ETC2Comp PUBLIC                ${CMAKE_CURRENT_BINARY_DIR}/../../../               deps/src/etc2comp/EtcTool/EtcFile.cpp)
target_sources(Ch2_Sample08_ETC2Comp PUBLIC                ${CMAKE_CURRENT_BINARY_DIR}/../../../               deps/src/etc2comp/EtcTool/EtcFileHeader.cpp)

The complete source code is located at Chapter2/08_ETC2Comp.

How to do it...

Let's build an application that loads a .jpg image via the STB library, converts it into an ETC2 image, and saves it within the .ktx file format:

  1. First, it is necessary to include some header files:
    #include "etc2comp/EtcLib/Etc/Etc.h"
    #include "etc2comp/EtcLib/Etc/EtcImage.h"
    #include "etc2comp/EtcLib/Etc/EtcFilter.h"
    #include "etc2comp/EtcTool/EtcFile.h"
    #define STB_IMAGE_IMPLEMENTATION
    #include <stb/stb_image.h>
  2. Let's load an image as a 4-component RGBA bitmap:
    int main() {
      int w, h, comp;
      const uint8_t* img = stbi_load(    "data/ch2_sample3_STB.jpg", &w, &h, &comp, 4);
  3. Etc2Comp takes floating-point RGBA bitmaps as input, so we have to convert our data, as follows:
      std::vector<float> rgbaf;
      for (int i = 0; i != w * h * 4; i+=4) {
         rgbaf.push_back(img[i+0] / 255.0f);
         rgbaf.push_back(img[i+1] / 255.0f);
         rgbaf.push_back(img[i+2] / 255.0f);
         rgbaf.push_back(img[i+3] / 255.0f);
      }

    Note

    Besides manual conversion, as mentioned earlier, STB can load 8-bit-per-channel images as floating-point images directly via the stbi_loadf() API. However, it will do an automatic gamma correction. Use stbi_ldr_to_hdr_scale(1.0f) and stbi_ldr_to_hdr_gamma(2.2f) if you want to control the amount of gamma correction.

  4. Now we can encode the floating-point image into ETC2 format using Etc2Comp. Because we don't use alpha transparency, our target format should be RGB8. We will use the default BT.709 error metric minimization schema:
      const auto etcFormat = Etc::Image::Format::RGB8;
      const auto errorMetric = Etc::ErrorMetric::BT709;
      Etc::Image image(rgbaf.data(), w, h, errorMetric);

    The encoder takes the number of threads as input. Let's use the value returned by thread::hardware_concurrency() and 1024 as the number of concurrent jobs:

      image.Encode(etcFormat, errorMetric,    ETCCOMP_DEFAULT_EFFORT_LEVEL,    std::thread::hardware_concurrency(), 1024);
  5. Once the image is converted, we can save it into the .ktx file format, which can store compressed texture data that is directly consumable by OpenGL. Etc2Comp provides the Etc::File helper class to do this:
      Etc::File etcFile("image.ktx",    Etc::File::Format::KTX,    etcFormat,    image.GetEncodingBits(),    image.GetEncodingBitsBytes(),    image.GetSourceWidth(),    image.GetSourceHeight(),    image.GetExtendedWidth(),    image.GetExtendedHeight());
      etcFile.Write();
      return 0;
    }

The bitmap is saved within the image.ktx file. It can be loaded into an OpenGL or Vulkan texture, which we will do in subsequent chapters.

There's more...

Pico Pixel is a great tool that you can use to view .ktx files and other texture formats (https://pixelandpolygon.com). It is freeware but not open source. There is an issues tracker that is publicly available on GitHub. You can find it at https://github.com/inalogic/pico-pixel-public/issues.

For those who want to jump into the latest state-of-the-art texture compression techniques, please refer to the Basis project from Binomial at https://github.com/BinomialLLC/basis_universal.