In this tutorial, we will add texture ing to the Terrain Based on illumination and the water surface program, and we will dynamically change the texture coordinates of the water surface based on time to achieve the effect of water surface texture fluctuation.
The terrain (Valley) and water surface are all grid-based planes.
For terrain, modify the vertex type:
Struct vertextype
{
D3dxvector3 position;
D3dxvector3 normal;
D3dxvector2 texture; // texture coordinate
D3dxvector4 KD; // diffuse reflection coefficient of the material
D3dxvector4 KS; // The highlight coefficient of the material.
};
Assuming m and n are the number of rows and columns in the grid, the texture coordinates are calculated as follows:
Float du = 10.0f/(n-1 );
Float DV = 10.0f/(m-1)
...
// Calculate the texture coordinate.
Vertices [I * n + J]. texture. x = J * du;
Vertices [I * n + J]. texture. Y = I * DV;
In Du and DV, 10.0f is used to increase the texture coordinates by 10 times. texture coordinates greater than [0, 1] are used in the wrap method, so that in a grid, you can use textures multiple times to avoid a large grid, while our texture images are relatively small, so as to avoid excessive magnification.
The following two figures compare the results with 10.0 and 2.0. the first figure is 2.0:
The texture coordinate calculation method of the water surface is basically the same as that of the valley, but we will dynamically change the coordinates based on DT in the update function, which will achieve the effect of water surface drift.
// Update the vertex buffer.
Float du = 5.0f/(m_NumCols-1 );
Float DV = 5.0f/(m_NumRows-1 );
// We dynamically calculate texture coordinates based on time
Static float mwatertexoffsetx = 0;
Static float mwatertexoffsety = 0;
Mwatertexoffsety + = 0.1f * DT;
Mwatertexoffsetx = 0.25f * sinf (4.0f * mwatertexoffsety );
...
Vertices [I * m_numcols + J]. texture. x = J * du + mwatertexoffsetx;
Vertices [I * m_numcols + J]. texture. Y = I * DV + mwatertexoffsety;
Of course, we also use a matrix to scale, translate, and rotate texture coordinates. In this way, we need to define a shader constant matrix and use it in the shader to multiply the texture coordinates, achieve the desired effect. Texture rotation is often used to achieve the effect of dynamic particles, such as flame.
Finally, we call the illumination texture rendering class in graphicsclass to get the final result:
// Use light shader texture for rendering
Result = m_lighttexshader-> render (m_d3d-> getdevicecontext (), m_model-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix,
Light, material, camera, m_texmanager-> createtex (m_d3d-> getdevice (), string ("grass. Dds ")));
...
Result = m_lighttexshader-> render (m_d3d-> getdevicecontext (), m_watermodel-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix,
Light, material, camera, m_texmanager-> createtex (m_d3d-> getdevice (), string ("water2.dds ")));
Complete code can be found:
Project File mytutoriald3d11_38
Download Code:
Http://files.cnblogs.com/mikewolf2002/d3d1127-28.zip
Http://files.cnblogs.com/mikewolf2002/pictures.zip