Nineth Normal Mapping and displacement Mapping
This chapter focuses on two graphics techniques that support adding more detail to the scene without adding objects poly primitive. The first is the normal mapping, which simulates the illumination by creating some "fake" geometry (a dummy polygon entity). The second is displacement mapping, which is based on the texture data moving vertices actually (which corresponds to "fake", which refers to the real movement) to create a rugged surface.
Normal Mapping (normal map)
In the previous chapters, SPECUALR maps,environment maps and transparency maps have been discussed, and these texture maps provide additional data information. The data in specular maps is used to restrict specular highlight,environment maps contains surfaces for reflective colors,transparency Maps is used to control the alpha blending of the output-merge phase. These additional information is provided by each pixel and is more accurate than the one provided by each vertex. Similarly, a normal map also provides the number of normal vectors for each pixel, and this additional data can be applied to a variety of technologies.
One of the applications of Normal maps is to fabricate details of a concave and convex surface, such as a stone wall. You can use enough geometry to simulate this rugged wall, and the more vertices you use, the more detail you get. This allows for better handling of the light in the scene, and the stones away from the light are displayed as darker areas. However, increasing geometry can lead to increased computational costs. Conversely, for an object that contains a small amount of poly geometry (even a flat plane), using the normal map method can also simulate the same lighting effect as a large increase in geometry situations. This application is called normal mapping.
Normal Maps
Figure 9.1 shows a color map for a stone wall (left) and normal map (right). The map looks a little strange relative to the color map,normal. Although the NORML map can be displayed, the normal map stores 3D directions vectors (3-dimensional directional vectors). When the normal map is sampled, the results obtained from the RGB channel represent the x, Y, and z components of the direction vector. These normal vectors can be used for calculations of some effects, such as diffuse lighting.
Figure 9.1 A color map (left) and normal map (right) for A stone wall. (Textures by Nick
Zuccarello, Florida Interactive Entertainment Academy.)
Each channels of the RGB texture stores a unsigned 8-bit value (unsigned char), which has a numeric range of [0, 255]. But a normalized direction vector, the corresponding XYZ component value range is [-1, 1]. Therefore, the normal vector must first be converted to the range [0, 255] before it is stored in the texture, and the texture must be converted to the range [-1, 1] when sampling. You can use the following formula to convert a floating-point vector from a range [-1, 1] to a range [0, 255]:
f (x) = (0.5x + 0.5) * 255
Then use the following equation to transform it back:
In practice, an image processing software, such as Adobe Photoshop, is typically used to encode a normal map into an RGB texture format. However, the sampling texture needs to be calculated manually in shader, converting the data from the range [0, 255] back to [-1, 1]. A floating-point division (divided by 255) operation has been performed during the sampling process, so the resulting sampling result is between the range [0, 1]. Therefore, you only need to use the following equation function to convert the range [0, 1] to the range [1, 1]:
f (x) = 2x? 1
Alternatively, you can use a 16-bit or 32-bit floating-point value as the texture format for normal maps, which results in better detail, but at the expense of some performance.
Tangent space (tangent spaces) generally use each of the vertex normals to calculate the diffuse, as well as use each of the pixel normals. But the normals must be in the same coordinate space as light. For Per-vertex normals, provided by object space. However, normal maps has a normals value of tangent space.
Tangent space (or texture space) is a texture-relative coordinate system, determined by three orthogonal vectors: surface normal vector, Tangent vector, and binormal vector. Figure 9.2 depicts these three vectors.
Figure 9.2 An illustration of tangent space. (Texture by Nick Zuccarello, Florida Interactive Entertainment Academy.)
Where the normal vector, N, is a vertex surface normal vector. Tangent vector, T, perpendicular to the normal of the surface, with the direction of the U-axis pointing to texture. The binormal vector, B, points to the V-axis direction of the texture.
You can use these three vectors to create a TBN (tangent, binormal, normal) transformation matrix, as follows:
You can use this matrix to transform vectors from tangent space into object space. However, since light vectors are usually in world space, it is necessary to transform the Noraml sampled from the normal map from tangent space to world space. Alternatively, create a TBN matrix directly using a vector that is already in world space.
Note
By encoding normals directly into world space, you can eliminate the transformation from tangent space to world space. However, objects that use these normals can only remain stationary (cannot perform transformations). In addition, these normals cannot simply be reused among multiple ojbects (because these normals can no longer be transformed).
The TBN matrix has a very interesting feature, which consists of three orthonormal vectors (orthogonal unit vectors) that form an orthogonal base (which defines a coordinate system). It is also said that the matrix is an orthogonal matrix, and the inverse of the orthogonal matrix equals its transpose. So, to transform a vector from object or world space back to tangent space (inverse mapping reverse mapping), simply multiply the vector with the transpose of the TBN matrix. In addition, according to the orthonormal nature of the TBN matrix, if any two vectors are known, a third vector can be deduced. Normally, the normal and tangent vectors are stored with the geometry, while the binormal vector (in vertex shader) is computed by cross product of the two vectors. Performing this calculation is a tradeoff between GPU operations and the high cost of data transfer (between the CPU and the GPU).
A Normal Mapping Effect
Based on the method discussed above, listing 9.1 lists a code for the normal mapping effect.
List 9.1 normalmapping.fx
#include "include\\common.fxh" Cbuffer cbufferperframe{float4 ambientcolor:ambient < string uiname = "Ambi ENT Light "; String uiwidget = "Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; FLOAT4 Lightcolor:color < string Object = "LightColor0"; String uiname = "Light Color"; String uiwidget = "Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; FLOAT3 Lightdirection:direction < string Object = "DirectionalLight0"; String uiname = "Light Direction"; String Space = "World"; > = {0.0f, 0.0f, -1.0f}; FLOAT3 Cameraposition:cameraposition < string uiwidget= "None"; A.;} Cbuffer cbufferperobject{float4x4 worldviewprojection:worldviewprojection < string uiwidget= "None"; >; float4x4 World:world < string uiwidget= "None"; >; FLOAT4 Specularcolor:specular < string uiname = "Specular Color"; String uiwidget = "Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; Float SpEcularpower:specularpower < String uiname = "Specular Power"; String uiwidget = "Slider"; float Uimin = 1.0; float Uimax = 255.0; float Uistep = 1.0; > = {25.0f};} Texture2d Colortexture < string resourcename = "Default_color.dds"; String uiname = "Color Texture"; String resourcetype = "2D";>; Texture2d Normalmap < string resourcename = "Default_bump_normal.dds"; String uiname = "Normap Map"; String resourcetype = "2D";>; Samplerstate trilinearsampler{Filter = min_mag_mip_linear; Addressu = WRAP; ADDRESSV = WRAP;}; Rasterizerstate disableculling{Cullmode = none;};/ Data Structures *************/struct vs_input{float4 objectposition:position; FLOAT2 Texturecoordinate:texcoord; FLOAT3 Normal:normal; FLOAT3 tangent:tangent;}; struct vs_output{float4 position:sv_position; FLOAT3 Normal:normal; FLOAT3 tangent:tangent; FLOAT3 Binormal:binormal; FloAT2 texturecoordinate:texcoord0; FLOAT3 Lightdirection:texcoord1; FLOAT3 viewdirection:texcoord2;};/ Vertex Shader *************/vs_output Vertex_shader (vs_input in) {vs_output out = (vs_output) 0; Out. Position = Mul (in. Objectposition, worldviewprojection); Out. Normal = Normalize (Mul (FLOAT4 (in. Normal, 0), world). xyz); Out. Tangent = Normalize (Mul (FLOAT4 (in. Tangent, 0), world). xyz); Out. Binormal = Cross (out. Normal, out. Tangent); Out. Texturecoordinate = Get_corrected_texture_coordinate (in. Texturecoordinate); Out. Lightdirection = normalize (-lightdirection); FLOAT3 worldposition = Mul (in. Objectposition, World). XYZ; FLOAT3 viewdirection = cameraposition-worldposition; Out. Viewdirection = normalize (viewdirection); return out;} /************* Pixel Shader *************/float4 pixel_shader (vs_output in): sv_target{float4 out = (float4) 0; FLOAT3 Samplednormal = (2 * normalmap.sample (Trilinearsampler, in. Texturecoordinate). xyz)-1.0; Map normal from [0..1] to [ -1..1] float3x3 TBN = float3x3 (in. Tangent, in. Binormal, in. Normal); Samplednormal = Mul (Samplednormal, TBN); Transform Normal to World space float3 viewdirection = normalize (in. Viewdirection); FLOAT4 color = colortexture.sample (Trilinearsampler, in. Texturecoordinate); FLOAT3 ambient = get_vector_color_contribution (Ambientcolor, COLOR.RGB); Light_contribution_data Lightcontributiondata; Lightcontributiondata.color = Color; Lightcontributiondata.normal = Samplednormal; Lightcontributiondata.viewdirection = viewdirection; Lightcontributiondata.lightdirection = Float4 (in. Lightdirection, 1); Lightcontributiondata.specularcolor = Specularcolor; Lightcontributiondata.specularpower = Specularpower; Lightcontributiondata.lightcolor = Lightcolor; FLOAT3 light_contribution = get_light_contribution (Lightcontributiondata); Out.rgb = ambient + light_contribution; OUT.A = 1.0f; return out;} /************* techniques *************/technique10 main10{pass P0 {SetVertexShader (Compileshader (Vs_4_0, V Ertex_shader ())); Setgeometryshader (NULL); SetPixelShader (Compileshader (Ps_4_0, Pixel_shader ())); Setrasterizerstate (disableculling); }}
Normal Mapping Preamble in the effect, a ambient light,specular highlight and a directional light were used first. A new Texture2d object is added to represent the normal map, and the vs_input structure contains a surface normal and a tangent vector (in object space). New members representing tangent and binormal are added in Vs_ouput and need to be transformed into world space between the next pipeline stages. Normal Mapping Vertex and Pixel shader in Vertex shader, the normal and tangent vectors are first transformed into world space, and then the cross of the two vectors is computed. Product gets the binormal vector. In pixel shader, the normal map is sampled first, and the normal vector sampled is converted from range [0, 1] to range [-1, 1]. Then create the TBN matrix and use the matrix to transform the normal vector of the sample into world space. When normal is transformed into world space, the next lighting calculation is exactly the same as before.
Note
If you find that vertex shader is a performance bottleneck, you can provide bionormal directly in the input parameters (along with surface normal and tangent). This is a tradeoff for balancing the vertex shader performance and the cost of data transfer in the graphics bus. More generally, the graphics pipeline becomes a bottleneck.
Normal Mapping output Figure 9.3 Shows the result of using the normal Mapping effect on a plane with a stone wall texture. The normal map in Figure 9.1 is used in the diagram on the left. In the image on the right, a normal map is used but has no effect. In both pictures, ambient light is disabled, direcitonal light is pure white, full-intensity (Strength value is 1.0), specular highlight power value is 100, The intensity value is 0.35.
Figure 9.3 Normalmapping.fx applied to a plane with a stone wall texture using a normal
Map (left) and without a normal map (right). (Textures by Nick Zuccarello, Florida Interactive
Entertainment Academy.)
Nineth Normal Mapping and displacement Mapping