top of page

​​Chap 7/100: 2D IMAGES (orthogonal projection) for Sprites​ / Sky and Fixed Camera

​

What I will Learn here?:

​On this Chapter we will learn hot to render with a fixed camera and how to render sprites (2D Images)  with orthographic projection

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​​​Note:

This is the base of a 3D world infinite sky and Infinite Ground

​​

​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

│
├───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

 

​

​

​(main.h) ​

- Set for Chapter 7

​

​​​​​​​(sprite​Class​.cpp​)​ - ​This class have several utilities to handle sprites, like render fixed ones, render sprites with rotation, etc...

​

​

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

​

​Here we load the banner and we set the fixed camera on center of world 0,0,0:

​

​    // [7]  SPRITES: Load all our 2D objects
    //-----------------------------------------------------------------------------------------

#if TUTORIAL_CHAP >= 7
    #if defined (SCENE_BANNER)
    #if TUTORIAL_CHAP < 90 // Until Chhap 90 Its not a game engine, its only a DX 11 Engine
    if(!m_BannerSprite.Initialize(m_DirectX11, g_ScreenWidth, g_ScreenHeight, L"engine/data/007Bannerv2.png", 442, 49, 1.0f))   //DX 11 Engine
    #else
    if(!m_BannerSprite.Initialize(m_DirectX11, g_ScreenWidth, g_ScreenHeight, L"engine/data/BannerV2.png", 610, 50, 1.0f))      //WOMA Engine
    #endif//
        {MyMessageBox(L"Could not initialize the Banner.png");return false;}
    #endif

   
    // [8] New FIXED CAMERA for SkyBox and Position to attache our position to the other camera
    //-----------------------------------------------------------------------------------------

    m_CameraSKY.SetPosition(0.0f, 0.0f, 0.0f);
    #if defined (SCENE_CLEANSKY)
    skyModel.m_sky = true; // This will be a sky, so prepare to use a fixed camera (m_CameraSKY)
    #endif//
#endif//

​

​We need to setup our new matrix for orthognal projection instead Perspective projection ans we also set the camera for 2D at position 0,0,-1 a close look:

​

​#if TUTORIAL_CHAP >= 7
bool ApplicationClass::Presentation_2D()  {
    #if TUTORIAL_CHAP >= 7
    // [3] # DirectX11 # - TurnOnAlphaBlending and TurnZBufferOn and also Setup the "orthoMatrix"
    //-----------------------------------------------------------------------------------------

    m_DirectX11->TurnOnAlphaBlending();    // Turn on the alpha blending before rendering the text.
    m_DirectX11->TurnZBufferOn();
    m_DirectX11->GetOrthoMatrix(g_orthoMatrix);

    // [4] (Set CAMERA for "2D")
    //-----------------------------------------------------------------------------------------

    m_Camera.SetRotation(0, 0, 0);            // SET Camera Roration and Position to 2D Render: TEXT and SPRITES
    m_Camera.SetPosition(0.0f, 0.0f, -1.0f);
    m_Camera.Render();                        // Calculate: ViewMatrix
    m_Camera.GetViewMatrix(g_viewMatrix2D);    // Get the view from the camera and 2D objects.
    #endif//

    // Initialize and SHOW the "Presentation" Sprite (AFTER (Set CAMERA for "2D")):
    // ---------------------------------------------------------------------------------------------------

    //

    return true;
}
#endif

 

 

Loading the sky object:

 

    #if TUTORIAL_CHAP >= 7 && defined (SCENE_CLEANSKY)
    // Load all our SKY object
    //-----------------------------------------------------------------------------------------

        if (!skyModel.LoadObjModel (MY_SHADER_TEXTURE, m_DirectX11, L"engine/data/004sky_256_20_half.obj"))
            {MyMessageBox(L"Could not initialize the objModel");return false;}
        skyModel.m_world._11 = skyModel.m_world._22 = skyModel.m_world._33 = 0.5f;
        skyModel.m_world._42 -= 8; //Move Y: Join our Sky to Horizon
    #endif//

 

​

 

Rendering the sky object with our fixed camera:

​

​​    // 1. 3D RENDERING: SKY
    //----------------------------------------------------------------------------------------------------------------------

#if TUTORIAL_CHAP >= 7 && defined (SCENE_CLEANSKY)
    // Generate the view matrix based on the camera's position.
    m_CameraSKY.m_rotationY = m_Camera.m_rotationY;
    m_CameraSKY.m_rotationX = m_Camera.m_rotationX;
    m_CameraSKY.Render();
    m_CameraSKY.GetViewMatrix(g_viewMatrixSKY);    // Get the world, view, projection, and ortho matrices from the camera and SKY object.

    skyModel.render(pContext);
#endif//
​
 

 

And finally we finish rendering the banner as a Sprite Object:

 

    // 13. SPRITE: BANNER title
    //----------------------------------------------------------------------------------------------------------------------

    pContext->RSSetState(m_DirectX11->m_rasterStateCullBack[FILL_SOLID]); // RESET
#if TUTORIAL_CHAP >= 7 && defined (SCENE_BANNER)// BANNER 2D TITLE:
    IF_NOT_RETURN_FALSE ( m_BannerSprite.RenderRotY (pContext, (g_ScreenWidth - m_BannerSprite.m_bitmapWidth)/2, 0, m_scale) );
#endif//

 

 

 

 

​

​

​To navigate on source code:

 

User: public

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​

​Project Code:

​

 

WoMA_Chap7.zip

​

​

​

​What's next?

On next chapter we will check how to hand Text / Font files in our engine and we will update the horizon with the day and night...

bottom of page