# DirectX/10.0/Direct3D/Alpha Mapping

Alpha mapping in DirectX 11 is the process of using the alpha layer of a texture to determine the blending amount for each pixel when combining two textures.
To start the process you first create an alpha layer for a texture such as follows:
Each pixel is just a 0.0 to 1.0 float range indicating how to combine two textures. For example if the alpha value at a certain pixel is 0.3 you would take 30% of the base texture pixel value and combine it with 70% of the color texture pixel. In this tutorial we will combine the following two textures based on the alpha map:
The final result will be the following:
In this tutorial I will be separating the alpha map onto its own individual texture. This gives us the ability to create several alpha maps and then combine the same two color and base textures in many different ways.
The code in this tutorial is based on the previous tutorial.

## Alphamap.vs

The alpha map vertex shader is just the light map shader renamed from the previous tutorial.

## Alphamap.ps

The first change to the pixel shader is the addition of a third element in the texture array for holding the alpha map texture.
In the alpha map pixel shader we first take a sample of the pixel from the two color textures and alpha texture. Then we multiply the alpha value by the base color to get the pixel value for the base texture. After that we multiply the inverse of the alpha (1.0 - alpha) by the second color texture to get the pixel value for the second texture. We then add the two pixel values together and saturate to produce the final blended pixel.

## Alphamapshaderclass.h

The AlphaMapShaderClass is the LightMapShaderClass slightly modified from the previous tutorial.

## Alphamapshaderclass.cpp

The first change is that the alphamap.vs and alphamap.ps HLSL shader files are now loaded.
The alpha map vertex shader is loaded here.
The alpha map pixel shader is loaded here.
The next major change is that we now set three textures in the shader instead of two like in the previous tutorials.

## Texturearrayclass.h

The TextureArrayClass has been changed to handle three textures instead of two.
The number of elements in the texture array is changed to three.

## Texturearrayclass.cpp

The three textures are initialized to null in the class constructor.
The Initialize function now loads three textures into the texture array.
Shutdown now releases three textures.

## Modelclass.h

The ModelClass has been modified just slightly to handle three textures instead of two.

## Modelclass.cpp

I will only cover the functions that have changed since the previous tutorial.
The Initialize function now takes as input three texture names. The first two are the color texture and the third is the alpha texture.
LoadTextures now takes the three texture names as input.
The LoadTextures function now takes the three texture file names as input and then creates and loads a texture array using the three texture files. Once again the first two textures are the color textures and the third is the alpha texture.

## Graphicsclass.h

The new AlphaMapShaderClass header is now included in the GraphicsClass header file.
We create the new AlphaMapShaderClass object here.

## Graphicsclass.cpp

I will only cover the functions that have changed since the previous tutorial.
The new AlphaMapShaderClass object is initialized to null in the class constructor.
The ModelClass object is initialized with three textures. The first two textures are the color textures. The third input texture is the alpha texture that will be used to blend the first two textures.
The new AlphaMapShaderClass object is created and initialized here.
The new AlphaMapShaderClass object is released here in the Shutdown function.
The new AlphaMapShaderClass object is used to render the model object using alpha blending.

## Summary

Alpha mapping provides an easy way of controlling on a very fine level of how to combine textures. Many terrain based applications use this to provide smooth transitions between different textures over a very large landscape.

## To Do Exercises

1. Recompile and run the program to see the alpha mapped texture combination. Press escape to quit.
2. Make some of your own alpha maps and use them to combine the two textures in different ways.