All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Heightmap.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: Heightmap geometry
18  * Author: Nate Koenig
19  * Date: 12 May 2009
20  */
21 
22 #ifndef _HEIGHTMAP_HH_
23 #define _HEIGHTMAP_HH_
24 
25 #include <string>
26 #include <vector>
27 #include <boost/filesystem.hpp>
28 
30 #include "gazebo/common/Image.hh"
31 #include "gazebo/math/Vector3.hh"
32 #include "gazebo/math/Vector2d.hh"
34 
35 namespace Ogre
36 {
37  class TerrainGlobalOptions;
38  class TerrainGroup;
39  class Terrain;
40 }
41 
42 namespace gazebo
43 {
44  namespace rendering
45  {
46  class GzTerrainMatGen;
47 
50  class DummyPageProvider : public Ogre::PageProvider
51  {
54  public: bool prepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
55  {
56  return true;
57  }
58 
61  public: bool loadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
62  {
63  return true;
64  }
65 
68  public: bool unloadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
69  {
70  return true;
71  }
72 
75  public:
76  bool unprepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
77  {
78  return true;
79  }
80  };
81 
84 
87  class Heightmap
88  {
91  public: Heightmap(ScenePtr _scene);
92 
94  public: virtual ~Heightmap();
95 
97  public: void Load();
98 
101  public: void LoadFromMsg(ConstVisualPtr &_msg);
102 
108  public: double GetHeight(double _x, double _y, double _z = 1000);
109 
119  public: bool Flatten(CameraPtr _camera, math::Vector2i _mousePos,
120  double _outsideRadius, double _insideRadius,
121  double _weight = 0.1);
122 
132  public: bool Smooth(CameraPtr _camera, math::Vector2i _mousePos,
133  double _outsideRadius, double _insideRadius,
134  double _weight = 0.1);
135 
145  public: bool Raise(CameraPtr _camera, math::Vector2i _mousePos,
146  double _outsideRadius, double _insideRadius,
147  double _weight = 0.1);
148 
158  public: bool Lower(CameraPtr _camera, math::Vector2i _mousePos,
159  double _outsideRadius, double _insideRadius,
160  double _weight = 0.1);
161 
165  public: double GetAvgHeight(Ogre::Vector3 _pos, double _brushSize);
166 
169  public: void SetWireframe(bool _show);
170 
173  public: Ogre::TerrainGroup *GetOgreTerrain() const;
174 
177  public: common::Image GetImage() const;
178 
184  public: Ogre::TerrainGroup::RayResult GetMouseHit(CameraPtr _camera,
185  math::Vector2i _mousePos);
186 
191  public: void SplitHeights(const std::vector<float> &_heightmap, int _n,
192  std::vector<std::vector<float> > &_v);
193 
197  public: unsigned int GetTerrainSubdivisionCount() const;
198 
206  private: void ModifyTerrain(Ogre::Vector3 _pos, double _outsideRadius,
207  double _insideRadius, double _weight,
208  const std::string &_op);
209 
212  private: bool InitBlendMaps(Ogre::Terrain *_terrain);
213 
215  private: void ConfigureTerrainDefaults();
216 
220  private: void DefineTerrain(int _x, int _y);
221 
224  private: void SetupShadows(bool _enabled);
225 
233  private: void UpdateTerrainHash(const std::string &_hash,
234  const boost::filesystem::path &_terrainDir);
235 
242  private: bool PrepareTerrainPaging(
243  const boost::filesystem::path &_terrainDirPath);
244 
246  // \todo Deprecated. Remove this in Gazebo 3.0
247  public: static const unsigned int NumTerrainSubdivisions;
248 
250  private: static const unsigned int numTerrainSubdivisions;
251 
256  private: static const double loadRadiusFactor;
257 
263  private: static const double holdRadiusFactor;
264 
267  private: static const boost::filesystem::path hashFilename;
268 
271  private: static const boost::filesystem::path pagingDirname;
272 
276  private: boost::filesystem::path gzPagingDir;
277 
279  private: ScenePtr scene;
280 
282  private: common::Image heightImage;
283 
285  private: math::Vector3 terrainSize;
286 
288  private: unsigned int dataSize;
289 
291  private: math::Vector3 terrainOrigin;
292 
294  private: Ogre::TerrainGlobalOptions *terrainGlobals;
295 
297  private: Ogre::TerrainGroup *terrainGroup;
298 
300  private: bool terrainsImported;
301 
303  private: std::vector<std::string> diffuseTextures;
304 
306  private: std::vector<std::string> normalTextures;
307 
309  private: std::vector<double> worldSizes;
310 
312  private: std::vector<double> blendHeight;
313 
315  private: std::vector<double> blendFade;
316 
318  private: std::vector<float> heights;
319 
321  private: GzTerrainMatGen *gzMatGen;
322 
324  private: DummyPageProvider dummyPageProvider;
325 
328  private: Ogre::PageManager *pageManager;
329 
331  private: Ogre::TerrainPaging *terrainPaging;
332 
334  private: Ogre::PagedWorld *world;
335 
337  private: std::vector<std::vector<float> > subTerrains;
338 
340  private: int terrainIdx;
341 
343  private: bool useTerrainPaging;
344 
346  private: bool terrainHashChanged;
347  };
349 
352  class GzTerrainMatGen : public Ogre::TerrainMaterialGeneratorA
353  {
355  public: GzTerrainMatGen();
356 
358  public: virtual ~GzTerrainMatGen();
359 
361  public: class SM2Profile :
362  public Ogre::TerrainMaterialGeneratorA::SM2Profile
363  {
365  public: SM2Profile(Ogre::TerrainMaterialGenerator *_parent,
366  const Ogre::String &_name, const Ogre::String &_desc);
367 
369  public: virtual ~SM2Profile();
370 
371  public: Ogre::MaterialPtr generate(const Ogre::Terrain *_terrain);
372 
373  public: Ogre::MaterialPtr generateForCompositeMap(
374  const Ogre::Terrain *_terrain);
375 
376  public: void UpdateParams(const Ogre::MaterialPtr &_mat,
377  const Ogre::Terrain *_terrain);
378 
379  public: void UpdateParamsForCompositeMap(const Ogre::MaterialPtr &_mat,
380  const Ogre::Terrain *_terrain);
381 
382  protected: virtual void addTechnique(const Ogre::MaterialPtr &_mat,
383  const Ogre::Terrain *_terrain, TechniqueType _tt);
384 
386  protected: class ShaderHelperGLSL :
387  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperGLSL
388  {
389  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
390  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
391  TechniqueType _tt);
392 
393  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
394  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
395  TechniqueType _tt);
396 
397  public: virtual void updateParams(const SM2Profile *_prof,
398  const Ogre::MaterialPtr &_mat,
399  const Ogre::Terrain *_terrain, bool _compositeMap);
400 
401  protected: virtual void generateVpHeader(const SM2Profile *_prof,
402  const Ogre::Terrain *_terrain, TechniqueType _tt,
403  Ogre::StringUtil::StrStreamType &_outStream);
404 
405  protected: virtual void generateVpFooter(const SM2Profile *_prof,
406  const Ogre::Terrain *_terrain, TechniqueType _tt,
407  Ogre::StringUtil::StrStreamType &_outStream);
408 
409  protected: virtual void generateVertexProgramSource(
410  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
411  TechniqueType _tt,
412  Ogre::StringUtil::StrStreamType &_outStream);
413 
414  protected: virtual void defaultVpParams(const SM2Profile *_prof,
415  const Ogre::Terrain *_terrain, TechniqueType _tt,
416  const Ogre::HighLevelGpuProgramPtr &_prog);
417 
418  protected: virtual unsigned int generateVpDynamicShadowsParams(
419  unsigned int _texCoordStart, const SM2Profile *_prof,
420  const Ogre::Terrain *_terrain, TechniqueType _tt,
421  Ogre::StringUtil::StrStreamType &_outStream);
422 
423  protected: virtual void generateVpDynamicShadows(
424  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
425  TechniqueType _tt,
426  Ogre::StringUtil::StrStreamType &_outStream);
427 
428  protected: virtual void generateFpHeader(const SM2Profile *_prof,
429  const Ogre::Terrain *_terrain,
430  TechniqueType tt,
431  Ogre::StringUtil::StrStreamType &_outStream);
432 
433  protected: virtual void generateFpLayer(const SM2Profile *_prof,
434  const Ogre::Terrain *_terrain, TechniqueType tt,
435  Ogre::uint _layer,
436  Ogre::StringUtil::StrStreamType &_outStream);
437 
438  protected: virtual void generateFpFooter(const SM2Profile *_prof,
439  const Ogre::Terrain *_terrain,
440  TechniqueType tt,
441  Ogre::StringUtil::StrStreamType &_outStream);
442 
443  protected: virtual void generateFpDynamicShadowsParams(
444  Ogre::uint *_texCoord, Ogre::uint *_sampler,
445  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
446  TechniqueType _tt,
447  Ogre::StringUtil::StrStreamType &_outStream);
448 
449  protected: virtual void generateFpDynamicShadowsHelpers(
450  const SM2Profile *_prof,
451  const Ogre::Terrain *_terrain,
452  TechniqueType tt,
453  Ogre::StringUtil::StrStreamType &_outStream);
454 
455  protected: void generateFpDynamicShadows(const SM2Profile *_prof,
456  const Ogre::Terrain *_terrain, TechniqueType _tt,
457  Ogre::StringUtil::StrStreamType &_outStream);
458 
459  protected: virtual void generateFragmentProgramSource(
460  const SM2Profile *_prof,
461  const Ogre::Terrain *_terrain,
462  TechniqueType _tt,
463  Ogre::StringUtil::StrStreamType &_outStream);
464 
465  protected: virtual void updateVpParams(const SM2Profile *_prof,
466  const Ogre::Terrain *_terrain, TechniqueType _tt,
467  const Ogre::GpuProgramParametersSharedPtr &_params);
468 
469  private: Ogre::String GetChannel(Ogre::uint _idx);
470  };
471 
474  protected: class ShaderHelperCg :
475  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperCg
476  {
477  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
478  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
479  TechniqueType _tt);
480 
481  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
482  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
483  TechniqueType _tt);
484 
485  protected: virtual void generateVpHeader(const SM2Profile *_prof,
486  const Ogre::Terrain *_terrain, TechniqueType _tt,
487  Ogre::StringUtil::StrStreamType &_outStream);
488 
489  protected: virtual void generateVpFooter(const SM2Profile *_prof,
490  const Ogre::Terrain *_terrain, TechniqueType _tt,
491  Ogre::StringUtil::StrStreamType &_outStream);
492 
493  protected: virtual void generateVertexProgramSource(
494  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
495  TechniqueType _tt,
496  Ogre::StringUtil::StrStreamType &_outStream);
497 
498  protected: virtual void defaultVpParams(const SM2Profile *_prof,
499  const Ogre::Terrain *_terrain, TechniqueType _tt,
500  const Ogre::HighLevelGpuProgramPtr &_prog);
501 
502  protected: virtual unsigned int generateVpDynamicShadowsParams(
503  unsigned int _texCoordStart, const SM2Profile *_prof,
504  const Ogre::Terrain *_terrain, TechniqueType _tt,
505  Ogre::StringUtil::StrStreamType &_outStream);
506 
507  protected: virtual void generateVpDynamicShadows(
508  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
509  TechniqueType _tt,
510  Ogre::StringUtil::StrStreamType &_outStream);
511  };
512  };
513  };
514  }
515 }
516 #endif