# 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.cpp`]

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

        ghostCameraManipulator()->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/ps_height_4k.jpg");
        terrain->setTerrainTexture("/images/ps_texture_4k.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
        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
          rendering()->as<vl::Rendering>()->camera()->viewport()->setClearColor(vl::white);
          // set fog render state
          vl::ref<vl::Fog> fog = new vl::Fog;
          fog->setColor(vl::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()->updateHierarchy();
        }

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

    // Have fun!

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:30.
Permission is granted to use this page to write and publish articles regarding Visualization Library.