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