How it works...

The preceding code that loads the two textures into the OpenGL program is very similar to the code from the previous recipe, Applying a 2D texture. The main difference is that we load each texture into a different texture unit. When loading the brick texture, we set the OpenGL state such that the active texture unit is unit zero:

glActiveTexture(GL_TEXTURE0); 

And when loading the second texture, we set the OpenGL state to texture unit one:

glActiveTexture(GL_TEXTURE1); 

In the fragment shader, we specify the texture binding for each sampler variable using the layout qualifier corresponding to the appropriate texture unit. We access the two textures using the corresponding uniform variables, and store the results in brickTexColor and mossTexColor. The two colors are blended together using the built-in function mix. The third parameter to the mix function is the percentage used when mixing the two colors. We use the alpha value of the moss texture for that parameter. This causes the result to be a linear interpolation of the two colors based on the value of the alpha in the moss texture. For those familiar with OpenGL blending functions, this is the same as the following blending function:

glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 

In this case, the color of the moss would be the source color, and the color of the brick would be the destination color. Finally, we use the result of the mix function as the ambient and diffuse reflectivities in the Blinn-Phong reflection model.

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

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