RigsofRods
Soft-body Physics Simulation
Real.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of Hydrax.
4 Visit http://www.ogre3d.org/tikiwiki/Hydrax
5 
6 Copyright (C) 2011 Jose Luis Cercós Pita <jlcercos@gmail.com>
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt.
21 --------------------------------------------------------------------------------
22 */
23 
24 // ----------------------------------------------------------------------------
25 // Include the main header
26 // ----------------------------------------------------------------------------
27 #include <Real.h>
28 
29 // ----------------------------------------------------------------------------
30 // Include Hydrax
31 // ----------------------------------------------------------------------------
32 #include <Hydrax.h>
33 
34 #define _def_PackedNoise true
35 
36 namespace Hydrax{ namespace Noise{
37 
39  : Noise("Real", true)
40  , mTime(0)
41  , mPerlinNoise(0)
42  , mGPUNormalMapManager(0)
43 {
44  mPerlinNoise = new Perlin(/*Generic one*/);
45 }
46 
48 {
49  remove();
50  delete mPerlinNoise;
51 
52  HydraxLOG(getName() + " destroyed.");
53 }
54 
56 {
57  if (isCreated())
58  {
59  return;
60  }
61 
62  Noise::create();
64 }
65 
67 {
69  {
71  }
72 
73  if (!isCreated())
74  {
75  return;
76  }
77 
78  mTime = 0;
79 
81  mWaves.clear();
82  mPressurePoints.clear();
83  Noise::remove();
84 }
85 
86 int Real::addWave(Ogre::Vector2 dir, float A, float T, float p)
87 {
88  mWaves.push_back(Wave(dir,A,T,p));
89  return static_cast<int>(mWaves.size());
90 }
91 
92 int Real::addPressurePoint(Ogre::Vector2 Orig, float p, float T, float L)
93 {
94  mPressurePoints.push_back(PressurePoint(Orig,p,T,L));
95  return static_cast<int>(mPressurePoints.size());
96 }
97 
98 Wave Real::getWave(int id) const
99 {
100  return mWaves.at(id);
101 }
102 
103 bool Real::eraseWave(int id)
104 {
105  if( (id < 0) || (id >= (int)mWaves.size()) ){
106  HydraxLOG("Error (Real::eraseWave):\tCan't delete the wave.");
107  HydraxLOG("\tIdentifier exceed the waves vector dimensions.");
108  return false;
109  }
110  size_t Size = mWaves.size();
111  mWaves.erase(mWaves.begin() + id);
112  if(mWaves.size() == Size){
113  HydraxLOG("Error (Real::eraseWave):\tCan't delete the wave.");
114  HydraxLOG("\tThe size before deletion matchs with the size after the operation.");
115  return false;
116  }
117  return true;
118 }
119 
120 bool Real::modifyWave(int id,Ogre::Vector2 dir, float A, float T, float p)
121 {
122  if( (id < 0) || (id >= (int)mWaves.size()) ){
123  HydraxLOG("Error (Real::eraseWave):\tCan't delete the wave.");
124  HydraxLOG("\tIdentifier exceed the waves vector dimensions.");
125  return false;
126  }
127  mWaves.at(id) = Wave(dir,A,T,p);
128  return true;
129 }
130 
132 {
134  return false;
135 }
136 
137 void Real::saveCfg(Ogre::String &Data)
138 {
139  mPerlinNoise->saveCfg(Data);
140 }
141 
142 bool Real::loadCfg(Ogre::ConfigFile &CfgFile)
143 {
144  if (!Noise::loadCfg(CfgFile))
145  {
146  return false;
147  }
148 
149  HydraxLOG("\tReading options...");
151  Perlin::Options(CfgFileManager::_getIntValue(CfgFile,"Perlin_Octaves"),
152  CfgFileManager::_getFloatValue(CfgFile,"Perlin_Scale"),
153  CfgFileManager::_getFloatValue(CfgFile,"Perlin_Falloff"),
154  CfgFileManager::_getFloatValue(CfgFile,"Perlin_Animspeed"),
155  CfgFileManager::_getFloatValue(CfgFile,"Perlin_Timemulti"),
156  CfgFileManager::_getFloatValue(CfgFile,"Perlin_GPU_Strength"),
157  CfgFileManager::_getVector3Value(CfgFile,"Perlin_GPU_LODParameters")));
158  HydraxLOG("\tOptions readed.");
159 
160  return true;
161 }
162 
163 void Real::update(const Ogre::Real &timeSinceLastFrame)
164 {
165  int i;
166  mTime += timeSinceLastFrame;
167  mPerlinNoise->update(timeSinceLastFrame);
168  for(i=(int)mWaves.size()-1;i>=0;i--) {
169  mWaves.at(i).update(timeSinceLastFrame);
170  }
171  for(i=(int)mPressurePoints.size()-1;i>=0;i--) {
172  if(!mPressurePoints.at(i).update(timeSinceLastFrame)) {
173  mPressurePoints.erase(mPressurePoints.begin() + i);
174  }
175  }
176 }
177 
178 float Real::getValue(const float &x, const float &y)
179 {
180  int i;
182  float H = mPerlinNoise->getValue(x,y);
184  for(i=0;i<(int)mWaves.size();i++) {
185  H += mWaves.at(i).getValue(x,y);
186  }
188  for(i=0;i<(int)mPressurePoints.size();i++) {
189  H += mPressurePoints.at(i).getValue(x,y);
190  }
191  return H;
192 }
193 
194 }} // namespace Hydrax::Noise
Hydrax::Noise::Perlin::getValue
float getValue(const float &x, const float &y)
Get the especified x/y noise value.
Definition: Perlin.cpp:403
Hydrax::Noise::Real::mGPUNormalMapManager
GPUNormalMapManager * mGPUNormalMapManager
GPUNormalMapManager pointer.
Definition: Real.h:179
Hydrax::Noise::Noise::remove
virtual void remove()
Remove.
Definition: Noise.cpp:47
Hydrax::Noise::Real::eraseWave
bool eraseWave(int id)
Removes a wave from the system.
Definition: Real.cpp:103
Hydrax::Noise::Real::create
void create()
Create.
Definition: Real.cpp:55
Hydrax::Noise::Perlin::remove
void remove()
Remove.
Definition: Perlin.cpp:74
y
float y
Definition: (ValueTypes) quaternion.h:6
Hydrax::Noise::Real::~Real
~Real()
Destructor.
Definition: Real.cpp:47
Hydrax::Noise::Noise::create
virtual void create()
Create.
Definition: Noise.cpp:42
Hydrax::Noise::Real::getValue
float getValue(const float &x, const float &y)
Get the especified x/y noise value.
Definition: Real.cpp:178
Hydrax::Noise::Real::addWave
int addWave(Ogre::Vector2 dir, float A, float T, float p=0.f)
Adds a wave to the system.
Definition: Real.cpp:86
Hydrax::Noise::Real::mPressurePoints
std::vector< PressurePoint > mPressurePoints
Vector of pressure points.
Definition: Real.h:185
Hydrax
Definition: CfgFileManager.cpp:28
Hydrax::Noise::Real::mTime
double mTime
Elapsed time.
Definition: Real.h:173
Hydrax::Noise::Real::remove
void remove()
Remove.
Definition: Real.cpp:66
Hydrax::Size
Struct wich contains an especific width and height value.
Definition: Help.h:40
Hydrax.h
Hydrax::Noise::Noise::removeGPUNormalMapResources
virtual void removeGPUNormalMapResources(GPUNormalMapManager *g)
Remove GPUNormalMap resources.
Definition: Noise.cpp:71
Hydrax::Noise::Noise::loadCfg
virtual bool loadCfg(Ogre::ConfigFile &CfgFile)
Load config.
Definition: Noise.cpp:87
Hydrax::Noise::Real::loadCfg
bool loadCfg(Ogre::ConfigFile &CfgFile)
Load config.
Definition: Real.cpp:142
Hydrax::GPUNormalMapManager
Class to manager GPU normal maps.
Definition: GPUNormalMapManager.h:45
Hydrax::Noise::Perlin::saveCfg
void saveCfg(Ogre::String &Data)
Save config.
Definition: Perlin.cpp:335
Hydrax::Noise::Real::createGPUNormalMapResources
bool createGPUNormalMapResources(GPUNormalMapManager *g)
Create GPUNormalMap resources.
Definition: Real.cpp:131
Hydrax::CfgFileManager::_getVector3Value
static Ogre::Vector3 _getVector3Value(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get vector3 value.
Definition: CfgFileManager.cpp:489
Hydrax::Noise::Real::saveCfg
void saveCfg(Ogre::String &Data)
Save config.
Definition: Real.cpp:137
Hydrax::Noise::Noise::getName
const Ogre::String & getName() const
Get noise name.
Definition: Noise.h:92
Hydrax::Noise::Noise
Base noise class, Override it for create different ways of create water noise.
Definition: Noise.h:42
Hydrax::Noise::Real::modifyWave
bool modifyWave(int id, Ogre::Vector2 dir, float A, float T, float p=0.f)
Modify a wave from the system.
Definition: Real.cpp:120
HydraxLOG
#define HydraxLOG(msg)
Definition: Application.h:59
Hydrax::Noise::Perlin::update
void update(const Ogre::Real &timeSinceLastFrame)
Call it each frame.
Definition: Perlin.cpp:367
Hydrax::Noise::Real::addPressurePoint
int addPressurePoint(Ogre::Vector2 Orig, float p, float T, float L)
Adds a pressure point to the system.
Definition: Real.cpp:92
Hydrax::Noise::Wave
A wave defined by the direction, amplitude, period, and optionally phase.
Definition: Wave.h:59
Real.h
Hydrax::Noise::Noise::areGPUNormalMapResourcesCreated
const bool & areGPUNormalMapResourcesCreated() const
Are GPU normal map resources created?
Definition: Noise.h:116
Hydrax::Noise::Real::Real
Real()
Default constructor.
Definition: Real.cpp:38
Hydrax::Noise::Real::mPerlinNoise
Perlin * mPerlinNoise
Perlin noise.
Definition: Real.h:176
Hydrax::Noise::Perlin::Options
Struct wich contains Perlin noise module options.
Definition: Perlin.h:78
Hydrax::Noise::Real::update
void update(const Ogre::Real &timeSinceLastFrame)
Call it each frame.
Definition: Real.cpp:163
Hydrax::Noise::PressurePoint
A PressurePoint defined by the origen, pressure pulse, Maximum time of perturbation and wave longitud...
Definition: PressurePoint.h:69
Hydrax::Noise::Perlin::create
void create()
Create.
Definition: Perlin.cpp:63
Hydrax::CfgFileManager::_getIntValue
static int _getIntValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get int value.
Definition: CfgFileManager.cpp:432
Hydrax::Noise::Noise::isCreated
const bool & isCreated() const
Is created() called?
Definition: Noise.h:100
Hydrax::Noise::Perlin::setOptions
void setOptions(const Options &Options)
Set/Update perlin noise options.
Definition: Perlin.cpp:91
Hydrax::CfgFileManager::_getFloatValue
static Ogre::Real _getFloatValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get float value.
Definition: CfgFileManager.cpp:446
Hydrax::Noise::Perlin
Perlin noise module class.
Definition: Perlin.h:73
x
float x
Definition: (ValueTypes) quaternion.h:5
Hydrax::Noise::Real::mWaves
std::vector< Wave > mWaves
Vector of waves.
Definition: Real.h:182
Hydrax::Noise::Real::getWave
Wave getWave(int id) const
Returns a wave to the system.
Definition: Real.cpp:98