1 /**************************************************************************/
2 /*  Copyright 2009 Tim Day                                                */
3 /*                                                                        */
4 /*  This file is part of Fracplanet                                       */
5 /*                                                                        */
6 /*  Fracplanet is free software: you can redistribute it and/or modify    */
7 /*  it under the terms of the GNU General Public License as published by  */
8 /*  the Free Software Foundation, either version 3 of the License, or     */
9 /*  (at your option) any later version.                                   */
10 /*                                                                        */
11 /*  Fracplanet is distributed in the hope that it will be useful,         */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
14 /*  GNU General Public License for more details.                          */
15 /*                                                                        */
16 /*  You should have received a copy of the GNU General Public License     */
17 /*  along with Fracplanet.  If not, see <http://www.gnu.org/licenses/>.   */
18 /**************************************************************************/
19 
20 /*! \file
21   \brief Interface for class TriangleMeshTerrain and derived classes.
22 */
23 
24 #ifndef _triangle_mesh_terrain_h_
25 #define _triangle_mesh_terrain_h_
26 
27 #include <set>
28 
29 #include "common.h"
30 #include "image.h"
31 #include "parameters_terrain.h"
32 #include "triangle_mesh.h"
33 
34 //! This class holds all the terrain-related methods.
35 /*! It's intended to be used as a "mix-in", adding terrain generating
36   functionality to terrain objects subclassed from simpler geometries.
37   \todo Ugh!!!  This is really yucky use of multiple inheritance.  Better for these terrain types to have-a TriangleMesh.
38  */
39 class TriangleMeshTerrain : virtual public TriangleMesh
40 {
41  public:
42 
43   //! Constructor.
44   TriangleMeshTerrain(Progress* progress);
45 
46   //! Destructor.
47   ~TriangleMeshTerrain();
48 
49   //! Dump the model as a POV scene.
50   /*! Virtual method because spherical and flat terrains need e.g different sea-level planes and atmosphere layers.
51    */
52   virtual void write_povray(std::ofstream& out,const ParametersSave&,const ParametersTerrain&) const
53     =0;
54 
55   //! Dump the model for Blender.
56   /*! Unlike write_povray there are no specialisations for flat/spherical terrain.
57    */
58   virtual void write_blender(std::ofstream& out,const ParametersSave&,const ParametersTerrain&,const std::string& mesh_name) const;
59 
60   //! Render the mesh onto raster images (colour texture, and optionally 16-bit DEM and/or normal map).
61   virtual void render_texture(Raster<ByteRGBA>&,Raster<ushort>*,Raster<ByteRGBA>*,bool shading,float ambient,const XYZ& illumination) const;
62 
63  protected:
64 
65   //! Indices of the set of triangles with all vertices at sea-level
66   std::set<uint> sea_triangles;
67 
68   //! Indices of the set of vertices comprising the river network
69   std::set<uint> river_vertices;
70 
71   //! Maximum height of terrain (used to scale to/from "normalised" height).
72   float max_height;
73 
74   //! Add noise to the terrain
75   void do_noise(const ParametersTerrain& parameters);
76 
77   //! Impose a sea level (raise lower vertices, and note sea triangles).
78   void do_sea_level(const ParametersTerrain& parameters);
79 
80   //! Apply power law
81   void do_power_law(const ParametersTerrain& parameters);
82 
83   //! Generate river network.
84   void do_rivers(const ParametersTerrain& parameters);
85 
86   //! Final colouration pass.
87   void do_colours(const ParametersTerrain& parameters);
88 
89   //! Invokes all the above steps (sea-level through final colouring) on a pre-subdivided triangle mesh.
90   void do_terrain(const ParametersTerrain& parameters);
91 };
92 
93 //! Class constructing specific case of a planetary terrain.
94 class TriangleMeshTerrainPlanet : public TriangleMeshSubdividedIcosahedron, virtual public TriangleMeshTerrain
95 {
96  public:
97 
98   //! Constructor.
99   TriangleMeshTerrainPlanet(const ParametersTerrain& param,Progress* progress);
100 
101   //! Destructor.
~TriangleMeshTerrainPlanet()102   ~TriangleMeshTerrainPlanet()
103     {}
104 
105   //! Specifc dump-to-povray for planet terrain.
106   void write_povray(std::ofstream& out,const ParametersSave&,const ParametersTerrain&) const;
107 };
108 
109 //! Class constructing specific case of a flat-base terrain area.
110 class TriangleMeshTerrainFlat : public TriangleMeshFlat, virtual public TriangleMeshTerrain
111 {
112  public:
113 
114   //! Constructor.
115   TriangleMeshTerrainFlat(const ParametersTerrain& parameters,Progress* progress);
116 
117   //! Destructor.
~TriangleMeshTerrainFlat()118   ~TriangleMeshTerrainFlat()
119     {}
120 
121   //! Specifc dump-to-povray for flat terrain area.
122   void write_povray(std::ofstream& out,const ParametersSave&,const ParametersTerrain&) const;
123 };
124 
125 #endif
126