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 2012 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 
29 
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 
53  class Heightmap
54  {
57  public: Heightmap(ScenePtr _scene);
58 
60  public: virtual ~Heightmap();
61 
63  public: void Load();
64 
67  public: void LoadFromMsg(ConstVisualPtr &_msg);
68 
74  public: double GetHeight(double _x, double _y, double _z = 1000);
75 
85  public: bool Flatten(CameraPtr _camera, math::Vector2i _mousePos,
86  double _outsideRadius, double _insideRadius,
87  double _weight = 0.1);
88 
98  public: bool Smooth(CameraPtr _camera, math::Vector2i _mousePos,
99  double _outsideRadius, double _insideRadius,
100  double _weight = 0.1);
101 
111  public: bool Raise(CameraPtr _camera, math::Vector2i _mousePos,
112  double _outsideRadius, double _insideRadius,
113  double _weight = 0.1);
114 
124  public: bool Lower(CameraPtr _camera, math::Vector2i _mousePos,
125  double _outsideRadius, double _insideRadius,
126  double _weight = 0.1);
127 
131  public: double GetAvgHeight(Ogre::Vector3 _pos, double _brushSize);
132 
135  public: void SetWireframe(bool _show);
136 
139  public: Ogre::TerrainGroup *GetOgreTerrain() const;
140 
143  public: common::Image GetImage() const;
144 
150  public: Ogre::TerrainGroup::RayResult GetMouseHit(CameraPtr _camera,
151  math::Vector2i _mousePos);
152 
160  private: void ModifyTerrain(Ogre::Vector3 _pos, double _outsideRadius,
161  double _insideRadius, double _weight,
162  const std::string &_op);
163 
166  private: bool InitBlendMaps(Ogre::Terrain *_terrain);
167 
169  private: void ConfigureTerrainDefaults();
170 
174  private: void DefineTerrain(int _x, int _y);
175 
178  private: void SetupShadows(bool _enabled);
179 
181  private: ScenePtr scene;
182 
184  private: common::Image heightImage;
185 
187  private: math::Vector3 terrainSize;
188 
190  private: unsigned int dataSize;
191 
193  private: math::Vector3 terrainOrigin;
194 
196  private: Ogre::TerrainGlobalOptions *terrainGlobals;
197 
199  private: Ogre::TerrainGroup *terrainGroup;
200 
202  private: bool terrainsImported;
203 
205  private: std::vector<std::string> diffuseTextures;
206 
208  private: std::vector<std::string> normalTextures;
209 
211  private: std::vector<double> worldSizes;
212 
214  private: std::vector<double> blendHeight;
215 
217  private: std::vector<double> blendFade;
218 
220  private: std::vector<float> heights;
221 
223  private: GzTerrainMatGen *gzMatGen;
224  };
226 
229  class GzTerrainMatGen : public Ogre::TerrainMaterialGeneratorA
230  {
232  public: GzTerrainMatGen();
233 
235  public: virtual ~GzTerrainMatGen();
236 
238  public: class SM2Profile :
239  public Ogre::TerrainMaterialGeneratorA::SM2Profile
240  {
242  public: SM2Profile(Ogre::TerrainMaterialGenerator *_parent,
243  const Ogre::String &_name, const Ogre::String &_desc);
244 
246  public: virtual ~SM2Profile();
247 
248  public: Ogre::MaterialPtr generate(const Ogre::Terrain *_terrain);
249 
250  public: Ogre::MaterialPtr generateForCompositeMap(
251  const Ogre::Terrain *_terrain);
252 
253  public: void UpdateParams(const Ogre::MaterialPtr &_mat,
254  const Ogre::Terrain *_terrain);
255 
256  public: void UpdateParamsForCompositeMap(const Ogre::MaterialPtr &_mat,
257  const Ogre::Terrain *_terrain);
258 
259  protected: virtual void addTechnique(const Ogre::MaterialPtr &_mat,
260  const Ogre::Terrain *_terrain, TechniqueType _tt);
261 
263  protected: class ShaderHelperGLSL :
264  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperGLSL
265  {
266  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
267  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
268  TechniqueType _tt);
269 
270  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
271  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
272  TechniqueType _tt);
273 
274  public: virtual void updateParams(const SM2Profile *_prof,
275  const Ogre::MaterialPtr &_mat,
276  const Ogre::Terrain *_terrain, bool _compositeMap);
277 
278  protected: virtual void generateVpHeader(const SM2Profile *_prof,
279  const Ogre::Terrain *_terrain, TechniqueType _tt,
280  Ogre::StringUtil::StrStreamType &_outStream);
281 
282  protected: virtual void generateVpFooter(const SM2Profile *_prof,
283  const Ogre::Terrain *_terrain, TechniqueType _tt,
284  Ogre::StringUtil::StrStreamType &_outStream);
285 
286  protected: virtual void generateVertexProgramSource(
287  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
288  TechniqueType _tt,
289  Ogre::StringUtil::StrStreamType &_outStream);
290 
291  protected: virtual void defaultVpParams(const SM2Profile *_prof,
292  const Ogre::Terrain *_terrain, TechniqueType _tt,
293  const Ogre::HighLevelGpuProgramPtr &_prog);
294 
295  protected: virtual unsigned int generateVpDynamicShadowsParams(
296  unsigned int _texCoordStart, const SM2Profile *_prof,
297  const Ogre::Terrain *_terrain, TechniqueType _tt,
298  Ogre::StringUtil::StrStreamType &_outStream);
299 
300  protected: virtual void generateVpDynamicShadows(
301  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
302  TechniqueType _tt,
303  Ogre::StringUtil::StrStreamType &_outStream);
304 
305  protected: virtual void generateFpHeader(const SM2Profile *_prof,
306  const Ogre::Terrain *_terrain,
307  TechniqueType tt,
308  Ogre::StringUtil::StrStreamType &_outStream);
309 
310  protected: virtual void generateFpLayer(const SM2Profile *_prof,
311  const Ogre::Terrain *_terrain, TechniqueType tt,
312  Ogre::uint _layer,
313  Ogre::StringUtil::StrStreamType &_outStream);
314 
315  protected: virtual void generateFpFooter(const SM2Profile *_prof,
316  const Ogre::Terrain *_terrain,
317  TechniqueType tt,
318  Ogre::StringUtil::StrStreamType &_outStream);
319 
320  protected: virtual void generateFpDynamicShadowsParams(
321  Ogre::uint *_texCoord, Ogre::uint *_sampler,
322  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
323  TechniqueType _tt,
324  Ogre::StringUtil::StrStreamType &_outStream);
325 
326  protected: virtual void generateFpDynamicShadowsHelpers(
327  const SM2Profile *_prof,
328  const Ogre::Terrain *_terrain,
329  TechniqueType tt,
330  Ogre::StringUtil::StrStreamType &_outStream);
331 
332  protected: void generateFpDynamicShadows(const SM2Profile *_prof,
333  const Ogre::Terrain *_terrain, TechniqueType _tt,
334  Ogre::StringUtil::StrStreamType &_outStream);
335 
336  protected: virtual void generateFragmentProgramSource(
337  const SM2Profile *_prof,
338  const Ogre::Terrain *_terrain,
339  TechniqueType _tt,
340  Ogre::StringUtil::StrStreamType &_outStream);
341 
342  protected: virtual void updateVpParams(const SM2Profile *_prof,
343  const Ogre::Terrain *_terrain, TechniqueType _tt,
344  const Ogre::GpuProgramParametersSharedPtr &_params);
345 
346  private: Ogre::String GetChannel(Ogre::uint _idx);
347  };
348 
351  protected: class ShaderHelperCg :
352  public Ogre::TerrainMaterialGeneratorA::SM2Profile::ShaderHelperCg
353  {
354  public: virtual Ogre::HighLevelGpuProgramPtr generateFragmentProgram(
355  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
356  TechniqueType _tt);
357 
358  public: virtual Ogre::HighLevelGpuProgramPtr generateVertexProgram(
359  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
360  TechniqueType _tt);
361 
362  protected: virtual void generateVpHeader(const SM2Profile *_prof,
363  const Ogre::Terrain *_terrain, TechniqueType _tt,
364  Ogre::StringUtil::StrStreamType &_outStream);
365 
366  protected: virtual void generateVpFooter(const SM2Profile *_prof,
367  const Ogre::Terrain *_terrain, TechniqueType _tt,
368  Ogre::StringUtil::StrStreamType &_outStream);
369 
370  protected: virtual void generateVertexProgramSource(
371  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
372  TechniqueType _tt,
373  Ogre::StringUtil::StrStreamType &_outStream);
374 
375  protected: virtual void defaultVpParams(const SM2Profile *_prof,
376  const Ogre::Terrain *_terrain, TechniqueType _tt,
377  const Ogre::HighLevelGpuProgramPtr &_prog);
378 
379  protected: virtual unsigned int generateVpDynamicShadowsParams(
380  unsigned int _texCoordStart, const SM2Profile *_prof,
381  const Ogre::Terrain *_terrain, TechniqueType _tt,
382  Ogre::StringUtil::StrStreamType &_outStream);
383 
384  protected: virtual void generateVpDynamicShadows(
385  const SM2Profile *_prof, const Ogre::Terrain *_terrain,
386  TechniqueType _tt,
387  Ogre::StringUtil::StrStreamType &_outStream);
388  };
389  };
390  };
391  }
392 }
393 #endif