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