top of page

Chap 14/100: Terrain QUAD + FRUSTRUM (optimization)​

What I will Learn here?:

On this tutorial we will see how to Optimize the data sent to Vertex Shader, in order to avoid sending off-screen vertices to the pipeline.

We will also see how to detect collisions with the terrain surface and we will also add gravity to the terrain!

Note:

All this new feactures and look at the speed, increased due to the use of quad tree to avoid sending vertices outside of our camera view to the GPU.

My old computer with 4 years old with:

- Intel-Core2-Duo-Processor-E8400-(6M-Cache-3_00-GHz-1333-MHz-FSB)

- GTX 560 ti

Can do +-2000 fps overclocked at full screen 1920x1080

Try in your computer Maximize the Window then ALT ENTER ...

​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
        quadtreeClass.cpp
        quadtreeClass.h

        terrainClass.cpp
        terrainClass.h
        terrainManagerClass.cpp
        terrainManagerClass.h

​​(main.h)

- Set for Chapter 14

​Colision Detection:

We will use a new variable/flag that switch with Key F12 between colision detection on / off

#if TUTORIAL_CHAP >= 14
bool g_GOD_MODE = false;
#endif/

 

    // Proccess "F12": GOD MODE SWITCH
    // --------------------------------------------------------------------------------------------

    #if TUTORIAL_CHAP >= 14
    {   static bool state = false;

        if ((m_player[g_NetID]->p_player.IsGodModePressed) && (!state)) {
            state = true;
        }
        if ((!m_player[g_NetID]->p_player.IsGodModePressed) && (state)) {
            g_GOD_MODE = !g_GOD_MODE;
            state = false;
        }
    }
    #endif

 

Also use Key "A" and "Z" to go up and down on god mode:


    if (g_GOD_MODE) {
        m_NextPosition.MoveUpward(m_player[g_NetID]->p_player.IsAPressed);   
        m_NextPosition.MoveDownward(m_player[g_NetID]->p_player.IsZPressed);
    }

 

This is the new part to detect colisions with the terrain comparing the normals (nx and nz <=0.5):

    //[3] - Check Colisions:
    // --------------------------------------------------------------------------------------------

    #if TUTORIAL_CHAP >= 14
    if (!g_GOD_MODE)
        nextHeight = m_TerrainManager.m_Terrain->getTerrainHeight(m_NextPosition.m_positionX, m_NextPosition.m_positionZ);

    //Detect colision with terrain:
    float nx=1.0f, nz=1.0f;
    if (!g_GOD_MODE) {
        if ((m_NextPosition.m_positionZ < m_TerrainManager.m_Terrain->m_terrainHeight) && (m_NextPosition.m_positionX < m_TerrainManager.m_Terrain->m_terrainWidth))  {
            #if TUTORIAL_CHAP >= 14 && TUTORIAL_CHAP < 18
            nx = m_TerrainManager.m_Terrain->m_heightMap_13[(m_TerrainManager.m_Terrain->m_terrainHeight * ((int)m_NextPosition.m_positionZ)) + ((int)m_NextPosition.m_positionX)].nx;//TODO: all map types
            nz = m_TerrainManager.m_Terrain->m_heightMap_13[(m_TerrainManager.m_Terrain->m_terrainHeight * ((int)m_NextPosition.m_positionZ)) + ((int)m_NextPosition.m_positionX)].nz;

            #elif TUTORIAL_CHAP >= 18 && TUTORIAL_CHAP < 19
            // Future
            #elif TUTORIAL_CHAP >= 19 && TUTORIAL_CHAP < 21
            // Future
            #elif TUTORIAL_CHAP >= 21
            // Future
            #endif
        }
    }

    if (!g_GOD_MODE) {
        if ( abs(nz) <= 0.5f || nextHeight < m_Position[g_NetID]->m_positionY)
            #if TUTORIAL_CHAP >= 96
            if ( CompoundZnormalOK )
            #endif
                m_Position[g_NetID]->m_positionZ = m_NextPosition.m_positionZ;

        if ( abs(nx) <= 0.5f  || nextHeight < m_Position[g_NetID]->m_positionY)
            #if TUTORIAL_CHAP >= 96
            if ( CompoundXnormalOK )
            #endif
                m_Position[g_NetID]->m_positionX = m_NextPosition.m_positionX;
    } else {
        m_Position[g_NetID]->m_positionZ = m_NextPosition.m_positionZ;
        m_Position[g_NetID]->m_positionX = m_NextPosition.m_positionX;
    }

    // Update also "Y" position (Get Height at our position)
    // --------------------------------------------------------------------------------------------

    if (!g_GOD_MODE)
        height = m_TerrainManager.m_Terrain->getTerrainHeight(m_Position[g_NetID]->m_positionX, m_Position[g_NetID]->m_positionZ); // Gravity to Terrain
    else
        height = m_NextPosition.m_positionY;
    #else
    //[4] Get the new allowed positions:
    m_Position[g_NetID]->m_positionZ = m_NextPosition.m_positionZ;
    m_Position[g_NetID]->m_positionX = m_NextPosition.m_positionX;
    #endif

 

​​​Project Code:

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

 

What's next?

On next tutorial we will see how to Render the Respective Mini-Map of this Big Map

bottom of page