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