CS315 Lab 5: Texture Mapping

This lab is an introduction to Texture Mapping.

Assignment:

After the lab lecture, you have until the start of next lab to:

  • Use your own image for texture mapping
  • Use repeating and clamped textures
  • Make changes to texture coordinates to achieve certain effects
  • Attempt a multi-texturing bonus exercise

A. Pictorial Overview and Definitions

The following diagram represents the idea of texture mapping:

Some definitions:

  • Texture Mapping—a method of mapping an image onto geometry to achieve a higher level of detail. It has been around since the earliest years of computer graphics and has become one of its most important techniques.

    "The original technique was pioneered by Edwin Catmull in 1974 as part of his doctoral thesis.
    "Texture mapping originally referred to diffuse mapping, a method that simply mapped pixels from a texture [texels] to a 3D surface ('wrapping' the image around the object). In recent decades, the advent of multi-pass rendering, multitexturing, mipmaps, and more complex mappings such as height mapping, bump mapping, normal mapping, displacement mapping, reflection mapping, specular mapping, occlusion mapping, and many other variations on the technique (controlled by a materials system) have made it possible to simulate near-photorealism in real time by vastly reducing the number of polygons and lighting calculations needed to construct a realistic and functional 3D scene."
    —Source: Wikipedia

    We will primarily focus on diffuse mapping but this week's bonus will involve multitexturing to control specular and ambient colours as well.

  • Textures—rectangular arrays of data. Historically the data was color, luminance, or color and alpha data. Since programmable shaders became popular, 3D programmers have become very creative with how they use textures.
  • Texels—the individual values in a texture array
  • Mipmaps—Although we will not be covering this in detail in this lab. Mipmaps are often discussed with texture mapping. It's something good to know.

    The idea is that you have multiple images to cover multiple levels of detail. For instance, for an object far away, you have a texture or a mipmap that is small and with little detail, and for closer objects, you have a texture that is large and detailed.

    Taking an example from msdn (Search for EasyTex). If you are looking at a stop sign from far away, you see only a red circle. As you get closer, you can see it is a red shape with some letters. Finally, you see the entire stop sign. You might specify the mipmaps to look something like this:

    If you did not specify different levels of detail, WebGL would have to decide. It would combine the red sign with the white letters, and you would get a pink blob instead of a round, red circle.


B. Coding Overview

Download

For the following discussion you may want to download Lab5.zip.

The Steps in Texture Mapping are the following

  1. Create a texture name and bind it to the type of texture we will be creating
  2. Specify texture properties and the texture image
  3. Enable texture mapping
  4. Provide the mapping between texture coordinates and the object's coordinates.

B1. Texture names

To begin with, we need a texture name. OpenGL and OpenGL ES use the glGenTextures() command to assign a name (index number - like a pointer) to one or more texture objects. WebGL uses the similar createTexture() command. It creates one texture name as if glGenTextures() was called, and uses it to initialize and return a WebGLTexture object. The code to generate a new WebGLTexture object is as follows:

  
    let texName;
    //allocate a texture name
    texName = gl.createTexture();
    

Now that we have a texture name, we neet to specify some information about the type of texture we are working with. We do this with bindTexture(). There are four types of textures in WebGL: 2D, cube map, 3D and 2D array. In this lab, we will be working with 2D textures, which is indicated by the gl.TEXTURE_2D in the following code:

  
    //select our current texture
    gl.bindTexture(gl.TEXTURE_2D, texName);

  

Calling gl.bindTexture() for the first time with texName, initializes the texture object as a 2D texture with some related default values or properties. Once you bind a texture name as either 2D or cube map, you must always bind it as such or you will get warnings and errors. The other properties of the texture can be modified while it is bound.

We will call gl.bindTexture() again later with this same texName. When gl.bindTexture() is called with a previously created texture object, that texture object becomes active. Therefore, the next call to gl.bindTexture() will make texName the current texture.

B2. Texture Properties and the Image

We've got a texture name and we've created a texture with the bindTexture() command, now we can specify some properties or parameters for the texture. If we don't specify any parameters, the default values will be used.

The following may be some parameters that we want to set. The comments provide some idea as to what each of these calls is doing. For more details about these parameters and their defaults, you should consult the MDN page for texParameter[if] .

  
    //The texture wraps over the edges (repeats) in the x direction
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);

    //The texture wraps over the edges (repeats) in the y direction
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

    //when the texture area is large, repeat texel nearest center
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

    //when the texture area is small, repeat texel nearest center
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);


Next, we can specify the image we are going to use. Assuming that we have an image (checkImage) which is a 2 dimensional checkerboard pattern stored in an array containing RGBA components, we can set the texture to the checkImage with the following code:

  
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, checkImageWidth, checkImageHeight, 
                             0, gl.RGBA, gl.UNSIGNED_BYTE, checkImage); 

See the MDN page for texImage2D for function details.

B3. Switching Textures

While you are drawing, you need to specify which texture you are going to be working with. You can have multiple textures loaded into your Rendering Context. You switch between them by calling gl.bindTexture. Remember, the first time you call gl.bindTexture it creates a texture object with default values for the texture image and texture properties. You can then load a texture and configure it. The second time you call it with the texture name, that texture data becomes the current texture state.

  
    gl.bindTexture(gl.TEXTURE_2D, texName);

B4. Provide the Mapping between Texture Coordinates and the Object's Coordinates.

You have to specify which part of our texture image are going to fit or map onto the object. Texture coordinates are often referred to as s and t. For a texture image, s and t are in the range of 0 to 1. The mapping itself is similar to setting a color for each vertex. To map the checkerboard texture onto a square, we would provide vertex and texture coordinate arrays that would look like this:

  
let points = 
[
  //square
	-2.0, -1.0,  0.0,
	 0.0,  1.0,  0.0,
	-2.0,  1.0,  0.0,

	-2.0, -1.0,  0.0,
	 0.0, -1.0,  0.0,
	 0.0,  1.0,  0.0,
];

let texCoords = 
[
  //square
	0.0,  0.0,
	1.0,  1.0,
	0.0,  1.0,

	0.0,  0.0,
	1.0,  0.0,
	1.0,  1.0,
];

Graphically, we could see the mapping as the following:

B5. A Simple Texture Mapping Shader

To add texture mapping capability to your shader program, you need to do the following:
  1. New technique: Connect active textures to the shader. You have to tell the shader what texture image unit(s) your texture(s) is (are) bound to. This information is sent to a special type of uniform in the fragment shader called a sampler.
  2. Send the texture coordinates to the vertex shader. Do this like sending vertices, colors and normals.
  3. Pass the texture coordinates to the fragment shader. This is like passing a color or vertex position, except texture coordinates usually have 2D coordinates (vec2).
  4. New technique: Look up texture values in the shader. In the fragment shader you use the GLSL function texture to look up a value in the texture sampler at the interpolated texture coordinates.
Let's look at the new techniques in a little more detail:

Connect Active Textures to the Shader

To use a texture you must declare a uniform of type sampler* in your fragment shader. Samplers come in 2D and cube flavours. You will be using sampler2D. The sampler is used to help look up values in a texture correctly. Each sampler in your shader program will be connected to a single texture image unit. WebGL 2.0 supports a minimum of 16 simultaneous texture samplers. For starters you will only be using one texture image unit, and that is unit 0, but you can find out your system's limit with this javacode: console.log(gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS));.

In your fragment shader:
  
uniform sampler2D tex;

In your OpenGL code:
  
    //The default texture image unit is unit 0
    gl.uniform1i(gl.getUniformLocation(program, "tex"), 0);

Look Up Texture Values in the Shader

You use the special GLSL function called texture to look up values in a texture. You provide it with a sampler which is connected to a texture image unit that has been configured to look up values in a certain way from a specific texture image. The value to look up is controlled by the texture coordinates. Consider the following example from the Checkers demo:
In your fragment shader:
  
#version 300 es
precision mediump float;
        
in vec2 TexCoord;
uniform sampler2D tex;
out vec4 fragColor;

void main() 
{ 
	fragColor = texture(tex, TexCoord);
}

Multi-Texturing

As you write more advanced programs, you may wish to use more than one texture at the same time to achieve certain effects such as adding detail, bump mapping or gloss mapping. To do this you create, bind, set parameters, and load data into multiple texture names, then you bind them to multiple texture image units and send their numbers to the shader like this:

In texture init phase
  
    //Configure two separate texture names, just like for simple texturing
     .
     .
     .
    //Connect the texture units you plan to use to samplers in the shader
    gl.uniform1i(gl.getUniformLocation(program, "tex0"), 0);
    gl.uniform1i(gl.getUniformLocation(program, "tex1"), 1);

  
While drawing
  
    //Bind texture names to texture units
    gl.activeTexture(gl.TEXTURE0); //switch to texture image unit 0
    gl.bindTexture(gl.TEXTURE_2D, textures[0]);   //Bind a texture to this unit
    gl.activeTexture(gl.TEXTURE1); //switch to texture image unit 1
    gl.bindTexture(gl.TEXTURE_2D, textures[1]);   //Bind a different texture to this unit

   //Draw textured item
   ...
   

Note that gl.activeTexture takes OpenGL constants of the form gl.TEXTUREn whereas you only send the number, n, of the texture unit to the sampler in the shader. WebGL 2.0 specifies a minimum of 16 texture units, so n can be a value from 0 through 8. The number can be higher if your implementation supports it, which is likely — even my laptop with Intel integrated graphics supports 16.



C. Repeating a Texture Pattern or Clamping It

In the example that we are using, we have specified that we would like to repeat the pattern, but we are currently not making use of this repeating feature. When we repeat a texture, it provides a "tile" effect. The following code specifies that we will repeat the texture map.

  
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
    
 

But in order to see any results, we have to assign texture coordinates outside the range [0,1]. For instance to get a 3x3 tile of the checkerboard on our square, we would specify the texture coordinates like this:

  
let texCoords = 
[
    //square
	0.0,  0.0,
	3.0,  3.0,
	0.0,  3.0,

	0.0,  0.0,
	3.0,  0.0,
	3.0,  3.0,
];

Try it out!

Graphically, we could see the mapping as the following (the red lines are placed in the texture map to help show that the texture is repeated three times)

Instead of specifying that we want to repeat the texture, we can specify that we want to clamp or mirror repeat the texture. In this case, we change the gl.REPEAT to gl.CLAMP_TO_EDGE or gl.MIRRORED_REPEAT. Here's an example of clamping:

  
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 
   gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 

What does this mean? Any values greater than 1.0 are set to 1.0, and any values less than 0.0 are set to 0.0. For instance, if we use gl.CLAMP_TO_EDGE with the above texture coordinates, then a small copy of the texture will appear in the bottom left-hand corner of the object. To the right will be a "clamp" of the texture at s=1. To the top, will be a clamp of t=1.

Graphically, we could see the mapping as something like this (again, the red lines are in the image to point out the original texture in the bottom left-hand corner).


D. Working with Image Files

The checkerboard pattern in the above example was a simple black and white image that we stored into an array. It is possible to generate other images procedurally in various ways, but you may want to use more complicated pictures — for instance art or photographs.

WebGL allows you to easily load any image supported by your browser, including images created in other canvases, with a modified version of texImage2d. It is documented in the WebGL specification, but has no equivalent in OpenGL ES. Here is how you would typically use it.

First you create an image tag. You can do it programatically with javascript, but it is easier to make a hidden img tag with an ID like this:

In your HTML file:
          
<img src="pic.png" id="mypic" hidden />

Then, you request the picture by its ID and send it to texImage2d as an argument, like this:
In your .js file:
          
    // WebGL often loads images upside down. This will correct that.
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,1);

    // Get the IMG tag by its ID
    let mypic = document.getElementById("mypic");
    
    // Load data from the IMG
    gl.texImage2D(gl.TEXTURE_2D, 
                  0,                //mip map level
                  gl.RGBA,          //internal texel format (in graphics memory)
                  gl.RGBA,          //external texel format (in system memory)
                  gl.UNSIGNED_BYTE, //external texel data type
                  mypic             //image data source - IMG tag, ImageData object, 
                                    //  HTMLCanvasElement or HTMLVideoElement 
                                    //  (frame number required)
                  );

        
Please Note:

Although modern OpenGL and WebGL2 support images of arbitrary sizes, WebGL 1 is like older OpenGL. In some cases they only allow images that have both a width and height that are a power of 2. For instance, valid sizes would be: 64 x 16, 512x512, 256 x 256, 128 x 128, and 16 x 4. Some invalid sizes would be: 100 x 100, 6 x 4, 5 x 5, and 2 x 22.

Compare and contrast the Notes for OpenGL ES 2 and 3 texParameteri() functions:


E++ OPTIONAL: Mipmap Sample

If you animate the square and triangle so they move toward and away from the camera by uncommenting the first couple lines of render:

In checkers.js:render()
              
function render()
{
    //Uncomment next two lines for distance changing animations
    //to demonstrate MIN and MAG filters
    d = Math.sin(a)*14-11.5;
    a+= .01;
    
    //...
}
              
            

You will be able to observe aliasing artifacts when the size of a texel falls below the size of a pixel. No combination of gl.LINEAR or gl.NEAREST min filters can adequately fix this. The smaller the texels, the worse the problem.

You can make smaller versions of the picture that pre-blur or alter the texture so it will look better when it is small. One simple way to do this is to use gl.generateMipmap() and then switch the min filter to one of the mipmap filters like this like this:

In checkers.js:init()
            
function init()
{
  //...

  //Bind the texture
  gl.bindTexture(gl.TEXTURE_2D, texNames[0]);

  //Set texture parameters
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);

  //Load texture data into WebGL from System RAM
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, checkImageWidth, checkImageHeight,
                0, gl.RGBA, gl.UNSIGNED_BYTE, checkImage);
  gl.generateMipmap(gl.TEXTURE_2D);
  
  //...
}
            
          

Try experimenting with other min filters too. Each of the NEAREST parts can be swapped for LINEAR. The first one is like a regular min filter, the second is for blending mipmap levels.

If you have a set of custom images, you must define one for each level from full resolution (level 0) down to 1x1. Each new level needs to be half the size of the previous. Your data folder has sample mipmaps from 32x32 to 1x1. Add them to your code to see how they work.

In checkers.html:init() add these hidden images:
            
              <img src="Data/32x32.png" id="mipmap0" hidden />
              <img src="Data/16x16.png" id="mipmap1" hidden />
              <img src="Data/8x8.png" id="mipmap2" hidden />
              <img src="Data/4x4.png" id="mipmap3" hidden />
              <img src="Data/2x2.png" id="mipmap4" hidden />
              <img src="Data/1x1.png" id="mipmap5" hidden />
            
          
In checkers.js:init() remove the old image load and replace it with the marked code
            
              //Load texture data into WebGL from System RAM
              //gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, checkImageWidth, checkImageHeight,
              //    0, gl.RGBA, gl.UNSIGNED_BYTE, checkImage);
              //gl.generateMipmap(gl.TEXTURE_2D);
              gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,1);
              for (let i = 0; i < 6; i++) {
                  let img = document.getElementById("mipmap"+i);
                  gl.texImage2D(gl.TEXTURE_2D, i, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
              }
            
          

E. References

The following are a list of references which were used in the making of this lab:


F. Exercise

Goals:

  • Edit your own picture and make it a texture map
  • Edit texture coordinates for the cube and octahedron shapes
  • Add keys to choose between:
    • three textures: D8, NeHe and your own
    • cube and octahedron
    • CLAMP_TO_EDGE vs REPEAT texture behaviour

This week's lab exercise was originally based on Lesson 6 from NeHe OpenGL tutorials. It has been mostly rewritten, but still uses some textures provided in that tutorial.

Modify the code by following these steps:

  1. First, run the code to see how it works. You should see a cube with a NeHe logo. You can use the 'a' key to toggle a spinning animation.
  2. Pick one of your favorite images and use an image editor to crop a portion of an image and save it in the web image format of your choice (jpg, png or gif are probably best). Save the image with a height and width that would always work in WebGL 1. Be prepared to tell my why this image's dimensions are "safe".
  3. Edit the code in Lab6Exercise.js so that you can switch between NeHe.jpg, D8.jpg and your picture by pressing keys. Do NOT reload textures using texImage2D every time you switch - only rebind textures you have already loaded.
  4. In render, set the currently bound texture's gl.TEXTURE_WRAP_S and gl.TEXTURE_WRAP_T to gl.REPEAT and modify the cube's texture coordinates so that your image in a 3x3 grid on the surface of the cube (similar to the image below). Try to make it so the (0,1) coordinates on both S and T axes are in the center of the cube.
  5. Add a global variable to represent the texture wrap mode set in render, and add a key to toggle it between gl.CLAMP_TO_EDGE and gl.REPEAT. Switch back and forth between the two modes to confirm that the cube shows a 3x3 grid in repeat mode and a centered image in clamp mode.
  6. Add a key to swap between drawing solidOctahedron and solidCube. Fix the texture coordinates for solidOctahedron in Lab5/shared_shapes.mjs so the D8.png image shows one number per face, like a dice. It's OK if the other two images look scrambled on the octahedron. We've already provided lookups for you - their expected locations on the repeated texture map are shown in the image below:
BONUS: Specular Mapping and Multitexturing

In the Bonus folder, notice that the fragment shader receives three colours from the vertex shader - diffuse, ambient, and specular. All are added together then multiplied by the texture colour. This hides specular highlights when the texture is dark. If you only multiply the texture against the diffuse and ambient colour, you can see the specular highlight across the surface of the whole cube. It is possible to use a multiple textures to control diffuse, ambient and specular colour independently. This can produce some interesting effects.

To do this bonus:
  • Note the names of the texture samplers in the fragment shader.
  • In setupTextures() add appropriate calls to connect texture units to the three samplers.
  • Multiply diffuse, ambient and specular by the results from their respective samplers, and add them all together.
  • Pick either Cube or Octahedron and load appropriate textures to separate texture names. Tips:
    • Cube: All three textures are intended to be used with full white in their base material property.
    • Octahedron: There's no ambient texture, so reuse the diffuse texture. Set their base material properties to .8 white for diffuse, and .2 white for ambient.
  • Just before drawing the box or octahedron, bind the textures to the appropriate texture units.
  • If you are confused, ask your lab instructor to demonstrate the desired effect.
  • Extra bonus: get both the Octahedron and Box working and provide a key to switch between them.

Deliverables

  • Completed Exercise with keys that allow all three textures to be applied to both a cube and octahedron in clamped or repeat mode.
  • Short explanation for why your custom texture would work in WebGL 1 (and why D8.png might not always work)
  • Optional: Indicate whether you completed any of the bonus work, and what part you did.
  • A .zip file containing your entire Lab5 folder.