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 /* 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 #include "gazebo/util/system.hh"
35 
36 namespace Ogre
37 {
38  class TerrainGlobalOptions;
39  class TerrainGroup;
40  class Terrain;
41 }
42 
43 namespace gazebo
44 {
45  namespace rendering
46  {
47  class GzTerrainMatGen;
48 
51  class GAZEBO_VISIBLE DummyPageProvider : public Ogre::PageProvider
52  {
55  public: bool prepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
56  {
57  return true;
58  }
59 
62  public: bool loadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
63  {
64  return true;
65  }
66 
69  public: bool unloadProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
70  {
71  return true;
72  }
73 
76  public:
77  bool unprepareProceduralPage(Ogre::Page*, Ogre::PagedWorldSection*)
78  {
79  return true;
80  }
81  };
82 
85 
89  {
92  public: Heightmap(ScenePtr _scene);
93 
95  public: virtual ~Heightmap();
96 
98  public: void Load();
99 
102  public: void LoadFromMsg(ConstVisualPtr &_msg);
103 
109  public: double GetHeight(double _x, double _y, double _z = 1000);
110 
120  public: bool Flatten(CameraPtr _camera, math::Vector2i _mousePos,
121  double _outsideRadius, double _insideRadius,
122  double _weight = 0.1);
123 
133  public: bool Smooth(CameraPtr _camera, math::Vector2i _mousePos,
134  double _outsideRadius, double _insideRadius,
135  double _weight = 0.1);
136 
146  public: bool Raise(CameraPtr _camera, math::Vector2i _mousePos,
147  double _outsideRadius, double _insideRadius,
148  double _weight = 0.1);
149 
159  public: bool Lower(CameraPtr _camera, math::Vector2i _mousePos,
160  double _outsideRadius, double _insideRadius,
161  double _weight = 0.1);
162 
166  public: double GetAvgHeight(Ogre::Vector3 _pos, double _brushSize);
167 
170  public: void SetWireframe(bool _show);
171 
174  public: Ogre::TerrainGroup *GetOgreTerrain() const;
175 
178  public: common::Image GetImage() const;
179 
185  public: Ogre::TerrainGroup::RayResult GetMouseHit(CameraPtr _camera,
186  math::Vector2i _mousePos);
187 
192  public: void SplitHeights(const std::vector<float> &_heightmap, int _n,
193  std::vector<std::vector<float> > &_v);
194 
198  public: unsigned int GetTerrainSubdivisionCount() const;
199 
207  private: void ModifyTerrain(Ogre::Vector3 _pos, double _outsideRadius,
208  double _insideRadius, double _weight,
209  const std::string &_op);
210 
213  private: bool InitBlendMaps(Ogre::Terrain *_terrain);
214 
216  private: void ConfigureTerrainDefaults();
217 
221  private: void DefineTerrain(int _x, int _y);
222 
225  private: void SetupShadows(bool _enabled);
226 
234  private: void UpdateTerrainHash(const std::string &_hash,
235  const boost::filesystem::path &_terrainDir);
236 
243  private: bool PrepareTerrainPaging(
244  const boost::filesystem::path &_terrainDirPath);
245 
247  private: static const unsigned int numTerrainSubdivisions;
248 
253  private: static const double loadRadiusFactor;
254 
260  private: static const double holdRadiusFactor;
261 
264  private: static const boost::filesystem::path hashFilename;
265 
268  private: static const boost::filesystem::path pagingDirname;
269 
273  private: boost::filesystem::path gzPagingDir;
274 
276  private: ScenePtr scene;
277 
279  private: common::Image heightImage;
280 
282  private: math::Vector3 terrainSize;
283 
285  private: unsigned int dataSize;
286 
288  private: math::Vector3 terrainOrigin;
289 
291  private: Ogre::TerrainGlobalOptions *terrainGlobals;
292 
294  private: Ogre::TerrainGroup *terrainGroup;
295 
297  private: bool terrainsImported;
298 
300  private: std::vector<std::string> diffuseTextures;
301 
303  private: std::vector<std::string> normalTextures;
304 
306  private: std::vector<double> worldSizes;
307 
309  private: std::vector<double> blendHeight;
310 
312  private: std::vector<double> blendFade;
313 
315  private: std::vector<float> heights;
316 
318  private: GzTerrainMatGen *gzMatGen;
319 
321  private: DummyPageProvider dummyPageProvider;
322 
325  private: Ogre::PageManager *pageManager;
326 
328  private: Ogre::TerrainPaging *terrainPaging;
329 
331  private: Ogre::PagedWorld *world;
332 
334  private: std::vector<std::vector<float> > subTerrains;
335 
337  private: int terrainIdx;
338 
340  private: bool useTerrainPaging;
341 
343  private: bool terrainHashChanged;
344  };
346 
350  : public Ogre::TerrainMaterialGeneratorA
351  {
353  public: GzTerrainMatGen();
354 
356  public: virtual ~GzTerrainMatGen();
357 
359  public: class SM2Profile :
360  public Ogre::TerrainMaterialGeneratorA::SM2Profile
361  {
363  public: SM2Profile(Ogre::TerrainMaterialGenerator *_parent,
364  const Ogre::String &_name, const Ogre::String &_desc);
365 
367  public: virtual ~SM2Profile();
368 
369  public: Ogre::MaterialPtr generate(const Ogre::Terrain *_terrain);
370 
371  public: Ogre::MaterialPtr generateForCompositeMap(
372  const Ogre::Terrain *_terrain);
373 
374  public: void UpdateParams(const Ogre::MaterialPtr &_mat,
375  const Ogre::Terrain *_terrain);
376 
377  public: void UpdateParamsForCompositeMap(const Ogre::MaterialPtr &_mat,
378  const Ogre::Terrain *_terrain);
379 
380  protected: virtual void addTechnique(const Ogre::MaterialPtr &_mat,
381  const Ogre::Terrain *_terrain, TechniqueType _tt);
382 
383 #ifdef __clang__
384 #pragma clang diagnostic push
385 #pragma clang diagnostic ignored "-Woverloaded-virtual"
386 #endif // ifdef __clang__
387  protected: class ShaderHelperGLSL :
389  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperGLSL
390  {
391  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
392  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
393  TechniqueType _tt);
394 
395  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
396  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
397  TechniqueType _tt);
398 
399  public: virtual void updateParams(const SM2Profile *_prof,
400  const Ogre::MaterialPtr &_mat,
401  const Ogre::Terrain *_terrain, bool _compositeMap);
402 
403  protected: virtual void generateVpHeader(const SM2Profile *_prof,
404  const Ogre::Terrain *_terrain, TechniqueType _tt,
405  Ogre::StringUtil::StrStreamType &_outStream);
406 
407  protected: virtual void generateVpFooter(const SM2Profile *_prof,
408  const Ogre::Terrain *_terrain, TechniqueType _tt,
409  Ogre::StringUtil::StrStreamType &_outStream);
410 
411  protected: virtual void generateVertexProgramSource(
412  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
413  TechniqueType _tt,
414  Ogre::StringUtil::StrStreamType &_outStream);
415 
416  protected: virtual void defaultVpParams(const SM2Profile *_prof,
417  const Ogre::Terrain *_terrain, TechniqueType _tt,
418  const Ogre::HighLevelGpuProgramPtr &_prog);
419 
420  protected: virtual unsigned int generateVpDynamicShadowsParams(
421  unsigned int _texCoordStart, const SM2Profile *_prof,
422  const Ogre::Terrain *_terrain, TechniqueType _tt,
423  Ogre::StringUtil::StrStreamType &_outStream);
424 
425  protected: virtual void generateVpDynamicShadows(
426  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
427  TechniqueType _tt,
428  Ogre::StringUtil::StrStreamType &_outStream);
429 
430  protected: virtual void generateFpHeader(const SM2Profile *_prof,
431  const Ogre::Terrain *_terrain,
432  TechniqueType tt,
433  Ogre::StringUtil::StrStreamType &_outStream);
434 
435  protected: virtual void generateFpLayer(const SM2Profile *_prof,
436  const Ogre::Terrain *_terrain, TechniqueType tt,
437  Ogre::uint _layer,
438  Ogre::StringUtil::StrStreamType &_outStream);
439 
440  protected: virtual void generateFpFooter(const SM2Profile *_prof,
441  const Ogre::Terrain *_terrain,
442  TechniqueType tt,
443  Ogre::StringUtil::StrStreamType &_outStream);
444 
445  protected: virtual void generateFpDynamicShadowsParams(
446  Ogre::uint *_texCoord, Ogre::uint *_sampler,
447  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
448  TechniqueType _tt,
449  Ogre::StringUtil::StrStreamType &_outStream);
450 
451  protected: virtual void generateFpDynamicShadowsHelpers(
452  const SM2Profile *_prof,
453  const Ogre::Terrain *_terrain,
454  TechniqueType tt,
455  Ogre::StringUtil::StrStreamType &_outStream);
456 
457  protected: void generateFpDynamicShadows(const SM2Profile *_prof,
458  const Ogre::Terrain *_terrain, TechniqueType _tt,
459  Ogre::StringUtil::StrStreamType &_outStream);
460 
461  protected: virtual void generateFragmentProgramSource(
462  const SM2Profile *_prof,
463  const Ogre::Terrain *_terrain,
464  TechniqueType _tt,
465  Ogre::StringUtil::StrStreamType &_outStream);
466 
467  protected: virtual void updateVpParams(const SM2Profile *_prof,
468  const Ogre::Terrain *_terrain, TechniqueType _tt,
469  const Ogre::GpuProgramParametersSharedPtr &_params);
470 
471  private: Ogre::String GetChannel(Ogre::uint _idx);
472  };
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::StringUtil::StrStreamType &_outStream);
490 
491  protected: virtual void generateVpFooter(const SM2Profile *_prof,
492  const Ogre::Terrain *_terrain, TechniqueType _tt,
493  Ogre::StringUtil::StrStreamType &_outStream);
494 
495  protected: virtual void generateVertexProgramSource(
496  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
497  TechniqueType _tt,
498  Ogre::StringUtil::StrStreamType &_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::StringUtil::StrStreamType &_outStream);
508 
509  protected: virtual void generateVpDynamicShadows(
510  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
511  TechniqueType _tt,
512  Ogre::StringUtil::StrStreamType &_outStream);
513  };
514 #ifdef __clang__
515 #pragma clang diagnostic pop
516 #endif // ifdef __clang__
517  };
518  };
519  }
520 }
521 #endif
bool unloadProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to unload page content procedurally.
Definition: Heightmap.hh:69
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:43
Generic integer x, y vector.
Definition: Vector2i.hh:39
Shader model 2 profile target.
Definition: Heightmap.hh:359
boost::shared_ptr< Camera > CameraPtr
Definition: RenderTypes.hh:82
bool loadProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to load page content procedurally.
Definition: Heightmap.hh:62
Rendering a terrain using heightmap information.
Definition: Heightmap.hh:88
bool prepareProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to prepare page content procedurally.
Definition: Heightmap.hh:55
boost::shared_ptr< Scene > ScenePtr
Definition: RenderTypes.hh:74
Utility class to help with generating shaders for GLSL.
Definition: Heightmap.hh:388
Definition: Heightmap.hh:349
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:51
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
bool unprepareProceduralPage(Ogre::Page *, Ogre::PagedWorldSection *)
Give a provider the opportunity to unprepare page content procedurally.
Definition: Heightmap.hh:77