# Visualization Library A lightweight C++ OpenGL middleware for 2D/3D graphics | [Home] [Tutorials] [All Classes] [Grouped Classes]

# Terrains and Heightmaps Tutorial

This tutorial demonstrates how to render a heightmap-based terrain using the Terrain scene manager.

[From `App_Terrain.hpp`]

    class App_Terrain: public BaseDemo
    {
    public:
      virtual void shutdown() {}

      virtual void run() {}

      virtual void initEvent()
      {
        if (!GLEW_ARB_multitexture)
        {
          vl::Log::error("GL_ARB_multitexture required.\n");
          openglContext()->quitApplication();
          return;
        }
        BaseDemo::initEvent();

        ghostCamera()->setMovementSpeed(5);
        // allocate terrain scene manager
        vl::ref<vl::Terrain> terrain = new vl::Terrain;
        // use GLSL?
        terrain->setUseGLSL(GLEW_ARB_shading_language_100?true:false);
        // dimensions of the terrain
        terrain->setWidth(100);
        terrain->setDepth(100);
        terrain->setHeight(5.0f);
        // heightmap texture size used by the GLSL program
        if (GLEW_ATI_texture_float || GLEW_ARB_texture_float)
          terrain->setHeightmapTextureFormat(vl::TF_LUMINANCE16F);
        else
          terrain->setHeightmapTextureFormat(vl::TF_LUMINANCE);
        // origin of the terrain
        terrain->setOrigin(vl::vec3(0,0,0));
        // define textures
        terrain->setHeightmapTexture("/images/terrain-h.jpg");
        terrain->setTerrainTexture("/images/terrain-t.jpg");
        terrain->setDetailTexture("/images/noise.png");
        terrain->setDetailRepetitionCount(8);
        // define shaders to be used to render the terrain
        terrain->setFragmentShader("/glsl/terrain.fs");
        terrain->setVertexShader("/glsl/terrain.vs");
        // initialize the terrain
        terrain->init();
        // add the terrain scene manager to the rendering
        vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->sceneManagers()->push_back( terrain.get() );

        // adds fog if we are not using GLSL but the fixed function pipeline
        if (!terrain->useGLSL())
        {
          // set sky to white
          vl::VisualizationLibrary::rendering()->as<vl::Rendering>()->camera()->viewport()->setClearColor(vlut::white);
          // set fog render state
          vl::ref<vl::Fog> fog = new vl::Fog;
          fog->setColor(vlut::white);
          fog->setDensity(0.045f);
          fog->setMode(vl::FM_EXP);
          // install and enable fog
          terrain->shaderNode()->setRenderState(fog.get());
          terrain->shaderNode()->setEnable(vl::EN_FOG,true);
          terrain->shaderNode()->updateHierachy();
        }

        // for debugging purposes
        #if 0
          showBoundingVolumes(1,0);
        #endif
      }
    };

    // Have fun!

Visualization Library v2009.08 Reference Documentation
Copyright 2005-2009 Michele Bosi. All rights reserved.
Updated on Sun Nov 29 11:11:42 2009.
Permission is granted to use this page to write and publish articles regarding Visualization Library.