]> git.mxchange.org Git - flightgear.git/blob - Objects/fragment.hxx
a0f2cce1f43b0b63d0da845830e334d23931f300
[flightgear.git] / Objects / fragment.hxx
1 // fragment.hxx -- routines to handle "atomic" display objects
2 //
3 // Written by Curtis Olson, started August 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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 // (Log is kept at end of this file)
23
24
25 #ifndef _FRAGMENT_HXX
26 #define _FRAGMENT_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <GL/glut.h>
42 #include <XGL/xgl.h>
43
44 #include <Include/compiler.h>
45
46 #include <vector>
47
48 #include <Bucket/bucketutils.h>
49 #include <Include/fg_constants.h>
50 #include <Math/mat3.h>
51 #include <Math/point3d.hxx>
52
53 FG_USING_STD(vector);
54
55 #ifdef FG_HAVE_NATIVE_SGI_COMPILERS
56 FG_USING_NAMESPACE(std);
57 #endif
58
59 // Maximum nodes per tile
60 #define MAX_NODES 2000
61
62
63 // Forward declarations
64 class fgTILE;
65 class fgMATERIAL;
66
67 // Object fragment data class
68 class fgFRAGMENT {
69
70 private:
71     struct fgFACE {
72         int n1, n2, n3;
73
74         fgFACE( int a = 0, int b =0, int c =0 )
75             : n1(a), n2(b), n3(c) {}
76
77         fgFACE( const fgFACE & image )
78             : n1(image.n1), n2(image.n2), n3(image.n3) {}
79
80         fgFACE& operator= ( const fgFACE & image ) {
81             n1 = image.n1; n2 = image.n2; n3 = image.n3; return *this;
82         }
83
84         ~fgFACE() {}
85     };
86
87 public:
88     // culling data for this object fragment (fine grain culling)
89     Point3D center;
90     double bounding_radius;
91
92     // variable offset data for this object fragment for this frame
93     // fgCartesianPoint3d tile_offset;
94
95     // saved transformation matrix for this fragment (used by renderer)
96     // GLfloat matrix[16];
97     
98     // tile_ptr & material_ptr are set so that when we traverse the
99     // list of fragments we can quickly reference back the tile or
100     // material property this fragment is assigned to.
101
102     // material property pointer
103     fgMATERIAL *material_ptr;
104
105     // tile pointer
106     fgTILE *tile_ptr;
107
108     // OpenGL display list for fragment data
109     GLint display_list;
110
111     // face list (this indexes into the master tile vertex list)
112     typedef vector < fgFACE > container;
113     typedef container::iterator iterator;
114     typedef container::const_iterator const_iterator;
115
116     container faces;
117
118 public:
119
120     // number of faces in this fragment
121     int num_faces() {
122         return faces.size();
123     }
124
125     // Add a face to the face list
126     void add_face(int n1, int n2, int n3) {
127         faces.push_back( fgFACE(n1,n2,n3) );
128     }
129
130     // test if line intesects with this fragment.  p0 and p1 are the
131     // two line end points of the line.  If side_flag is true, check
132     // to see that end points are on opposite sides of face.  Returns
133     // 1 if it intersection found, 0 otherwise.  If it intesects,
134     // result is the point of intersection
135     int intersect( const Point3D& end0,
136                    const Point3D& end1,
137                    int side_flag,
138                    Point3D& result) const;
139
140     // Constructors
141     fgFRAGMENT () { /*faces.reserve(512);*/}
142     fgFRAGMENT ( const fgFRAGMENT &image );
143
144     // Destructor
145     ~fgFRAGMENT() { faces.erase( faces.begin(), faces.end() ); }
146
147     // operators
148     fgFRAGMENT & operator = ( const fgFRAGMENT & rhs );
149
150     bool operator <  ( const fgFRAGMENT & rhs ) const {
151         // This is completely arbitrary. It satisfies RW's STL implementation
152         return bounding_radius < rhs.bounding_radius;
153     }
154
155     void init() {
156         faces.erase( faces.begin(), faces.end() );
157     }
158
159     int deleteDisplayList() const {
160         xglDeleteLists( display_list, 1 ); return 0;
161     }
162
163     friend bool operator== ( const fgFRAGMENT::fgFACE & lhs,
164                              const fgFRAGMENT::fgFACE & rhs );
165     friend bool operator== ( const fgFRAGMENT & lhs, const fgFRAGMENT & rhs );
166 };
167
168 inline bool
169 operator== ( const fgFRAGMENT::fgFACE& lhs,
170              const fgFRAGMENT::fgFACE& rhs )
171 {
172     return (lhs.n1 == rhs.n1) && (lhs.n2 == rhs.n2) && (lhs.n3 == rhs.n3);
173 }
174
175 inline bool
176 operator == ( const fgFRAGMENT & lhs, const fgFRAGMENT & rhs ) {
177     return lhs.center == rhs.center;
178 }
179
180
181 #endif // _FRAGMENT_HXX 
182
183
184 // $Log$
185 // Revision 1.8  1999/02/26 22:09:57  curt
186 // Added initial support for native SGI compilers.
187 //
188 // Revision 1.7  1998/11/02 18:29:00  curt
189 // Portability changes for the Borland compiler.
190 //
191 // Revision 1.6  1998/10/16 00:54:38  curt
192 // Converted to Point3D class.
193 //
194 // Revision 1.5  1998/09/15 01:35:04  curt
195 // cleaned up my fragment.num_faces hack :-) to use the STL (no need in
196 // duplicating work.)
197 // Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
198 // removed some unneeded stuff from fgTileMgrCurElev()
199 //
200 // Revision 1.4  1998/09/10 19:07:09  curt
201 // /Simulator/Objects/fragment.hxx
202 //   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
203 //
204 // ./Simulator/Objects/material.cxx
205 // ./Simulator/Objects/material.hxx
206 //   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
207 //   data members - that should keep the rabble happy :)
208 //
209 // ./Simulator/Scenery/tilemgr.cxx
210 //   In viewable() delay evaluation of eye[0] and eye[1] in until they're
211 //   actually needed.
212 //   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
213 //   method.
214 //
215 // ./Include/fg_stl_config.h
216 // ./Include/auto_ptr.hxx
217 //   Added support for g++ 2.7.
218 //   Further changes to other files are forthcoming.
219 //
220 // Brief summary of changes required for g++ 2.7.
221 //   operator->() not supported by iterators: use (*i).x instead of i->x
222 //   default template arguments not supported,
223 //   <functional> doesn't have mem_fun_ref() needed by callbacks.
224 //   some std include files have different names.
225 //   template member functions not supported.
226 //
227 // Revision 1.3  1998/09/08 21:40:44  curt
228 // Updates from Bernie Bright.
229 //
230 // Revision 1.2  1998/09/01 19:03:08  curt
231 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
232 //  - The new classes in libmisc.tgz define a stream interface into zlib.
233 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
234 //    to something more appropriate.  However you'll have to change the
235 //    include directives in all the other files.  Additionally you'll have
236 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
237 //
238 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
239 //    test so I've included the required changes in config.tgz.
240 //
241 //    There are a fair few changes to Simulator/Objects as I've moved
242 //    things around.  Loading tiles is quicker but thats not where the delay
243 //    is.  Tile loading takes a few tenths of a second per file on a P200
244 //    but it seems to be the post-processing that leads to a noticeable
245 //    blip in framerate.  I suppose its time to start profiling to see where
246 //    the delays are.
247 //
248 //    I've included a brief description of each archives contents.
249 //
250 // Lib/Misc/
251 //   zfstream.cxx
252 //   zfstream.hxx
253 //     C++ stream interface into zlib.
254 //     Taken from zlib-1.1.3/contrib/iostream/.
255 //     Minor mods for STL compatibility.
256 //     There's no copyright associated with these so I assume they're
257 //     covered by zlib's.
258 //
259 //   fgstream.cxx
260 //   fgstream.hxx
261 //     FlightGear input stream using gz_ifstream.  Tries to open the
262 //     given filename.  If that fails then filename is examined and a
263 //     ".gz" suffix is removed or appended and that file is opened.
264 //
265 //   stopwatch.hxx
266 //     A simple timer for benchmarking.  Not used in production code.
267 //     Taken from the Blitz++ project.  Covered by GPL.
268 //
269 //   strutils.cxx
270 //   strutils.hxx
271 //     Some simple string manipulation routines.
272 //
273 // Simulator/Airports/
274 //   Load airports database using fgstream.
275 //   Changed fgAIRPORTS to use set<> instead of map<>.
276 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
277 //   Returns true if found.
278 //
279 // Simulator/Astro/
280 //   Modified fgStarsInit() to load stars database using fgstream.
281 //
282 // Simulator/Objects/
283 //   Modified fgObjLoad() to use fgstream.
284 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
285 //   Many changes to fgMATERIAL.
286 //   Some changes to fgFRAGMENT but I forget what!
287 //
288 // Revision 1.1  1998/08/25 16:51:23  curt
289 // Moved from ../Scenery
290 //
291 //