]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.hxx
Initial revision.
[flightgear.git] / src / Scenery / tileentry.hxx
1 // tileentry.hxx -- routines to handle an individual scenery tile
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998, 1999  Curtis L. Olson  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _TILEENTRY_HXX
25 #define _TILEENTRY_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32 #ifdef HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #ifdef HAVE_WINDOWS_H
37 #  include <windows.h>
38 #endif
39
40 #include <GL/glut.h>
41 #include <simgear/xgl/xgl.h>
42
43 #include <simgear/compiler.h>
44
45 #include <vector>
46 #include STL_STRING
47
48 #include <plib/ssg.h>           // plib includes
49
50 #include <simgear/bucket/newbucket.hxx>
51 #include <simgear/math/point3d.hxx>
52
53 #include <Objects/fragment.hxx>
54
55 #if defined( sgi )
56 #include <strings.h>
57 #endif
58
59 FG_USING_STD(string);
60 FG_USING_STD(vector);
61
62
63 typedef vector < Point3D > point_list;
64 typedef point_list::iterator point_list_iterator;
65 typedef point_list::const_iterator const_point_list_iterator;
66
67
68 // Scenery tile class
69 class FGTileEntry {
70
71 private:
72
73     // Tile state
74     enum tile_state {
75         Unused = 0,
76         Scheduled_for_use = 1,
77         Scheduled_for_cache = 2,
78         Loaded = 3,
79         Cached = 4
80     };
81
82 public:
83
84     typedef vector < fgFRAGMENT > container;
85     typedef container::iterator FragmentIterator;
86     typedef container::const_iterator FragmentConstIterator;
87
88     typedef vector < sgVec3 * > free_vec3_list;
89     typedef vector < sgVec2 * > free_vec2_list;
90     typedef vector < unsigned short * > free_index_list;
91
92 public:
93     // node list (the per fragment face lists reference this node list)
94     point_list nodes;
95     int ncount;
96
97     // global tile culling data
98     Point3D center;
99     double bounding_radius;
100     Point3D offset;
101
102     // this tile's official location in the world
103     FGBucket tile_bucket;
104
105     // the tile cache will keep track here if the tile is being used
106     tile_state state;
107
108     container fragment_list;
109
110     // ssg related structures
111     // sgVec3 *vtlist;
112     // sgVec3 *vnlist;
113     // sgVec2 *tclist;
114
115     // list of pointers to memory chunks that need to be freed when
116     // tile entry goes away
117     free_vec3_list vec3_ptrs;
118     free_vec2_list vec2_ptrs;
119     free_index_list index_ptrs;
120
121     // ssg tree structure for this tile is as follows:
122     // ssgRoot(scene)
123     //     - ssgBranch(terrain)
124     //      - ssgSelector(tile)
125     //        - ssgTransform(tile)
126     //           - ssgRangeSelector(tile)
127     //              - ssgEntity(tile)
128     //                 - kid1(fan)
129     //                 - kid2(fan)
130     //                   ...
131     //                 - kidn(fan)
132
133     // selector (turn tile on/off)
134     ssgSelector *select_ptr;
135
136     // pointer to ssg transform for this tile
137     ssgTransform *transform_ptr;
138
139     // pointer to ssg range selector for this tile
140     ssgRangeSelector *range_ptr;
141
142 public:
143
144     // Constructor
145     FGTileEntry ( void );
146
147     // Destructor
148     ~FGTileEntry ( void );
149
150     FragmentIterator begin() { return fragment_list.begin(); }
151     FragmentConstIterator begin() const { return fragment_list.begin(); }
152
153     FragmentIterator end() { return fragment_list.end(); }
154     FragmentConstIterator end() const { return fragment_list.end(); }
155
156     void add_fragment( fgFRAGMENT& frag ) {
157         frag.tile_ptr = this;
158         fragment_list.push_back( frag );
159     }
160
161     size_t num_fragments() const {
162         return fragment_list.size();
163     }
164
165     // Step through the fragment list, deleting the display list, then
166     // the fragment, until the list is empty.  Also delete the arrays
167     // used by ssg as well as the whole ssg branch
168     void free_tile();
169
170     // Calculate this tile's offset
171     void SetOffset( const Point3D& p)
172     {
173         offset = center - p;
174     }
175
176     // Return this tile's offset
177     inline Point3D get_offset( void ) const { return offset; }
178         
179     inline bool is_unused() const { return state == Unused; }
180     inline bool is_scheduled_for_use() const { 
181         return state == Scheduled_for_use;
182     }
183     inline bool is_scheduled_for_cache() const {
184         return state == Scheduled_for_cache;
185     }
186     inline bool is_loaded() const { return state == Loaded; }
187     inline bool is_cached() const { return state == Cached; }
188
189     inline void mark_unused() { state = Unused; }
190     inline void mark_scheduled_for_use() { state = Scheduled_for_use; }
191     inline void mark_scheduled_for_cache() { state = Scheduled_for_use; }
192     inline void mark_loaded() { state = Loaded; }
193
194
195     // when a tile is still in the cache, but not in the immediate
196     // draw l ist, it can still remain in the scene graph, but we use
197     // a range selector to disable it from ever being drawn.
198     void ssg_disable();
199 };
200
201
202 typedef vector < FGTileEntry > tile_list;
203 typedef tile_list::iterator tile_list_iterator;
204 typedef tile_list::const_iterator const_tile_list_iterator;
205
206
207 #endif // _TILEENTRY_HXX