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 2013 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 
201  private: void ModifyTerrain(Ogre::Vector3 _pos, double _outsideRadius,
202  double _insideRadius, double _weight,
203  const std::string &_op);
204 
207  private: bool InitBlendMaps(Ogre::Terrain *_terrain);
208 
210  private: void ConfigureTerrainDefaults();
211 
215  private: void DefineTerrain(int _x, int _y);
216 
219  private: void SetupShadows(bool _enabled);
220 
222  public: static const unsigned int NumTerrainSubdivisions;
223 
225  private: boost::filesystem::path pagingPath;
226 
231  private: static const double LoadRadiusFactor;
232 
238  private: static const double HoldRadiusFactor;
239 
241  private: ScenePtr scene;
242 
244  private: common::Image heightImage;
245 
247  private: math::Vector3 terrainSize;
248 
250  private: unsigned int dataSize;
251 
253  private: math::Vector3 terrainOrigin;
254 
256  private: Ogre::TerrainGlobalOptions *terrainGlobals;
257 
259  private: Ogre::TerrainGroup *terrainGroup;
260 
262  private: bool terrainsImported;
263 
265  private: std::vector<std::string> diffuseTextures;
266 
268  private: std::vector<std::string> normalTextures;
269 
271  private: std::vector<double> worldSizes;
272 
274  private: std::vector<double> blendHeight;
275 
277  private: std::vector<double> blendFade;
278 
280  private: std::vector<float> heights;
281 
283  private: GzTerrainMatGen *gzMatGen;
284 
286  private: DummyPageProvider dummyPageProvider;
287 
290  private: Ogre::PageManager *pageManager;
291 
293  private: Ogre::TerrainPaging *terrainPaging;
294 
296  private: Ogre::PagedWorld *world;
297 
299  private: std::vector<std::vector<float> > subTerrains;
300 
302  private: int terrainIdx;
303 
305  private: bool useTerrainPaging;
306  };
308 
311  class GzTerrainMatGen : public Ogre::TerrainMaterialGeneratorA
312  {
314  public: GzTerrainMatGen();
315 
317  public: virtual ~GzTerrainMatGen();
318 
320  public: class SM2Profile :
321  public Ogre::TerrainMaterialGeneratorA::SM2Profile
322  {
324  public: SM2Profile(Ogre::TerrainMaterialGenerator *_parent,
325  const Ogre::String &_name, const Ogre::String &_desc);
326 
328  public: virtual ~SM2Profile();
329 
330  public: Ogre::MaterialPtr generate(const Ogre::Terrain *_terrain);
331 
332  public: Ogre::MaterialPtr generateForCompositeMap(
333  const Ogre::Terrain *_terrain);
334 
335  public: void UpdateParams(const Ogre::MaterialPtr &_mat,
336  const Ogre::Terrain *_terrain);
337 
338  public: void UpdateParamsForCompositeMap(const Ogre::MaterialPtr &_mat,
339  const Ogre::Terrain *_terrain);
340 
341  protected: virtual void addTechnique(const Ogre::MaterialPtr &_mat,
342  const Ogre::Terrain *_terrain, TechniqueType _tt);
343 
345  protected: class ShaderHelperGLSL :
346  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperGLSL
347  {
348  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
349  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
350  TechniqueType _tt);
351 
352  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
353  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
354  TechniqueType _tt);
355 
356  public: virtual void updateParams(const SM2Profile *_prof,
357  const Ogre::MaterialPtr &_mat,
358  const Ogre::Terrain *_terrain, bool _compositeMap);
359 
360  protected: virtual void generateVpHeader(const SM2Profile *_prof,
361  const Ogre::Terrain *_terrain, TechniqueType _tt,
362  Ogre::StringUtil::StrStreamType &_outStream);
363 
364  protected: virtual void generateVpFooter(const SM2Profile *_prof,
365  const Ogre::Terrain *_terrain, TechniqueType _tt,
366  Ogre::StringUtil::StrStreamType &_outStream);
367 
368  protected: virtual void generateVertexProgramSource(
369  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
370  TechniqueType _tt,
371  Ogre::StringUtil::StrStreamType &_outStream);
372 
373  protected: virtual void defaultVpParams(const SM2Profile *_prof,
374  const Ogre::Terrain *_terrain, TechniqueType _tt,
375  const Ogre::HighLevelGpuProgramPtr &_prog);
376 
377  protected: virtual unsigned int generateVpDynamicShadowsParams(
378  unsigned int _texCoordStart, const SM2Profile *_prof,
379  const Ogre::Terrain *_terrain, TechniqueType _tt,
380  Ogre::StringUtil::StrStreamType &_outStream);
381 
382  protected: virtual void generateVpDynamicShadows(
383  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
384  TechniqueType _tt,
385  Ogre::StringUtil::StrStreamType &_outStream);
386 
387  protected: virtual void generateFpHeader(const SM2Profile *_prof,
388  const Ogre::Terrain *_terrain,
389  TechniqueType tt,
390  Ogre::StringUtil::StrStreamType &_outStream);
391 
392  protected: virtual void generateFpLayer(const SM2Profile *_prof,
393  const Ogre::Terrain *_terrain, TechniqueType tt,
394  Ogre::uint _layer,
395  Ogre::StringUtil::StrStreamType &_outStream);
396 
397  protected: virtual void generateFpFooter(const SM2Profile *_prof,
398  const Ogre::Terrain *_terrain,
399  TechniqueType tt,
400  Ogre::StringUtil::StrStreamType &_outStream);
401 
402  protected: virtual void generateFpDynamicShadowsParams(
403  Ogre::uint *_texCoord, Ogre::uint *_sampler,
404  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
405  TechniqueType _tt,
406  Ogre::StringUtil::StrStreamType &_outStream);
407 
408  protected: virtual void generateFpDynamicShadowsHelpers(
409  const SM2Profile *_prof,
410  const Ogre::Terrain *_terrain,
411  TechniqueType tt,
412  Ogre::StringUtil::StrStreamType &_outStream);
413 
414  protected: void generateFpDynamicShadows(const SM2Profile *_prof,
415  const Ogre::Terrain *_terrain, TechniqueType _tt,
416  Ogre::StringUtil::StrStreamType &_outStream);
417 
418  protected: virtual void generateFragmentProgramSource(
419  const SM2Profile *_prof,
420  const Ogre::Terrain *_terrain,
421  TechniqueType _tt,
422  Ogre::StringUtil::StrStreamType &_outStream);
423 
424  protected: virtual void updateVpParams(const SM2Profile *_prof,
425  const Ogre::Terrain *_terrain, TechniqueType _tt,
426  const Ogre::GpuProgramParametersSharedPtr &_params);
427 
428  private: Ogre::String GetChannel(Ogre::uint _idx);
429  };
430 
433  protected: class ShaderHelperCg :
434  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperCg
435  {
436  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
437  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
438  TechniqueType _tt);
439 
440  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
441  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
442  TechniqueType _tt);
443 
444  protected: virtual void generateVpHeader(const SM2Profile *_prof,
445  const Ogre::Terrain *_terrain, TechniqueType _tt,
446  Ogre::StringUtil::StrStreamType &_outStream);
447 
448  protected: virtual void generateVpFooter(const SM2Profile *_prof,
449  const Ogre::Terrain *_terrain, TechniqueType _tt,
450  Ogre::StringUtil::StrStreamType &_outStream);
451 
452  protected: virtual void generateVertexProgramSource(
453  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
454  TechniqueType _tt,
455  Ogre::StringUtil::StrStreamType &_outStream);
456 
457  protected: virtual void defaultVpParams(const SM2Profile *_prof,
458  const Ogre::Terrain *_terrain, TechniqueType _tt,
459  const Ogre::HighLevelGpuProgramPtr &_prog);
460 
461  protected: virtual unsigned int generateVpDynamicShadowsParams(
462  unsigned int _texCoordStart, const SM2Profile *_prof,
463  const Ogre::Terrain *_terrain, TechniqueType _tt,
464  Ogre::StringUtil::StrStreamType &_outStream);
465 
466  protected: virtual void generateVpDynamicShadows(
467  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
468  TechniqueType _tt,
469  Ogre::StringUtil::StrStreamType &_outStream);
470  };
471  };
472  };
473  }
474 }
475 #endif