User Tools

Site Tools


Rendering

For rendering the terrain system, you have two choices: i) use the built-in terrain rendering system, or ii) render it yourself. This section outlines how to use the built-in rendering system.


CTerrainRender

To access this class (the built-in renderer), include the header file for the flavor of a compatible graphics API (e.g. #include “Renderers/OpenGL/OpenGLRenderer.h”). Once included it will typedef a class called CTerrainRender which will use the CTerrainRI with class policies that provide the correct rendering constructs.

To get started, call CTerrainRender:Init() during initialization. This function is similar to CTerrain::Init() but has one additional parameter. The CTerrainRender class is derived from CTerrain and will call CTerrain::Init() internally.

st_bool CTerrainRender::InitGfx(st_int32                 nNumLods,
                                st_int32                 nMaxTileRes,
                                st_float32               fCellSize,
                                const SVertexAttribDesc* pVertexFormat);

Descriptions for the first three parameters can be found on the terrain overview page. The fourth parameter describes the vertex format the application would like to use for rendering the terrain. An example of this parameter, taken from the reference application, is:

const SVertexDecl::SAttribDesc c_asTerrainVertexDecl[ ] =
{
    { 0, VERTEX_ATTRIB_0, VERTEX_FORMAT_FULL_FLOAT, 3,
        { { VERTEX_PROPERTY_POSITION, VERTEX_COMPONENT_X }, 
          { VERTEX_PROPERTY_POSITION, VERTEX_COMPONENT_Y }, 
          { VERTEX_PROPERTY_POSITION, VERTEX_COMPONENT_Z }, 
          { VERTEX_PROPERTY_UNASSIGNED, 0 } } },
    { 0, VERTEX_ATTRIB_1, VERTEX_FORMAT_FULL_FLOAT, 3,
	{ { VERTEX_PROPERTY_DIFFUSE_TEXCOORDS, VERTEX_COMPONENT_X },
          { VERTEX_PROPERTY_DIFFUSE_TEXCOORDS, VERTEX_COMPONENT_Y },
          { VERTEX_PROPERTY_AMBIENT_OCCLUSION, VERTEX_COMPONENT_X },
          { VERTEX_PROPERTY_UNASSIGNED, 0 } } },
    VERTEX_DECL_END( )
};

The enumerations and macro used in this declaration can be found in ForestRI.h. The example vertex declaration matches with the following terrain vertex structure definition.

struct STerrainVertex
{
    Vec3        m_vCoords;
    st_float32  m_afTexCoords[3];
};

Graphics Initialization

Initialize the rendering attributes by calling CTerrainRender::SetRenderInfo(). It takes a STerrainRenderInfo structure that makes some assumptions about how terrain rendering will be done. Modifications will be necessary (and expected) when deviating from the example setup.

The example terrain rendering setup used by the reference application assumes that three diffuse textures are used, a splat map, and a normal map. See the terrain pixel shader in Terrain.hlsl for details about how these textures are used.

TerrainRI.inl contains the source code for the entire terrain render loop in CTerrainRI::Render(), including how it retrieves the correct LOD index buffer from the base class CTerrain.