]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.hxx
5b6824d6f27ab463c00839c32112b4122f8161c8
[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 <XGL/xgl.h>
42
43 #include <Include/compiler.h>
44
45 #include <vector>
46 #include STL_STRING
47
48 #include <ssg.h>                // plib includes
49
50 #include <Bucket/newbucket.hxx>
51 #include <Math/point3d.hxx>
52 #include <Objects/fragment.hxx>
53
54 #ifdef FG_HAVE_NATIVE_SGI_COMPILERS
55 #include <strings.h>
56 #endif
57
58 FG_USING_STD(string);
59 FG_USING_STD(vector);
60
61
62 typedef vector < Point3D > point_list;
63 typedef point_list::iterator point_list_iterator;
64 typedef point_list::const_iterator const_point_list_iterator;
65
66
67 // Scenery tile class
68 class FGTileEntry {
69
70 private:
71
72     // Tile state
73     enum tile_state {
74         Unused = 0,
75         Scheduled_for_use = 1,
76         Scheduled_for_cache = 2,
77         Loaded = 3,
78         Cached = 4
79     };
80
81 public:
82
83     typedef vector < fgFRAGMENT > container;
84     typedef container::iterator FragmentIterator;
85     typedef container::const_iterator FragmentConstIterator;
86
87 public:
88     // node list (the per fragment face lists reference this node list)
89     point_list nodes;
90     int ncount;
91
92     // global tile culling data
93     Point3D center;
94     double bounding_radius;
95     Point3D offset;
96
97     // model view matrix for this tile
98     GLfloat model_view[16];
99
100     // this tile's official location in the world
101     FGBucket tile_bucket;
102
103     // the tile cache will keep track here if the tile is being used
104     tile_state state;
105
106     container fragment_list;
107
108     // ssg related structures
109     sgVec3 *vtlist;
110     sgVec3 *vnlist;
111     sgVec2 *tclist;
112
113     // ssg tree structure for this tile is as follows:
114     // ssgRoot(scene)
115     //     - ssgBranch(terrain)
116     //      - ssgSelector(tile)
117     //        - ssgTransform(tile)
118     //           - ssgRangeSelector(tile)
119     //              - ssgEntity(tile)
120     //                 - kid1(fan)
121     //                 - kid2(fan)
122     //                   ...
123     //                 - kidn(fan)
124
125     // selector (turn tile on/off)
126     ssgSelector *select_ptr;
127
128     // pointer to ssg transform for this tile
129     ssgTransform *transform_ptr;
130
131     // pointer to ssg range selector for this tile
132     ssgRangeSelector *range_ptr;
133
134 public:
135
136     // Constructor
137     FGTileEntry ( void );
138
139     // Destructor
140     ~FGTileEntry ( void );
141
142     FragmentIterator begin() { return fragment_list.begin(); }
143     FragmentConstIterator begin() const { return fragment_list.begin(); }
144
145     FragmentIterator end() { return fragment_list.end(); }
146     FragmentConstIterator end() const { return fragment_list.end(); }
147
148     void add_fragment( fgFRAGMENT& frag ) {
149         frag.tile_ptr = this;
150         fragment_list.push_back( frag );
151     }
152
153     size_t num_fragments() const {
154         return fragment_list.size();
155     }
156
157     // Step through the fragment list, deleting the display list, then
158     // the fragment, until the list is empty.  Also delete the arrays
159     // used by ssg as well as the whole ssg branch
160     void free_tile();
161
162     // Calculate this tile's offset
163     void SetOffset( const Point3D& off)
164     {
165         offset = center - off;
166     }
167
168     // Return this tile's offset
169     inline Point3D get_offset( void ) const { return offset; }
170
171     // Calculate the model_view transformation matrix for this tile
172     inline void update_view_matrix(GLfloat *MODEL_VIEW)
173     {
174
175 #if defined( USE_MEM ) || defined( WIN32 )
176         memcpy( model_view, MODEL_VIEW, 16*sizeof(GLfloat) );
177 #else 
178         bcopy( MODEL_VIEW, model_view, 16*sizeof(GLfloat) );
179 #endif
180         
181         // This is equivalent to doing a glTranslatef(x, y, z);
182         model_view[12] += (model_view[0]*offset.x() +
183                            model_view[4]*offset.y() +
184                            model_view[8]*offset.z());
185         model_view[13] += (model_view[1]*offset.x() +
186                            model_view[5]*offset.y() +
187                            model_view[9]*offset.z());
188         model_view[14] += (model_view[2]*offset.x() +
189                            model_view[6]*offset.y() +
190                            model_view[10]*offset.z() );
191         // m[15] += (m[3]*x + m[7]*y + m[11]*z);
192         // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
193         // so m[15] is unchanged
194     }
195
196     inline bool is_unused() const { return state == Unused; }
197     inline bool is_scheduled_for_use() const { 
198         return state == Scheduled_for_use;
199     }
200     inline bool is_scheduled_for_cache() const {
201         return state == Scheduled_for_cache;
202     }
203     inline bool is_loaded() const { return state == Loaded; }
204     inline bool is_cached() const { return state == Cached; }
205
206     inline void mark_unused() { state = Unused; }
207     inline void mark_scheduled_for_use() { state = Scheduled_for_use; }
208     inline void mark_scheduled_for_cache() { state = Scheduled_for_use; }
209     inline void mark_loaded() { state = Loaded; }
210
211
212     // when a tile is still in the cache, but not in the immediate
213     // draw l ist, it can still remain in the scene graph, but we use
214     // a range selector to disable it from ever being drawn.
215     void ssg_disable();
216 };
217
218
219 typedef vector < FGTileEntry > tile_list;
220 typedef tile_list::iterator tile_list_iterator;
221 typedef tile_list::const_iterator const_tile_list_iterator;
222
223
224 #endif // _TILEENTRY_HXX