]> git.mxchange.org Git - flightgear.git/blob - Simulator/Objects/fragment.hxx
Initial revision.
[flightgear.git] / Simulator / 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
23
24 #ifndef _FRAGMENT_HXX
25 #define _FRAGMENT_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
47 #include <Include/fg_constants.h>
48 #include <Math/mat3.h>
49 #include <Math/point3d.hxx>
50
51 FG_USING_STD(vector);
52
53
54 struct fgFACE {
55     int n1, n2, n3;
56
57     fgFACE( int a = 0, int b =0, int c =0 )
58         : n1(a), n2(b), n3(c) {}
59
60     fgFACE( const fgFACE & image )
61         : n1(image.n1), n2(image.n2), n3(image.n3) {}
62
63     fgFACE& operator= ( const fgFACE & image ) {
64         n1 = image.n1; n2 = image.n2; n3 = image.n3; return *this;
65     }
66
67     ~fgFACE() {}
68 };
69
70 inline bool
71 operator== ( const fgFACE& lhs, const fgFACE& rhs )
72 {
73     return (lhs.n1 == rhs.n1) && (lhs.n2 == rhs.n2) && (lhs.n3 == rhs.n3);
74 }
75
76 // Forward declarations
77 class fgTILE;
78 class fgMATERIAL;
79
80 // Object fragment data class
81 class fgFRAGMENT {
82
83 private:
84
85 public:
86     // culling data for this object fragment (fine grain culling)
87     Point3D 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 public:
117
118     // number of faces in this fragment
119     int num_faces() {
120         return faces.size();
121     }
122
123     // Add a face to the face list
124     void add_face(int n1, int n2, int n3) {
125         faces.push_back( fgFACE(n1,n2,n3) );
126     }
127
128     // test if line intesects with this fragment.  p0 and p1 are the
129     // two line end points of the line.  If side_flag is true, check
130     // to see that end points are on opposite sides of face.  Returns
131     // 1 if it intersection found, 0 otherwise.  If it intesects,
132     // result is the point of intersection
133     int intersect( const Point3D& end0,
134                    const Point3D& end1,
135                    int side_flag,
136                    Point3D& result) const;
137
138     // Constructors
139     fgFRAGMENT () { /*faces.reserve(512);*/}
140     fgFRAGMENT ( const fgFRAGMENT &image );
141
142     // Destructor
143     ~fgFRAGMENT() { faces.erase( faces.begin(), faces.end() ); }
144
145     // operators
146     fgFRAGMENT & operator = ( const fgFRAGMENT & rhs );
147
148     bool operator <  ( const fgFRAGMENT & rhs ) const {
149         // This is completely arbitrary. It satisfies RW's STL implementation
150         return bounding_radius < rhs.bounding_radius;
151     }
152
153     void init() {
154         faces.erase( faces.begin(), faces.end() );
155     }
156
157     int deleteDisplayList() {
158         xglDeleteLists( display_list, 1 ); return 0;
159     }
160
161     friend bool operator== ( const fgFRAGMENT & lhs, const fgFRAGMENT & rhs );
162 };
163
164 inline bool
165 operator == ( const fgFRAGMENT & lhs, const fgFRAGMENT & rhs ) {
166     return lhs.center == rhs.center;
167 }
168
169
170 #endif // _FRAGMENT_HXX 
171
172