Chapter 4. Using Textures

In this chapter, we will cover:

  • Applying a 2D texture
  • Applying multiple textures
  • Using alpha maps to discard pixels
  • Using normal maps
  • Simulating reflection with cube maps
  • Simulating refraction with cube maps
  • Applying a projected texture
  • Rendering to a texture
  • Using sampler objects

Introduction

Textures are an important and fundamental aspect of real-time rendering in general, and OpenGL in particular. The use of textures within a shader opens up a huge range of possibilities. Beyond just using textures as sources of color information, they can be used for things like depth information, shading parameters, displacement maps, normal vectors, or other vertex data. The list is virtually endless. Textures are among the most widely used tools for advanced effects in OpenGL programs, and that isn't likely to change anytime soon.

Note

In OpenGL 4, we now have the ability to read and write to memory via buffer textures, shader storage buffer objects, and image textures (image load/store). This further muddies the waters of what exactly defines a texture. In general, we might just think of it as a buffer of data that may or may not contain an image.

OpenGL 4.2 introduced immutable storage textures. Despite what the term may imply, immutable storage textures are not textures that can't change. Instead, the term immutable refers to the fact that, once the texture is allocated, the storage cannot be changed. That is, the size, format, and number of layers are fixed, but the texture content itself can be modified. The word immutable refers to the allocation of the memory, not the contents of the memory. Immutable storage textures are preferable in the vast majority of cases because of the fact that many run-time (draw-time) consistency checks can be avoided, and you include a certain degree of "type safety," since we can't accidentally change the allocation of a texture. Throughout this book, we'll use immutable storage textures exclusively.

Note

Immutable storage textures are allocated using the glTexStorage* functions. If you're experienced with textures, you might be accustomed to using glTexImage* functions, which are still supported, but create mutable storage textures.

In this chapter, we'll look at some basic and advanced texturing techniques. We'll start with the basics, just applying color textures, and move on to using textures as normal maps and environment maps. With environment maps, we can simulate things like reflection and refraction. We'll see an example of projecting a texture onto objects in a scene similar to the way that a slide projector projects an image. Finally, we'll wrap up with an example of rendering directly to a texture(using framebuffer objects (FBOs) and then applying that texture to an object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.121.242