Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
SimpleGrid.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of Hydrax.
4Visit ---
5
6Copyright (C) 2008 Xavier Vergu�n Gonz�lez <xavierverguin@hotmail.com>
7 <xavyiy@gmail.com>
8
9This program is free software; you can redistribute it and/or modify it under
10the terms of the GNU Lesser General Public License as published by the Free Software
11Foundation; either version 2 of the License, or (at your option) any later
12version.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public License along with
19this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21http://www.gnu.org/copyleft/lesser.txt.
22--------------------------------------------------------------------------------
23*/
24
25#include <SimpleGrid.h>
26
27namespace Hydrax{namespace Module
28{
30 {
31 if (NormalMode == MaterialManager::NM_VERTEX)
32 {
33 return Mesh::VT_POS_NORM;
34 }
35
36 // NM_RTT
37 return Mesh::VT_POS;
38 }
39
41 {
42 if (NormalMode == MaterialManager::NM_VERTEX)
43 {
44 return "Vertex";
45 }
46
47 return "Rtt";
48 }
49
51 : Module("SimpleGrid" + _SG_getNormalModeString(NormalMode),
52 n, Mesh::Options(256, Size(100), _SG_getVertexTypeFromNormalMode(NormalMode)), NormalMode)
53 , mHydrax(h)
54 , mVertices(0)
55 , mVerticesChoppyBuffer(0)
56 {
57 }
58
60 : Module("SimpleGrid" + _SG_getNormalModeString(NormalMode),
61 n, Mesh::Options(Options.Complexity, Size(Options.MeshSize), _SG_getVertexTypeFromNormalMode(NormalMode)), NormalMode)
62 , mHydrax(h)
63 , mVertices(0)
64 , mVerticesChoppyBuffer(0)
65 {
67 }
68
70 {
71 remove();
72
73 HydraxLOG(getName() + " destroyed.");
74 }
75
77 {
81
84
85 if (isCreated())
86 {
88 {
89 remove();
91 create();
92
94 {
96 {
97 HydraxLOG(mNoise->getName() + " doesn't support GPU Normal map generation.");
98 }
99 }
100
101 Ogre::String MaterialNameTmp = mHydrax->getMesh()->getMaterialName();
102 mHydrax->getMesh()->remove();
103
105 mHydrax->getMesh()->setMaterialName(MaterialNameTmp);
106 mHydrax->getMesh()->create();
107
108 return;
109 }
110
112
113 int v, u;
115 {
116 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
117
118 for(v=0; v<mOptions.Complexity; v++)
119 {
120 for(u=0; u<mOptions.Complexity; u++)
121 {
122 Vertices[v*mOptions.Complexity + u].x = (static_cast<float>(v)/(mOptions.Complexity-1)) * mOptions.MeshSize.Width;
123 Vertices[v*mOptions.Complexity + u].z = (static_cast<float>(u)/(mOptions.Complexity-1)) * mOptions.MeshSize.Height;
124 }
125 }
126
128 {
129 for(int i = 0; i < mOptions.Complexity*mOptions.Complexity; i++)
130 {
131 mVerticesChoppyBuffer[i] = Vertices[i];
132 }
133 }
134 }
136 {
137 Mesh::POS_VERTEX* Vertices = static_cast<Mesh::POS_VERTEX*>(mVertices);
138
139 for(v=0; v<mOptions.Complexity; v++)
140 {
141 for(u=0; u<mOptions.Complexity; u++)
142 {
143 Vertices[v*mOptions.Complexity + u].x = (static_cast<float>(v)/(mOptions.Complexity-1)) * mOptions.MeshSize.Width;
144 Vertices[v*mOptions.Complexity + u].z = (static_cast<float>(u)/(mOptions.Complexity-1)) * mOptions.MeshSize.Height;
145 }
146 }
147 }
148
149 return;
150 }
151
153 }
154
156 {
157 HydraxLOG("Creating " + getName() + " module.");
158
160
161 int v, u;
163 {
165 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
166
167 for(v=0; v<mOptions.Complexity; v++)
168 {
169 for(u=0; u<mOptions.Complexity; u++)
170 {
171 Vertices[v*mOptions.Complexity + u].x = (static_cast<float>(v)/(mOptions.Complexity-1)) * mOptions.MeshSize.Width;
172 Vertices[v*mOptions.Complexity + u].z = (static_cast<float>(u)/(mOptions.Complexity-1)) * mOptions.MeshSize.Height;
173
174 Vertices[v*mOptions.Complexity + u].nx = 0;
175 Vertices[v*mOptions.Complexity + u].ny = -1;
176 Vertices[v*mOptions.Complexity + u].nz = 0;
177 }
178 }
179
181 {
183
184 for(int i = 0; i < mOptions.Complexity*mOptions.Complexity; i++)
185 {
186 mVerticesChoppyBuffer[i] = Vertices[i];
187 }
188 }
189 }
191 {
193 Mesh::POS_VERTEX* Vertices = static_cast<Mesh::POS_VERTEX*>(mVertices);
194
195 for(v=0; v<mOptions.Complexity; v++)
196 {
197 for(u=0; u<mOptions.Complexity; u++)
198 {
199 Vertices[v*mOptions.Complexity + u].x = (static_cast<float>(v)/(mOptions.Complexity-1)) * mOptions.MeshSize.Width;
200 Vertices[v*mOptions.Complexity + u].z = (static_cast<float>(u)/(mOptions.Complexity-1)) * mOptions.MeshSize.Height;
201 }
202 }
203 }
204
205 HydraxLOG(getName() + " created.");
206 }
207
209 {
210 if (!isCreated())
211 {
212 return;
213 }
214
216
217 if (mVertices)
218 {
220 {
221 delete [] static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
222 mVertices = 0;
223 }
225 {
226 delete [] static_cast<Mesh::POS_VERTEX*>(mVertices);
227 mVertices = 0;
228 }
229 }
230
232 {
233 delete [] mVerticesChoppyBuffer;
234 }
235 }
236
237 void SimpleGrid::saveCfg(Ogre::String &Data)
238 {
239 Module::saveCfg(Data);
240
241 Data += CfgFileManager::_getCfgString("SG_Complexity", mOptions.Complexity);
242 Data += CfgFileManager::_getCfgString("SG_MeshSize", mOptions.MeshSize);
243 Data += CfgFileManager::_getCfgString("SG_Strength", mOptions.Strength);
244 Data += CfgFileManager::_getCfgString("SG_Smooth", mOptions.Smooth);
245 Data += CfgFileManager::_getCfgString("SG_ChoppyWaves", mOptions.ChoppyWaves);
246 Data += CfgFileManager::_getCfgString("SG_ChoppyStrength", mOptions.ChoppyStrength); Data += "\n";
247 }
248
249 bool SimpleGrid::loadCfg(Ogre::ConfigFile &CfgFile)
250 {
251 if (!Module::loadCfg(CfgFile))
252 {
253 return false;
254 }
255
257 Options(CfgFileManager::_getIntValue(CfgFile, "SG_Complexity"),
258 CfgFileManager::_getSizeValue(CfgFile, "SG_MeshSize"),
259 CfgFileManager::_getFloatValue(CfgFile, "SG_Strength"),
260 CfgFileManager::_getBoolValue(CfgFile, "PG_Smooth"),
261 CfgFileManager::_getBoolValue(CfgFile, "PG_ChoppyWaves"),
262 CfgFileManager::_getFloatValue(CfgFile, "PG_ChoopyStrength")));
263
264 return true;
265 }
266
267 void SimpleGrid::update(const Ogre::Real &timeSinceLastFrame)
268 {
269 if (!isCreated())
270 {
271 return;
272 }
273
274 Module::update(timeSinceLastFrame);
275
276 // Update heigths
277
279 {
280 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
281
283 {
284 for(int i = 0; i < mOptions.Complexity*mOptions.Complexity; i++)
285 {
286 Vertices[i] = mVerticesChoppyBuffer[i];
287 Vertices[i].y = mNoise->getValue(Vertices[i].x, Vertices[i].z) * mOptions.Strength;
288 }
289 }
290 else
291 {
292 for(int i = 0; i < mOptions.Complexity*mOptions.Complexity; i++)
293 {
294 Vertices[i].y = mNoise->getValue(Vertices[i].x, Vertices[i].z) * mOptions.Strength;
295 }
296 }
297 }
299 {
300 Mesh::POS_VERTEX* Vertices = static_cast<Mesh::POS_VERTEX*>(mVertices);
301
302 // For object-space to world-space conversion
303 // RTT normals calculation needs world-space coords
304 Ogre::Vector3 p = Ogre::Vector3(0,0,0);
305 Ogre::Affine3 mWorldMatrix;
306 mWorldMatrix = mHydrax->getMesh()->getEntity()->getParentSceneNode()->_getFullTransform();
307 for(int i = 0; i < mOptions.Complexity*mOptions.Complexity; i++)
308 {
309 p.x = Vertices[i].x;
310 p.y = 0;
311 p.z = Vertices[i].z;
312
313 // Calculate the world-space position
314 mWorldMatrix*(p);
315
316 Vertices[i].y = mNoise->getValue(p.x, p.z) * mOptions.Strength;
317 }
318 }
319
320 // Smooth the heightdata
321 if (mOptions.Smooth)
322 {
324 {
325 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
326
327 for(int v=1; v<(mOptions.Complexity-1); v++)
328 {
329 for(int u=1; u<(mOptions.Complexity-1); u++)
330 {
331 Vertices[v*mOptions.Complexity + u].y =
332 0.2f *
333 (Vertices[v *mOptions.Complexity + u ].y +
334 Vertices[v *mOptions.Complexity + (u+1)].y +
335 Vertices[v *mOptions.Complexity + (u-1)].y +
336 Vertices[(v+1)*mOptions.Complexity + u ].y +
337 Vertices[(v-1)*mOptions.Complexity + u ].y);
338 }
339 }
340 }
342 {
343 Mesh::POS_VERTEX* Vertices = static_cast<Mesh::POS_VERTEX*>(mVertices);
344
345 for(int v=1; v<(mOptions.Complexity-1); v++)
346 {
347 for(int u=1; u<(mOptions.Complexity-1); u++)
348 {
349 Vertices[v*mOptions.Complexity + u].y =
350 0.2f *
351 (Vertices[v *mOptions.Complexity + u ].y +
352 Vertices[v *mOptions.Complexity + (u+1)].y +
353 Vertices[v *mOptions.Complexity + (u-1)].y +
354 Vertices[(v+1)*mOptions.Complexity + u ].y +
355 Vertices[(v-1)*mOptions.Complexity + u ].y);
356 }
357 }
358 }
359 }
360
361 // Update normals
363
364 // Perform choppy waves
366
367 // Upload geometry changes
369 }
370
372 {
374 {
375 return;
376 }
377
378 int v, u;
379 Ogre::Vector3 vec1, vec2, normal;
380
381 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
382
383 for(v=1; v<(mOptions.Complexity-1); v++)
384 {
385 for(u=1; u<(mOptions.Complexity-1); u++)
386 {
387 vec1 = Ogre::Vector3(
388 Vertices[v*mOptions.Complexity + u + 1].x-Vertices[v*mOptions.Complexity + u - 1].x,
389 Vertices[v*mOptions.Complexity + u + 1].y-Vertices[v*mOptions.Complexity + u - 1].y,
390 Vertices[v*mOptions.Complexity + u + 1].z-Vertices[v*mOptions.Complexity + u - 1].z);
391
392 vec2 = Ogre::Vector3(
393 Vertices[(v+1)*mOptions.Complexity + u].x - Vertices[(v-1)*mOptions.Complexity + u].x,
394 Vertices[(v+1)*mOptions.Complexity + u].y - Vertices[(v-1)*mOptions.Complexity + u].y,
395 Vertices[(v+1)*mOptions.Complexity + u].z - Vertices[(v-1)*mOptions.Complexity + u].z);
396
397 normal = vec2.crossProduct(vec1);
398
399 Vertices[v*mOptions.Complexity + u].nx = normal.x;
400 Vertices[v*mOptions.Complexity + u].ny = normal.y;
401 Vertices[v*mOptions.Complexity + u].nz = normal.z;
402 }
403 }
404 }
405
407 {
409 {
410 return;
411 }
412
413 int v, u,
414 Underwater = 1;
415
417 {
418 Underwater = -1;
419 }
420
421 Mesh::POS_NORM_VERTEX* Vertices = static_cast<Mesh::POS_NORM_VERTEX*>(mVertices);
422
423 for(v=1; v<(mOptions.Complexity-1); v++)
424 {
425 for(u=1; u<(mOptions.Complexity-1); u++)
426 {
427 Vertices[v*mOptions.Complexity + u].x += Vertices[v*mOptions.Complexity + u].nx * mOptions.ChoppyStrength * Underwater;
428 Vertices[v*mOptions.Complexity + u].z += Vertices[v*mOptions.Complexity + u].nz * mOptions.ChoppyStrength * Underwater;
429 }
430 }
431 }
432
433 float SimpleGrid::getHeigth(const Ogre::Vector2 &Position)
434 {
436 {
437 Ogre::Vector2 RelativePos = mHydrax->getMesh()->getGridPosition(Position);
438
439 RelativePos.x *= mOptions.MeshSize.Width;
440 RelativePos.y *= mOptions.MeshSize.Height;
441
442 return mHydrax->getPosition().y + mNoise->getValue(RelativePos.x, RelativePos.y)*mOptions.Strength;
443 }
444 else // RTT Normals calculations works with world-space coords
445 {
446 return mHydrax->getPosition().y + mNoise->getValue(Position.x, Position.y)*mOptions.Strength;
447 }
448 }
449}}
#define HydraxLOG(msg)
Definition Application.h:60
static Ogre::String _getCfgString(const Ogre::String &Name, const int &Value)
static Size _getSizeValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get size value.
static Ogre::Real _getFloatValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get float value.
static bool _getBoolValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get bool value.
static int _getIntValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get int value.
Mesh * getMesh()
Get Hydrax::Mesh.
Definition Hydrax.h:317
const Ogre::Vector3 & getPosition() const
Get water position.
Definition Hydrax.h:413
void _setStrength(const Ogre::Real &Strength)
Set water strength GPU param.
Definition Hydrax.cpp:764
GPUNormalMapManager * getGPUNormalMapManager()
Get Hydrax::GPUNormalMapManager.
Definition Hydrax.h:365
const bool & _isCurrentFrameUnderwater() const
Is current frame underwater?
Definition Hydrax.h:621
NormalMode
Normal generation mode.
Class wich contains all funtions/variables related to Hydrax water mesh.
Definition Mesh.h:47
const Ogre::String & getMaterialName() const
Get material name.
Definition Mesh.h:269
Ogre::Vector2 getGridPosition(const Ogre::Vector2 &Position)
Get the [0,1] range x/y grid position from a 2D world space x/z point.
Definition Mesh.cpp:353
void create()
Create our water mesh, geometry, entity, etc...
Definition Mesh.cpp:117
void setMaterialName(const Ogre::String &MaterialName)
Set mesh material.
Definition Mesh.cpp:107
void remove()
Remove all resources.
Definition Mesh.cpp:52
VertexType
Mesh vertex type enum.
Definition Mesh.h:84
@ VT_POS_NORM
Definition Mesh.h:86
void setOptions(const Options &Options)
Update options.
Definition Mesh.cpp:78
Ogre::Entity * getEntity()
Get entity.
Definition Mesh.h:221
bool updateGeometry(const int &numVer, void *verArray)
Update geomtry.
Definition Mesh.cpp:281
Base module class, Override it for create different ways of create water noise.
Definition Module.h:47
virtual void update(const Ogre::Real &timeSinceLastFrame)
Call it each frame.
Definition Module.cpp:94
virtual void remove()
Remove.
Definition Module.cpp:53
const Ogre::String & getName() const
Get module name.
Definition Module.h:99
virtual void create()
Create.
Definition Module.cpp:46
const bool & isCreated() const
Is created() called?
Definition Module.h:107
Noise::Noise * mNoise
Noise generator pointer.
Definition Module.h:156
MaterialManager::NormalMode mNormalMode
Normal map generation mode.
Definition Module.h:160
virtual void saveCfg(Ogre::String &Data)
Save config.
Definition Module.cpp:99
Mesh::Options mMeshOptions
Module mesh options.
Definition Module.h:158
const MaterialManager::NormalMode & getNormalMode() const
Get the normal generation mode.
Definition Module.h:125
virtual bool loadCfg(Ogre::ConfigFile &CfgFile)
Load config.
Definition Module.cpp:105
const Mesh::Options & getMeshOptions() const
Get the mesh options for this module.
Definition Module.h:133
SimpleGrid(Hydrax *h, Noise::Noise *n, const MaterialManager::NormalMode &NormalMode)
Constructor.
Hydrax * mHydrax
Our Hydrax pointer.
Definition SimpleGrid.h:195
bool loadCfg(Ogre::ConfigFile &CfgFile)
Load config.
Mesh::POS_NORM_VERTEX * mVerticesChoppyBuffer
Use it to store vertex positions when choppy displacement is enabled.
Definition SimpleGrid.h:189
void _performChoppyWaves()
Perform choppy waves.
void setOptions(const Options &Options)
Set options.
Options mOptions
Our projected grid options.
Definition SimpleGrid.h:192
float getHeigth(const Ogre::Vector2 &Position)
Get the current heigth at a especified world-space point.
void update(const Ogre::Real &timeSinceLastFrame)
Call it each frame.
void * mVertices
Vertex pointer (Mesh::POS_NORM_VERTEX or Mesh::POS_VERTEX)
Definition SimpleGrid.h:186
void _calculeNormals()
Calcule current normals.
void saveCfg(Ogre::String &Data)
Save config.
Base noise class, Override it for create different ways of create water noise.
Definition Noise.h:43
const Ogre::String & getName() const
Get noise name.
Definition Noise.h:92
virtual bool createGPUNormalMapResources(GPUNormalMapManager *g)
Create GPUNormalMap resources.
Definition Noise.cpp:52
virtual float getValue(const float &x, const float &y)=0
Get the especified x/y noise value.
Mesh::VertexType _SG_getVertexTypeFromNormalMode(const MaterialManager::NormalMode &NormalMode)
Ogre::String _SG_getNormalModeString(const MaterialManager::NormalMode &NormalMode)
Size MeshSize
Grid size (X/Z) world space.
Definition Mesh.h:135
float MeshStrength
Water strength.
Definition Mesh.h:137
int MeshComplexity
Mesh complexity.
Definition Mesh.h:133
Vertex struct for position and normals data.
Definition Mesh.h:61
Vertex struct for position data.
Definition Mesh.h:77
Struct wich contains Hydrax simple grid module options.
Definition SimpleGrid.h:50
float ChoppyStrength
Choppy waves strength.
Definition SimpleGrid.h:62
int Complexity
Projected grid complexity (N*N)
Definition SimpleGrid.h:52
Struct wich contains an especific width and height value.
Definition Help.h:41
int Height
Height value.
Definition Help.h:45
int Width
Width value.
Definition Help.h:43