top of page

​​Chap 10/100​The Moon and the Sun with Billboards

​

What I will Learn here?:

​On this Chapter we will learn how to render two basic billboards as Moon at North and the Sun at Este side.

Later on Chap 12, we will use the Sun as our Light Source

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​​​Note:

Try move you self and discover also the Sun at Este side when it's Day.

Swap between Day and Night are presentlly for demos propose at 5 seconds.

​​

​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
│       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 10

​

​​​​​​​​​​​​​​(applicationClass.cpp)​ - CHANGES

​

​

​Here we initialize our Billboards for the Moon and for the Sun:

​
    // Initialize all 3D objects:
    // ---------------------------------------------------------------------------------------------------

    #if TUTORIAL_CHAP >= 10 && defined (SCENE_SUN_MOON)
    // Load our 1st 3D object
    if (!m_MoonBill.LoadObjModel (MY_SHADER_BLEND_TEXTURE_COLOR, m_DirectX11, L"engine/data/010MoonSquare.obj", true))
        {MyMessageBox(L"Could not initialize the objModel");return false;}
    m_MoonBill.m_sky = true; // Use fixed camera

    // Load our 1st 3D object
    if (!m_SunBill.LoadObjModel (MY_SHADER_BLEND_TEXTURE_COLOR, m_DirectX11, L"engine/data/010SunSquare.obj", true))
        {MyMessageBox(L"Could not initialize the objModel");return false;}
    m_SunBill.m_sky = true; // Use fixed camera
​

​

​

The rendering part its also very easy depending on the "currentSky" variable (for Day and Night):

 

    // 1. 3D RENDERING: MOON or SUN as a Single Billboard
    //----------------------------------------------------------------------------------------------------------------------

#if TUTORIAL_CHAP >= 10 && defined (SCENE_SUN_MOON)
   
    D3DXMATRIX translateMatrix = g_identMatrix;
    if (currentSky == 1)
    {
        translateMatrix._41 = 0;
        translateMatrix._42 = 4;
// Height of Moon on horizon
        translateMatrix._43 = g_SCREEN_DEPTH/6;
// Set Moon at "North"

        D3DXMatrixRotationY(&m_MoonBill.m_world, atan2(translateMatrix._41, translateMatrix._43));
        D3DXMatrixMultiply(&m_MoonBill.m_world, &m_MoonBill.m_world, &translateMatrix);        // Finally combine the rotation and translation matrices
        m_MoonBill.render(pContext);
    } else {
        translateMatrix._41 = g_SCREEN_DEPTH/6;
// Set Sun at "Este"
        translateMatrix._42 = 12;
// Height of Sun on horizon
        translateMatrix._43 = 0;

        m_DirectX11->TurnOnAlphaBlending(); //need to mask Sun
        D3DXMatrixRotationY(&m_SunBill.m_world, atan2(translateMatrix._41, translateMatrix._43));
        D3DXMatrixMultiply(&m_SunBill.m_world, &m_SunBill.m_world, &translateMatrix);        // Finally combine the rotation and translation matrices
        m_SunBill.render(pContext, 0, false);
        m_DirectX11->TurnOffAlphaBlending(); //not needed any more
    }
#endif//

 

 

​

​

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

​​

010TextureBlendedColor.hlsl shader ​file with a basic Vertex Shader / Pixel Shader:​

​

​​

​////////////////////////////////////////////////////////////////////////////////
// Filename: 008TextureBlendedPixelColor.hlsl
////////////////////////////////////////////////////////////////////////////////


#include "000cbuffer.hlsl"

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


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

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

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

Texture2D shaderTexture;
SamplerState SampleType;

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

PSIn MyVertexShader010TextureBlendedColor(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.
   
    return output;
}

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

float4 MyPixelShader010TextureBlendedColor(PSIn input) : SV_TARGET
{
    float4 color;
   
    // Sample the texture pixel at this location.
    color = shaderTexture.Sample(SampleType, input.tex);

    if(color.r == 0.0f && color.g == 0.0f && color.b == 0.0f)    // If the color is black on the texture then treat this pixel as transparent.
    {    clip (-1.0); return (float4)0;    }                       

    return color;
}


 

​

​To navigate on source code:

 

User: public

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​Project Code:

​

​WoMA_Chap10.zip

​

​What's next?

The basics are done, at this Tutorial Part I, on Next Part II, we will cover several "Height Map" terrain types​.

That is the base of any Game Engine! Curious? go go to Part II

bottom of page