Visualization Library v1.0.3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]

Terrains and Heightmaps Tutorial

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

pagGuideTerrain.jpg

[From App_Terrain.cpp]

class App_Terrain: public BaseDemo
{
public:
  virtual void initEvent()
  {
    Log::notify(appletInfo());

    if (!Has_Multitexture)
    {
      Log::error("This test requres multi texturing.\n");
      Time::sleep(2000);
      exit(1);
    }

    ghostCameraManipulator()->setMovementSpeed(5);
    // allocate terrain scene manager
    ref<Terrain> terrain = new Terrain;
    // use GLSL?
    terrain->setUseGLSL( Has_GL_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 (Has_GL_ATI_texture_float || Has_GL_ARB_texture_float)
      terrain->setHeightmapTextureFormat(TF_LUMINANCE16F);
    else
      terrain->setHeightmapTextureFormat(TF_LUMINANCE);
    // origin of the terrain
    terrain->setOrigin(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<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<Rendering>()->camera()->viewport()->setClearColor(white);
      // set fog render state
      ref<Fog> fog = new Fog;
      fog->setColor(white);
      fog->setDensity(0.045f);
      fog->setMode(FM_EXP);
      // install and enable fog
      terrain->shaderNode()->setRenderState(IN_Propagate, fog.get());
      terrain->shaderNode()->setEnable(EN_FOG, true);
      terrain->shaderNode()->updateHierarchy();
    }

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

// Have fun!


Visualization Library v1.0.3 Reference Documentation
Copyright Michele Bosi. All rights reserved.
Updated on Tue Feb 7 2017 00:55:05.
Permission is granted to use this page to write and publish articles regarding Visualization Library.