
Chap 16/100: Terrain with FOG as LOD (Level of Detail - Part I)
What I will Learn here?:
On this tutorial we will learn how to render the terrain with a Fog color based on z-depth.
Note:
Try to move and see the "Fog" also try the F12 key to see on mini map the 2D view of our frustrum.
Let's check our current main source tree, except the LIBs:
Added source on White:
│ Applicationclass.cpp
│ Applicationclass.h
│ counter.h
│ main.cpp
│ main.h
│
├───camera
│ cameraClass.cpp
│ cameraClass.h
│ frustumClass.cpp
│ frustumClass.h
│ lightClass.cpp
│ lightClass.h
│ positionClass.cpp
│ positionClass.h
│ RenderFrustumClass.cpp
│ RenderFrustumClass.h
│
├───game
│ playerClass.cpp
│ playerClass.h
│
├───graphics
│ renderTextureClass.cpp
│ renderTextureClass.h
│ spriteClass.cpp
│ spriteClass.h
│ textClass.cpp
│ textClass.h
│ textFontClass.cpp
│ textFontClass.h
│
├───input
│ inputClass.cpp
│ inputClass.h
│
├───loader
│ objModelV2Class.cpp
│ objModelV2Class.h
│
├───shader
│ shaderClass.cpp
│ shaderClass.h
│
├───system
│ dx11Class.cpp
│ dx11class.h
│ SystemClass.cpp
│ SystemClass.h
│ xml_loader.cpp
│ xml_loader.h
│
└───terrain
bitmapclass.cpp
bitmapclass.h
Minimapclass.cpp
Minimapclass.h
quadtreeClass.cpp
quadtreeClass.h
terrainClass.cpp
terrainClass.h
terrainManagerClass.cpp
terrainManagerClass.h
(main.h)
- Set for Chapter 16
016terrain_fog.hlsl shader file using Fog with a basic Vertex Shader / Pixel Shader:
////////////////////////////////////////////////////////////////////////////////
// Filename: 016terrain_fog.hlsl
////////////////////////////////////////////////////////////////////////////////
#include "000cbuffer.hlsl"
//////////////
// TYPEDEFS //
//////////////
// VERTEX:
struct VSIn
{
float4 position : POSITION;
float2 tex : TEXCOORD0; //*5-TEXTURE
float3 normal : NORMAL; //*6-LIGHT
float4 color : COLOR; //*107-MIX COLOR
};
struct PSIn
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL; //*6-LIGHT
float4 color : COLOR; //*107-MIX COLOR
float fogFactor : FOG; //*FOG
};
/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture; //*5-TEXTURE
SamplerState SampleType; //*5-TEXTURE
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PSIn MyVertexShader016terrain_fog(VSIn input)
{
PSIn output;
float4 cameraPosition; //FOG
input.position.w = 1.0f; // Change the position vector to be 4 units for proper matrix calculations.
output.position = mul(input.position, WVP); // Calculate the position of the vertex against the world, view, and projection matrices.
output.tex = input.tex; // Store the texture coordinates for the pixel shader.
//LIGHT:
output.normal = normalize(mul(input.normal, (float3x3)worldMatrix));// Calculate the normal vector against the world matrix only.
output.color = input.color; // Send the color map color into the pixel shader.
//FOG:
cameraPosition = mul(input.position, WV); // FOG: Calculate the camera position.
output.fogFactor = ((fogEnd - cameraPosition.z) / (fogEnd - fogStart)); // Calculate linear fog.
return output;
}
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 MyPixelShader016terrain_fog(PSIn input) : SV_TARGET
{
float4 textureColor;
float lightIntensity; //6-
float4 color = ambientColor;
float4 fogColor = float4(0.5f, 0.5f, 0.5f, 1.0f); //FOG 1- Set the color of the fog to grey.
if (input.fogFactor > 0) {
// TEXTURE
textureColor = shaderTexture.Sample(SampleType, input.tex); // Sample the pixel color from the texture using the sampler at this texture coordinate location.
//LIGHT:
lightIntensity = saturate(dot(input.normal, lightDirection));// Calculate the amount of light on this pixel.
color += (diffuseColor * lightIntensity); // Determine the final diffuse color based on the diffuse color and the amount of light intensity.
color = color * textureColor; // The texture pixel is combined with the light color to create the final color result.
// COLOR
color = saturate(color * input.color * 2.0f); // Combine the color map value into the final color.
// FOG
color = input.fogFactor * color + (1.0 - input.fogFactor) * fogColor; // FOG: // 2- The fog color equation does a linear interpolation between the texture color and the fog color based on the fog factor input value.
return color;
} else {
clip (-1.0); return (float4)0; // this pixel is too far away (on fog), SKIP it! CLIP IF: (x is less than zero)
}
}
Project Code:
http://woma.no-ip.org/woma/WoMA_PartII_Chap16.zip
What's next?
On next tutorial we will see how to Render the terrain using a texture based on the slop angle.





