top of page

​​Chap 12/100: Terrain with LIGHT

​

What I will Learn here?:

​On this Chapter we will learn how to load the same height map terrain but with light.

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​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
│
├───game
│       playerClass.cpp
│       playerClass.h​
│
​

├───graphics
│       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
        terrainClass.cpp
        terrainClass.h
        terrainManagerClass.cpp
        terrainManagerClass.h
​

​

​

​​(main.h) ​

- Set for Chapter 12

​

The Shader​ that we are using on this chapter is:

​​

012terrain_Light.hlsl shader ​file with a basic Vertex Shader / Pixel Shader:​

​

​
// Render 1 Full FRAME:
​

////////////////////////////////////////////////////////////////////////////////
// Filename: 012terrain_Light.hlsl
////////////////////////////////////////////////////////////////////////////////


#include "000cbuffer.hlsl"

//////////////
// TYPEDEFS //
//////////////


// VERTEX:
struct VSIn
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
};

// PIXEL:
struct PSIn
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
};

/////////////
// GLOBALS //
/////////////

Texture2D shaderTexture;
SamplerState SampleType;

////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////

PSIn MyVertexShader012terrain_Light(VSIn input)
{
    PSIn output;
   
    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.

    return output;
}

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////

float4 MyPixelShader012terrain_Light(PSIn input) : SV_TARGET
{
    float4 textureColor;
    float lightIntensity;
    float4 color = ambientColor;   

    textureColor = shaderTexture.Sample(SampleType, input.tex);     // Sample the pixel color from the texture using the sampler at this texture coordinate location.

    if (hasLight) {
        //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.

    } else {
        color = textureColor;                                 // The texture pixel is combined with the light color to create the final color result.
    }

    return color;
}   #endif
​
​

​Note:

This parameter buffer hasLigth will be used to render Water/Moon and Sun without ligth.

​

​​​​Project Code:

​

http://woma.no-ip.org/woma/WoMA_PartII_Chap12.zip

 

​What's next?

On next tutorial we will see how to add another texture source the color texture to our terrain

bottom of page