Heightmap.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 
18 #ifndef _GAZEBO_RENDERING_HEIGHTMAP_HH_
19 #define _GAZEBO_RENDERING_HEIGHTMAP_HH_
20 
21 #include <string>
22 #include <vector>
23 #include <boost/filesystem.hpp>
24 
26 #include "gazebo/common/Image.hh"
27 #include "gazebo/math/Vector3.hh"
28 #include "gazebo/math/Vector2d.hh"
30 #include "gazebo/util/system.hh"
31 
32 namespace Ogre
33 {
34  class TerrainGlobalOptions;
35  class TerrainGroup;
36  class Terrain;
37 }
38 
39 namespace gazebo
40 {
41  namespace rendering
42  {
43  class GzTerrainMatGen;
44 
47  class GZ_RENDERING_VISIBLE DummyPageProvider : public Ogre::PageProvider
48  {
51  public: bool prepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
52  {
53  return true;
54  }
55 
58  public: bool loadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
59  {
60  return true;
61  }
62 
65  public: bool unloadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
66  {
67  return true;
68  }
69 
72  public:
73  bool unprepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
74  {
75  return true;
76  }
77  };
78 
81 
85  {
88  public: Heightmap(ScenePtr _scene);
89 
91  public: virtual ~Heightmap();
92 
94  public: void Load();
95 
98  public: void LoadFromMsg(ConstVisualPtr &_msg);
99 
105  public: double GetHeight(double _x, double _y, double _z = 1000);
106 
116  public: bool Flatten(CameraPtr _camera, math::Vector2i _mousePos,
117  double _outsideRadius, double _insideRadius,
118  double _weight = 0.1);
119 
129  public: bool Smooth(CameraPtr _camera, math::Vector2i _mousePos,
130  double _outsideRadius, double _insideRadius,
131  double _weight = 0.1);
132 
142  public: bool Raise(CameraPtr _camera, math::Vector2i _mousePos,
143  double _outsideRadius, double _insideRadius,
144  double _weight = 0.1);
145 
155  public: bool Lower(CameraPtr _camera, math::Vector2i _mousePos,
156  double _outsideRadius, double _insideRadius,
157  double _weight = 0.1);
158 
162  public: double GetAvgHeight(Ogre::Vector3 _pos, double _brushSize);
163 
166  public: void SetWireframe(bool _show);
167 
170  public: Ogre::TerrainGroup *GetOgreTerrain() const;
171 
174  public: common::Image GetImage() const;
175 
181  public: Ogre::TerrainGroup::RayResult GetMouseHit(CameraPtr _camera,
182  math::Vector2i _mousePos);
183 
188  public: void SplitHeights(const std::vector<float> &_heightmap, int _n,
189  std::vector<std::vector<float> > &_v);
190 
194  public: unsigned int GetTerrainSubdivisionCount() const;
195 
203  private: void ModifyTerrain(Ogre::Vector3 _pos, double _outsideRadius,
204  double _insideRadius, double _weight,
205  const std::string &_op);
206 
209  private: bool InitBlendMaps(Ogre::Terrain *_terrain);
210 
212  private: void ConfigureTerrainDefaults();
213 
217  private: void DefineTerrain(int _x, int _y);
218 
221  private: void SetupShadows(bool _enabled);
222 
230  private: void UpdateTerrainHash(const std::string &_hash,
231  const boost::filesystem::path &_terrainDir);
232 
239  private: bool PrepareTerrainPaging(
240  const boost::filesystem::path &_terrainDirPath);
241 
243  private: static const unsigned int numTerrainSubdivisions;
244 
249  private: static const double loadRadiusFactor;
250 
256  private: static const double holdRadiusFactor;
257 
260  private: static const boost::filesystem::path hashFilename;
261 
264  private: static const boost::filesystem::path pagingDirname;
265 
269  private: boost::filesystem::path gzPagingDir;
270 
272  private: ScenePtr scene;
273 
275  private: common::Image heightImage;
276 
278  private: math::Vector3 terrainSize;
279 
281  private: unsigned int dataSize;
282 
284  private: math::Vector3 terrainOrigin;
285 
287  private: Ogre::TerrainGlobalOptions *terrainGlobals;
288 
290  private: Ogre::TerrainGroup *terrainGroup;
291 
293  private: bool terrainsImported;
294 
296  private: std::vector<std::string> diffuseTextures;
297 
299  private: std::vector<std::string> normalTextures;
300 
302  private: std::vector<double> worldSizes;
303 
305  private: std::vector<double> blendHeight;
306 
308  private: std::vector<double> blendFade;
309 
311  private: std::vector<float> heights;
312 
314  private: GzTerrainMatGen *gzMatGen;
315 
317  private: DummyPageProvider dummyPageProvider;
318 
321  private: Ogre::PageManager *pageManager;
322 
324  private: Ogre::TerrainPaging *terrainPaging;
325 
327  private: Ogre::PagedWorld *world;
328 
330  private: std::vector<std::vector<float> > subTerrains;
331 
333  private: int terrainIdx;
334 
336  private: bool useTerrainPaging;
337 
339  private: bool terrainHashChanged;
340  };
342 
346  : public Ogre::TerrainMaterialGeneratorA
347  {
349  public: GzTerrainMatGen();
350 
352  public: virtual ~GzTerrainMatGen();
353 
355  public: class SM2Profile :
356  public Ogre::TerrainMaterialGeneratorA::SM2Profile
357  {
359  public: SM2Profile(Ogre::TerrainMaterialGenerator *_parent,
360  const Ogre::String &_name, const Ogre::String &_desc);
361 
363  public: virtual ~SM2Profile();
364 
365  public: Ogre::MaterialPtr generate(const Ogre::Terrain *_terrain);
366 
367  public: Ogre::MaterialPtr generateForCompositeMap(
368  const Ogre::Terrain *_terrain);
369 
370  public: void UpdateParams(const Ogre::MaterialPtr &_mat,
371  const Ogre::Terrain *_terrain);
372 
373  public: void UpdateParamsForCompositeMap(const Ogre::MaterialPtr &_mat,
374  const Ogre::Terrain *_terrain);
375 
376  protected: virtual void addTechnique(const Ogre::MaterialPtr &_mat,
377  const Ogre::Terrain *_terrain, TechniqueType _tt);
378 
379 #ifdef __clang__
380 #pragma clang diagnostic push
381 #pragma clang diagnostic ignored "-Woverloaded-virtual"
382 #endif // ifdef __clang__
383  protected: class ShaderHelperGLSL :
385  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperGLSL
386  {
387  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
388  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
389  TechniqueType _tt);
390 
391  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
392  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
393  TechniqueType _tt);
394 
395  public: virtual void updateParams(const SM2Profile *_prof,
396  const Ogre::MaterialPtr &_mat,
397  const Ogre::Terrain *_terrain, bool _compositeMap);
398 
399  protected: virtual void generateVpHeader(const SM2Profile *_prof,
400  const Ogre::Terrain *_terrain, TechniqueType _tt,
401  Ogre::StringStream &_outStream);
402 
403  protected: virtual void generateVpFooter(const SM2Profile *_prof,
404  const Ogre::Terrain *_terrain, TechniqueType _tt,
405  Ogre::StringStream &_outStream);
406 
407  protected: virtual void generateVertexProgramSource(
408  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
409  TechniqueType _tt,
410  Ogre::StringStream &_outStream);
411 
412  protected: virtual void defaultVpParams(const SM2Profile *_prof,
413  const Ogre::Terrain *_terrain, TechniqueType _tt,
414  const Ogre::HighLevelGpuProgramPtr &_prog);
415 
416  protected: virtual unsigned int generateVpDynamicShadowsParams(
417  unsigned int _texCoordStart, const SM2Profile *_prof,
418  const Ogre::Terrain *_terrain, TechniqueType _tt,
419  Ogre::StringStream &_outStream);
420 
421  protected: virtual void generateVpDynamicShadows(
422  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
423  TechniqueType _tt,
424  Ogre::StringStream &_outStream);
425 
426  protected: virtual void generateFpHeader(const SM2Profile *_prof,
427  const Ogre::Terrain *_terrain,
428  TechniqueType tt,
429  Ogre::StringStream &_outStream);
430 
431  protected: virtual void generateFpLayer(const SM2Profile *_prof,
432  const Ogre::Terrain *_terrain, TechniqueType tt,
433  Ogre::uint _layer,
434  Ogre::StringStream &_outStream);
435 
436  protected: virtual void generateFpFooter(const SM2Profile *_prof,
437  const Ogre::Terrain *_terrain,
438  TechniqueType tt,
439  Ogre::StringStream &_outStream);
440 
441  protected: virtual void generateFpDynamicShadowsParams(
442  Ogre::uint *_texCoord, Ogre::uint *_sampler,
443  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
444  TechniqueType _tt,
445  Ogre::StringStream &_outStream);
446 
447  protected: virtual void generateFpDynamicShadowsHelpers(
448  const SM2Profile *_prof,
449  const Ogre::Terrain *_terrain,
450  TechniqueType tt,
451  Ogre::StringStream &_outStream);
452 
453  protected: void generateFpDynamicShadows(const SM2Profile *_prof,
454  const Ogre::Terrain *_terrain, TechniqueType _tt,
455  Ogre::StringStream &_outStream);
456 
457  protected: virtual void generateFragmentProgramSource(
458  const SM2Profile *_prof,
459  const Ogre::Terrain *_terrain,
460  TechniqueType _tt,
461  Ogre::StringStream &_outStream);
462 
463  protected: virtual void updateVpParams(const SM2Profile *_prof,
464  const Ogre::Terrain *_terrain, TechniqueType _tt,
465  const Ogre::GpuProgramParametersSharedPtr &_params);
466 
467  private: Ogre::String GetChannel(Ogre::uint _idx);
468  };
469 
470  // Needed to allow access from ShaderHelperGLSL to protected members
471  // of SM2Profile.
473 
476  protected: class ShaderHelperCg :
477  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperCg
478  {
479  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
480  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
481  TechniqueType _tt);
482 
483  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
484  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
485  TechniqueType _tt);
486 
487  protected: virtual void generateVpHeader(const SM2Profile *_prof,
488  const Ogre::Terrain *_terrain, TechniqueType _tt,
489  Ogre::StringStream &_outStream);
490 
491  protected: virtual void generateVpFooter(const SM2Profile *_prof,
492  const Ogre::Terrain *_terrain, TechniqueType _tt,
493  Ogre::StringStream &_outStream);
494 
495  protected: virtual void generateVertexProgramSource(
496  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
497  TechniqueType _tt,
498  Ogre::StringStream &_outStream);
499 
500  protected: virtual void defaultVpParams(const SM2Profile *_prof,
501  const Ogre::Terrain *_terrain, TechniqueType _tt,
502  const Ogre::HighLevelGpuProgramPtr &_prog);
503 
504  protected: virtual unsigned int generateVpDynamicShadowsParams(
505  unsigned int _texCoordStart, const SM2Profile *_prof,
506  const Ogre::Terrain *_terrain, TechniqueType _tt,
507  Ogre::StringStream &_outStream);
508 
509  protected: virtual void generateVpDynamicShadows(
510  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
511  TechniqueType _tt,
512  Ogre::StringStream &_outStream);
513  };
514 
515  // Needed to allow access from ShaderHelperCg to protected members
516  // of SM2Profile.
518 
519 #ifdef __clang__
520 #pragma clang diagnostic pop
521 #endif // ifdef __clang__
522  };
523  };
524  }
525 }
526 #endif
bool unloadProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to unload page content procedurally.
Definition: Heightmap.hh:65
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
Generic integer x, y vector.
Definition: Vector2i.hh:36
Shader model 2 profile target.
Definition: Heightmap.hh:355
boost::shared_ptr< Camera > CameraPtr
Definition: RenderTypes.hh:87
friend ShaderHelperGLSL
Definition: Heightmap.hh:472
bool loadProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to load page content procedurally.
Definition: Heightmap.hh:58
#define GZ_RENDERING_VISIBLE
Definition: system.hh:241
Rendering a terrain using heightmap information.
Definition: Heightmap.hh:84
bool prepareProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to prepare page content procedurally.
Definition: Heightmap.hh:51
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:79
friend ShaderHelperCg
Definition: Heightmap.hh:517
Utility class to help with generating shaders for GLSL.
Definition: Heightmap.hh:384
Definition: Heightmap.hh:345
Encapsulates an image.
Definition: Image.hh:68
Keeping the CG shader for reference.
Definition: Heightmap.hh:476
Pretends to provide procedural page content to avoid page loading.
Definition: Heightmap.hh:47
bool unprepareProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to unprepare page content procedurally.
Definition: Heightmap.hh:73