]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matmodel.hxx
Modified Files:
[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
38 #include <simgear/structure/SGReferenced.hxx>
39 #include <simgear/structure/SGSharedPtr.hxx>
40 #include <simgear/props/props.hxx>
41
42 SG_USING_STD(string);
43
44
45 class SGMatModelGroup;
46 class SGModelLib;
47
48
49 /**
50  * A randomly-placeable object.
51  *
52  * SGMaterial uses this class to keep track of the model(s) and
53  * parameters for a single instance of a randomly-placeable object.
54  * The object can have more than one variant model (i.e. slightly
55  * different shapes of trees), but they are considered equivalent
56  * and interchangeable.
57  */
58 class SGMatModel : public SGReferenced {
59
60 public:
61
62     /**
63      * The heading type for a randomly-placed object.
64      */
65     enum HeadingType {
66         HEADING_FIXED,
67         HEADING_BILLBOARD,
68         HEADING_RANDOM
69     };
70
71     /**
72      * Get the number of variant models available for the object.
73      *
74      * @return The number of variant models.
75      */
76     int get_model_count( SGModelLib *modellib,
77                          const string &fg_root,
78                          SGPropertyNode *prop_root,
79                          double sim_time_sec );
80
81
82     /**
83      * Get a specific variant model for the object.
84      *
85      * @param index The index of the model.
86      * @return The model.
87      */
88      osg::Node *get_model( int index,
89                           SGModelLib *modellib,
90                           const string &fg_root,
91                           SGPropertyNode *prop_root,
92                           double sim_time_sec );
93
94
95     /**
96      * Get a randomly-selected variant model for the object.
97      *
98      * @return A randomly select model from the variants.
99      */
100     osg::Node *get_random_model( SGModelLib *modellib,
101                                  const string &fg_root,
102                                  SGPropertyNode *prop_root,
103                                  double sim_time_sec );
104
105
106     /**
107      * Get the average number of meters^2 occupied by each instance.
108      *
109      * @return The coverage in meters^2.
110      */
111     double get_coverage_m2 () const;
112
113
114     /**
115      * Get the heading type for the object.
116      *
117      * @return The heading type.
118      */
119     HeadingType get_heading_type () const;
120
121     virtual ~SGMatModel ();
122
123 protected:
124
125     friend class SGMatModelGroup;
126
127     SGMatModel (const SGPropertyNode * node, double range_m);
128
129 private:
130
131     /**
132      * Actually load the models.
133      *
134      * This class uses lazy loading so that models won't be held
135      * in memory for materials that are never referenced.
136      */
137     void load_models( SGModelLib *modellib,
138                       const string &fg_root,
139                       SGPropertyNode *prop_root,
140                       double sim_time_sec );
141
142     vector<string> _paths;
143     mutable vector<osg::ref_ptr<osg::Node> > _models;
144     mutable bool _models_loaded;
145     double _coverage_m2;
146     double _range_m;
147     HeadingType _heading_type;
148 };
149
150
151 /**
152  * A collection of related objects with the same visual range.
153  *
154  * Grouping objects with the same range together significantly
155  * reduces the memory requirements of randomly-placed objects.
156  * Each SGMaterial instance keeps a (possibly-empty) list of
157  * object groups for placing randomly on the scenery.
158  */
159 class SGMatModelGroup : public SGReferenced {
160
161 public:
162
163     virtual ~SGMatModelGroup ();
164
165
166     /**
167      * Get the visual range of the object in meters.
168      *
169      * @return The visual range.
170      */
171     double get_range_m () const;
172
173
174     /**
175      * Get the number of objects in the group.
176      *
177      * @return The number of objects.
178      */
179     int get_object_count () const;
180
181
182     /**
183      * Get a specific object.
184      *
185      * @param index The object's index, zero-based.
186      * @return The object selected.
187      */
188     SGMatModel * get_object (int index) const;
189
190 protected:
191
192     friend class SGMaterial;
193
194     SGMatModelGroup (SGPropertyNode * node);
195
196 private:
197
198     double _range_m;
199     vector<SGSharedPtr<SGMatModel> > _objects;
200 };
201
202
203 #endif // _SG_MAT_MODEL_HXX