]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.hxx
More ssg tweaks.
[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/mat3.h>
52 #include <Math/point3d.hxx>
53 #include <Objects/fragment.hxx>
54
55 #ifdef FG_HAVE_NATIVE_SGI_COMPILERS
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 = 1,
77         Loaded = 2
78     };
79
80 public:
81
82     typedef vector < fgFRAGMENT > container;
83     typedef container::iterator FragmentIterator;
84     typedef container::const_iterator FragmentConstIterator;
85
86 public:
87     // node list (the per fragment face lists reference this node list)
88     point_list nodes;
89     int ncount;
90
91     // global tile culling data
92     Point3D center;
93     double bounding_radius;
94     Point3D offset;
95
96     // model view matrix for this tile
97     GLfloat model_view[16];
98
99     // this tile's official location in the world
100     FGBucket tile_bucket;
101
102     // the tile cache will keep track here if the tile is being used
103     tile_state state;
104
105     container fragment_list;
106
107     // ssg related structures
108     sgVec3 *vtlist;
109     sgVec3 *vnlist;
110     sgVec2 *tclist;
111
112     // pointer to ssg branch;
113     ssgTransform *branch_ptr;
114
115 public:
116
117     // Constructor
118     FGTileEntry ( void );
119
120     // Destructor
121     ~FGTileEntry ( void );
122
123     FragmentIterator begin() { return fragment_list.begin(); }
124     FragmentConstIterator begin() const { return fragment_list.begin(); }
125
126     FragmentIterator end() { return fragment_list.end(); }
127     FragmentConstIterator end() const { return fragment_list.end(); }
128
129     void add_fragment( fgFRAGMENT& frag ) {
130         frag.tile_ptr = this;
131         fragment_list.push_back( frag );
132     }
133
134     size_t num_fragments() const {
135         return fragment_list.size();
136     }
137
138     // Step through the fragment list, deleting the display list, then
139     // the fragment, until the list is empty.  Also delete the arrays
140     // used by ssg as well as the whole ssg branch
141     void free_tile();
142
143     // Calculate this tile's offset
144     void SetOffset( const Point3D& off)
145     {
146         offset = center - off;
147     }
148
149     // Return this tile's offset
150     inline Point3D get_offset( void ) const { return offset; }
151
152     // Calculate the model_view transformation matrix for this tile
153     inline void update_view_matrix(GLfloat *MODEL_VIEW)
154     {
155
156 #if defined( USE_MEM ) || defined( WIN32 )
157         memcpy( model_view, MODEL_VIEW, 16*sizeof(GLfloat) );
158 #else 
159         bcopy( MODEL_VIEW, model_view, 16*sizeof(GLfloat) );
160 #endif
161         
162         // This is equivalent to doing a glTranslatef(x, y, z);
163         model_view[12] += (model_view[0]*offset.x() +
164                            model_view[4]*offset.y() +
165                            model_view[8]*offset.z());
166         model_view[13] += (model_view[1]*offset.x() +
167                            model_view[5]*offset.y() +
168                            model_view[9]*offset.z());
169         model_view[14] += (model_view[2]*offset.x() +
170                            model_view[6]*offset.y() +
171                            model_view[10]*offset.z() );
172         // m[15] += (m[3]*x + m[7]*y + m[11]*z);
173         // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
174         // so m[15] is unchanged
175     }
176
177     inline bool is_unused() const { return state == Unused; }
178     inline bool is_loaded() const { return state == Loaded; }
179
180     inline void mark_unused() { state = Unused; }
181     inline void mark_scheduled() { state = Scheduled; }
182     inline void mark_loaded() { state = Loaded; }
183 };
184
185
186 typedef vector < FGTileEntry > tile_list;
187 typedef tile_list::iterator tile_list_iterator;
188 typedef tile_list::const_iterator const_tile_list_iterator;
189
190
191 #endif // _TILEENTRY_HXX