⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tefaultformation.cpp

📁 海量地形数据漫游系统,对于OPENGL开发人员具有一定的参考
💻 CPP
字号:
/*****************************************************************************\ * * TeFaultFormation.cpp * * TeFaultFormation generator implementation * * Author: Martin Havl龛ek (xhavli15 AT stud.fit.vutbr.cz) * Contributors: * * ---------------------------------------------------------------------------- * * THIS SOFTWARE IS NOT COPYRIGHTED * * This source code is offered for use in the public domain. * You may use, modify or distribute it freely. * * This source code is distributed in the hope that it will be useful but * WITHOUT ANY WARRANTY.  ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY * DISCLAIMED.  This includes but is not limited to warranties of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * If you find the source code useful, authors will kindly welcome * if you give them credit and keep their names with their source code. *\*****************************************************************************/#include <cstdlib> // srand, rand#include "generators/TeFaultFormation.h"//-----------------------------------------------------------------------------/** * Constructor. * * Sets default generator parameters and resets the step counter. */TeFaultFormation::TeFaultFormation(){  this->setDefaults();  this->reset();}//-----------------------------------------------------------------------------/** * Sets default generator parameters. */void TeFaultFormation::setDefaults(){  hmap = NULL;  stepNum = 0; // not used by this generator  _seed = seed = 0;  _numFaults = numFaults = 150;  _minDelta = minDelta = 0.05f;  _maxDelta = maxDelta = 2.0f;}//-----------------------------------------------------------------------------/** * Updates private parameters to fit theirs public images. * * I call this "delayed setting". It ensures, that correct attributes * are used for the current task (the private ones). When the task is done, * private attributes are updated to match the public ones and then will be * used for the next task. * * User doesn't need to worry about this, because this method is called * internally from generate() and genStep() functions. * * \sa seed, numFaults, minDelta, maxDelta */void TeFaultFormation::updateParams(){  _seed = seed;  _numFaults = numFaults;  _minDelta = minDelta;  _maxDelta = maxDelta;}//-----------------------------------------------------------------------------/** * In-One-Step generation. * * The whole generation will be done in one function call. * * Calling updateParams() should be the first thing done here. It calls * original TeHeightMap::generate_FaultFormation() method. */void TeFaultFormation::generate(){  this->updateParams();  hmap->generate_FaultFormation(_seed, _numFaults, _minDelta, _maxDelta);}//-----------------------------------------------------------------------------/** * One step of the generation algorithm. * * Calling updateParams() should be the first thing done in the first run. * reset() and TeHeightMap::resetFlags() should be called before returning * TRUE. * * \return TRUE if generation finished and object is ready, FALSE otherwise. * * \todo Generation should be controlled by stepNum attribute. This one *       should be dynamically updated according to system performance. */SbBool TeFaultFormation::genStep(){  static float Delta = 0;  static unsigned int Rand1X, Rand1Y, Rand2X, Rand2Y;  static int FaultVectorX, FaultVectorY;  static int ToPointVectorX, ToPointVectorY;  static int resx, resy;  switch (step) {     case 0: // init             this->updateParams();             srand(_seed);             hmap->setAllValues(0);             hmap->getResolution(resx, resy);             step++;             break;    default: // loop             if (step<_numFaults) {               for (int j=0; j<_numFaults/10; j++) { // FIXME: 10 ticks of work                                                     // should be not MAGIC-NUM                 Rand1X = rand() % resx;                 Rand1Y = rand() % resy;                 Rand2X = rand() % resx;                 Rand2Y = rand() % resy;                 // fault vector                 FaultVectorX = Rand2X - Rand1X;                 FaultVectorY = Rand2Y - Rand1Y;                 // value to be added to one side of the fault                 Delta = static_cast<float>                         (_maxDelta-((_maxDelta-_minDelta)*step)/_numFaults);                 int idx=0;                 for (int y = 0; y < resy; y++) {                   for (int x = 0; x < resx; x++) {                     // vector to the current map position                     ToPointVectorX = x - Rand1X;                     ToPointVectorY = y - Rand1Y;                     if ((ToPointVectorX*FaultVectorY-FaultVectorX*ToPointVectorY) > 0) hmap->values[idx] += Delta;                     idx++;                   }                 }                 step++;               }             }             else {               hmap->resetFlags();               this->reset();               return TRUE;             }             break;  }  return FALSE;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -