]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matmodel.hxx
982e89a95293015d6183071865df50ce5309d7ba
[simgear.git] / simgear / scene / material / matmodel.hxx
1 // matmodel.hxx -- class to handle models tied to a material property
2 //
3 // Written by David Megginson, December 2001
4 //
5 // Copyright (C) 1998 - 2003  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _SG_MAT_MODEL_HXX
25 #define _SG_MAT_MODEL_HXX
26
27 #ifndef __cplusplus
28 # error This library requires C++
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include STL_STRING      // Standard C++ string library
34
35 #include <osg/ref_ptr>
36 #include <osg/Node>
37 #include <osg/NodeVisitor>
38 #include <osg/Billboard>
39
40 #include <simgear/structure/SGReferenced.hxx>
41 #include <simgear/structure/SGSharedPtr.hxx>
42 #include <simgear/props/props.hxx>
43 #include <simgear/math/sg_random.h>
44
45 SG_USING_STD(string);
46
47
48 class SGMatModelGroup;
49 class SGModelLib;
50
51
52 /**
53  * A randomly-placeable object.
54  *
55  * SGMaterial uses this class to keep track of the model(s) and
56  * parameters for a single instance of a randomly-placeable object.
57  * The object can have more than one variant model (i.e. slightly
58  * different shapes of trees), but they are considered equivalent
59  * and interchangeable.
60  */
61 class SGMatModel : public SGReferenced {
62
63 public:
64
65     /**
66      * The heading type for a randomly-placed object.
67      */
68     enum HeadingType {
69         HEADING_FIXED,
70         HEADING_BILLBOARD,
71         HEADING_RANDOM
72     };
73
74     /**
75      * Get the number of variant models available for the object.
76      *
77      * @return The number of variant models.
78      */
79     int get_model_count( SGModelLib *modellib,
80                          const string &fg_root,
81                          SGPropertyNode *prop_root,
82                          double sim_time_sec );
83
84
85     /**
86      * Get a specific variant model for the object.
87      *
88      * @param index The index of the model.
89      * @return The model.
90      */
91      osg::Node *get_model( int index,
92                           SGModelLib *modellib,
93                           const string &fg_root,
94                           SGPropertyNode *prop_root,
95                           double sim_time_sec );
96
97
98     /**
99      * Get a randomly-selected variant model for the object.
100      *
101      * @return A randomly select model from the variants.
102      */
103     osg::Node *get_random_model( SGModelLib *modellib,
104                                  const string &fg_root,
105                                  SGPropertyNode *prop_root,
106                                  double sim_time_sec );
107
108
109     /**
110      * Get the average number of meters^2 occupied by each instance.
111      *
112      * @return The coverage in meters^2.
113      */
114     double get_coverage_m2 () const;
115
116     /**
117      * Get the visual range of the object in meters.
118      *
119      * @return The visual range.
120      */
121     double get_range_m () const;
122     
123     /**
124      * Get a randomized visual range
125      *
126      * @return a randomized visual range
127      */    
128     double get_randomized_range_m(mt* seed) const;    
129
130     /**
131      * Get the heading type for the object.
132      *
133      * @return The heading type.
134      */
135     HeadingType get_heading_type () const;
136
137     virtual ~SGMatModel ();
138     
139
140 protected:
141
142     friend class SGMatModelGroup;
143
144     SGMatModel (const SGPropertyNode * node, double range_m);
145
146 private:
147
148     /**
149      * Actually load the models.
150      *
151      * This class uses lazy loading so that models won't be held
152      * in memory for materials that are never referenced.
153      */
154     void load_models( SGModelLib *modellib,
155                       const string &fg_root,
156                       SGPropertyNode *prop_root,
157                       double sim_time_sec );
158
159     vector<string> _paths;
160     mutable vector<osg::ref_ptr<osg::Node> > _models;
161     mutable bool _models_loaded;
162     double _coverage_m2;
163     double _range_m;
164     HeadingType _heading_type;
165 };
166
167
168 /**
169  * A collection of related objects with the same visual range.
170  *
171  * Grouping objects with the same range together significantly
172  * reduces the memory requirements of randomly-placed objects.
173  * Each SGMaterial instance keeps a (possibly-empty) list of
174  * object groups for placing randomly on the scenery.
175  */
176 class SGMatModelGroup : public SGReferenced {
177
178 public:
179
180     virtual ~SGMatModelGroup ();
181
182
183     /**
184      * Get the visual range of the object in meters.
185      *
186      * @return The visual range.
187      */
188     double get_range_m () const;
189
190
191     /**
192      * Get the number of objects in the group.
193      *
194      * @return The number of objects.
195      */
196     int get_object_count () const;
197
198
199     /**
200      * Get a specific object.
201      *
202      * @param index The object's index, zero-based.
203      * @return The object selected.
204      */
205     SGMatModel * get_object (int index) const;
206
207 protected:
208
209     friend class SGMaterial;
210
211     SGMatModelGroup (SGPropertyNode * node);
212
213 private:
214
215     double _range_m;
216     vector<SGSharedPtr<SGMatModel> > _objects;
217 };
218
219 #endif // _SG_MAT_MODEL_HXX