VR-Forces 4.0.4 Class Documentation
Create Terrain Example

The Create Terrain example demonstrates the following:

How to Run

This example demonstrates how to programmatically create GDB (VR-Forces native format) terrain database.

Usage

(in bin directory)

createTerrainDb

A simple terrain database named data\terrain\simpleTerrain.gdb will be created. See the comments in main for details on the composition of the terrain. To view the terrain, open the database in TDBTool or vrfGui.

Main Application File

/*******************************************************************************
** Copyright (c) 2003 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/
/*******************************************************************************
** $RCSfile: main.cxx,v $ $Revision: 1.13 $ $State: Exp $
*******************************************************************************/

#include "gdb/terrainDb.h"
#include "terrainCS/ser_utm.h"
#include "geometry/point.h"
#include "geometry/surface.h"
#include "gdb/triInd.h"
#include "gdb/gdbTerrainWriter.h"
#include "gdb/gdbTerrainReader.h"

// This example demonstrates how to use the terrain API to create a simple
// terrain database in code.  It creates a flat, 1000 meter square UTM terrain
// database.  It saves the newly created database to file, in gdb format 
// (..\data\simpleTerrain.gdb).

int main(int argc, char** argv)
{
   DtTerrainDatabase terrainDb;

   terrainDb.init();

   // Local origin of terrain database.
   DtDegMinSec latRef = {35.0, 0.0, 0.0, DtNorth};
   DtDegMinSec lonRef = {122.0, 0.0, 0.0, DtWest};

   // If standardOffsets is true, the standard false easting (500,000 m)
   // and false northing (10,000,000 m only when south of equator) will
   // be applied.
   int standardOffsets = 0;
   DtSerializableUtmCS* coordinateSystem = new DtSerializableUtmCS(
      latRef, lonRef, standardOffsets);

   // Set the coordinate system for our terrain database.  The terrain
   // database will assume responsibility for deleting the memory
   // associated with the coordinate system.
   terrainDb.setCoordinateSystem(coordinateSystem);

   // Create a simple terrain skin, consisting of an 1000 meter square plane with
   // zero altitude, formed by 8 triangles.

   // Create the surface which will be applied to the triangles
   DtSurface surface;
   surface.setIsGround(true);
   surface.setIsGrass(true);
   surface.setColor(DtColor(127, 127, 90, 0));

   // Create the vertices that will be used by the triangles  
   DtPoint vert0(    0.0,    0.0, 0.0); 
   DtPoint vert1( 1000.0,    0.0, 0.0); 
   DtPoint vert2(    0.0, 1000.0, 0.0); 
   DtPoint vert3( 1000.0, 1000.0, 0.0); 
   DtPoint vert4(  500.0,  500.0, 0.0); 
   DtPoint vert5(  500.0,    0.0, 0.0); 
   DtPoint vert6( 1000.0,  500.0, 0.0); 
   DtPoint vert7(  500.0, 1000.0, 0.0); 
   DtPoint vert8(    0.0,  500.0, 0.0); 


   // Terrain will be comprised of 8 250 x 250 x 353.5 meter polygons.

   // Create and triangles for the lower left quadrant of the terrain.
   // Triangles are created by the terrain database by passing in 3 vertices, in counter-clockwise
   // order (to determine the face of the triangle), a surface for the triangle, and two optional 
   // parameters:  a pointer to a parent DtGroup node and a flag specifying whether or not to
   // add the triangle to the beginning of the group it is added to.
   // All triangles created by the terrain database must be assigned as children of a group.  Thus
   // if a parent is not specified, the root terrain node will be used.  
   // By specifying false, the triangles will be added to the end of the root terrain node's list of 
   // child nodes.  False is the default value.
   // 
   // The createTriangle function will return a pointer to the newly created indirect triangle if it 
   // succeeds and NULL if it does not.
   DtTriangleIndirect* tri0 = terrainDb.createTriangle(vert0, vert5, vert4, surface, NULL, false);
   DtTriangleIndirect* tri1 = terrainDb.createTriangle(vert0, vert4, vert8, surface, NULL, false);

   // Create the remaining quadrants' triangles

   // Lower right quandrant of terrain
   DtTriangleIndirect* tri2 = terrainDb.createTriangle(vert5, vert1, vert6, surface, NULL, false);
   DtTriangleIndirect* tri3 = terrainDb.createTriangle(vert5, vert6, vert4, surface, NULL, false);

   // Upper right quandrant of terrain
   DtTriangleIndirect* tri4 = terrainDb.createTriangle(vert4, vert6, vert3, surface, NULL, false);
   DtTriangleIndirect* tri5 = terrainDb.createTriangle(vert4, vert3, vert7, surface, NULL, false);

   // Upper left quandrant of terrain
   DtTriangleIndirect* tri6 = terrainDb.createTriangle(vert8, vert4, vert7, surface, NULL, false);
   DtTriangleIndirect* tri7 = terrainDb.createTriangle(vert8, vert7, vert2, surface, NULL, false);

   // Optionally, to save memory 
   // you can ask the terrain database to share identical surfaces and
   // vertices that are within a certain tolerance (defaults to using 0.0)

   // User can specify a tolerance if so desired, but may combine vertices that are
   // not actually intended to be the same if the tolerance is too generous.
   terrainDb.eliminateDuplicateVertices(0.001);

   // no tolerance - surfaces are equal or they aren't.
   terrainDb.eliminateDuplicateSurfaces();

   // Save new terrain database to gdb format file
   DtGdbTerrainWriter gdbWriter;
   gdbWriter.writeTerrain("..\\data\\terrain\\simpleTerrain.gdb", &terrainDb);

   DtInfo << "Creating terrain " << DtFilename("..\\data\\terrain\\simpleTerrain.gdb") << std::endl;

   return 0;
}


Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)