]> git.mxchange.org Git - flightgear.git/blob - Simulator/Objects/material.hxx
Merge Include as subdirectory
[flightgear.git] / Simulator / Objects / material.hxx
1 // material.hxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 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 _MATERIAL_HXX
26 #define _MATERIAL_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 <string>        // Standard C++ string library
45 #include <map>           // STL associative "array"
46 #include <vector>        // STL "array"
47
48 #include "Include/compiler.h"
49 FG_USING_STD(string);
50 FG_USING_STD(map);
51 FG_USING_STD(vector);
52 FG_USING_STD(less);
53
54 // forward decl.
55 class fgFRAGMENT;
56
57
58 // convenience types
59 typedef vector < fgFRAGMENT * > frag_list_type;
60 typedef frag_list_type::iterator frag_list_iterator;
61 typedef frag_list_type::const_iterator frag_list_const_iterator;
62
63
64 // #define FG_MAX_MATERIAL_FRAGS 800
65
66 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
67 class fgMATERIAL;
68 istream& operator >> ( istream& in, fgMATERIAL& m );
69
70 // Material property class
71 class fgMATERIAL {
72
73 private:
74     // OpenGL texture name
75     GLuint texture_id;
76
77     // file name of texture
78     string texture_name;
79
80     // alpha texture?
81     int alpha;
82
83     // material properties
84     GLfloat ambient[4], diffuse[4], specular[4], emissive[4];
85     GLint texture_ptr;
86
87     // transient list of objects with this material type (used for sorting
88     // by material to reduce GL state changes when rendering the scene
89     frag_list_type list;
90     // size_t list_size;
91
92 public:
93
94     // Constructor
95     fgMATERIAL ( void );
96
97     int size() const { return list.size(); }
98     bool empty() const { return list.size() == 0; }
99
100     // Sorting routines
101     void init_sort_list( void ) {
102         list.erase( list.begin(), list.end() );
103     }
104
105     bool append_sort_list( fgFRAGMENT *object ) {
106         list.push_back( object );
107         return true;
108     }
109
110     void render_fragments();
111
112     void load_texture();
113
114     // Destructor
115     ~fgMATERIAL ( void );
116
117     friend istream& operator >> ( istream& in, fgMATERIAL& m );
118 };
119
120
121 // Material management class
122 class fgMATERIAL_MGR {
123
124 public:
125
126     // associative array of materials
127     typedef map < string, fgMATERIAL, less<string> > container;
128     typedef container::iterator iterator;
129     typedef container::const_iterator const_iterator;
130
131     iterator begin() { return material_map.begin(); }
132     const_iterator begin() const { return material_map.begin(); }
133
134     iterator end() { return material_map.end(); }
135     const_iterator end() const { return material_map.end(); }
136
137     // Constructor
138     fgMATERIAL_MGR ( void );
139
140     // Load a library of material properties
141     int load_lib ( void );
142
143     bool get_textures_loaded() { return textures_loaded; }
144
145     // Initialize the transient list of fragments for each material property
146     void init_transient_material_lists( void );
147
148     bool find( const string& material, fgMATERIAL*& mtl_ptr );
149
150     void render_fragments();
151
152     // Destructor
153     ~fgMATERIAL_MGR ( void );
154
155 private:
156
157     // Have textures been loaded
158     bool textures_loaded;
159
160     container material_map;
161 };
162
163
164 // global material management class
165 extern fgMATERIAL_MGR material_mgr;
166
167
168 #endif // _MATERIAL_HXX 
169
170
171 // $Log$
172 // Revision 1.6  1999/02/02 20:13:39  curt
173 // MSVC++ portability changes by Bernie Bright:
174 //
175 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
176 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
177 // Simulator/Cockpit/hud.cxx: Added Standard headers
178 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
179 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG.  Deleted <stdio.h>
180 // Simulator/Main/fg_init.cxx:
181 // Simulator/Main/GLUTmain.cxx:
182 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
183 // Simulator/Objects/material.hxx:
184 // Simulator/Time/timestamp.hxx: VC++ friend kludge
185 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
186 // Simulator/Main/views.hxx: Added a constant
187 //
188 // Revision 1.5  1998/10/12 23:49:18  curt
189 // Changes from NHV to make the code more dynamic with fewer hard coded limits.
190 //
191 // Revision 1.4  1998/09/17 18:35:53  curt
192 // Tweaks and optimizations by Norman Vine.
193 //
194 // Revision 1.3  1998/09/10 19:07:12  curt
195 // /Simulator/Objects/fragment.hxx
196 //   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
197 //
198 // ./Simulator/Objects/material.cxx
199 // ./Simulator/Objects/material.hxx
200 //   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
201 //   data members - that should keep the rabble happy :)
202 //
203 // ./Simulator/Scenery/tilemgr.cxx
204 //   In viewable() delay evaluation of eye[0] and eye[1] in until they're
205 //   actually needed.
206 //   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
207 //   method.
208 //
209 // ./Include/fg_stl_config.h
210 // ./Include/auto_ptr.hxx
211 //   Added support for g++ 2.7.
212 //   Further changes to other files are forthcoming.
213 //
214 // Brief summary of changes required for g++ 2.7.
215 //   operator->() not supported by iterators: use (*i).x instead of i->x
216 //   default template arguments not supported,
217 //   <functional> doesn't have mem_fun_ref() needed by callbacks.
218 //   some std include files have different names.
219 //   template member functions not supported.
220 //
221 // Revision 1.2  1998/09/01 19:03:09  curt
222 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
223 //  - The new classes in libmisc.tgz define a stream interface into zlib.
224 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
225 //    to something more appropriate.  However you'll have to change the
226 //    include directives in all the other files.  Additionally you'll have
227 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
228 //
229 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
230 //    test so I've included the required changes in config.tgz.
231 //
232 //    There are a fair few changes to Simulator/Objects as I've moved
233 //    things around.  Loading tiles is quicker but thats not where the delay
234 //    is.  Tile loading takes a few tenths of a second per file on a P200
235 //    but it seems to be the post-processing that leads to a noticeable
236 //    blip in framerate.  I suppose its time to start profiling to see where
237 //    the delays are.
238 //
239 //    I've included a brief description of each archives contents.
240 //
241 // Lib/Misc/
242 //   zfstream.cxx
243 //   zfstream.hxx
244 //     C++ stream interface into zlib.
245 //     Taken from zlib-1.1.3/contrib/iostream/.
246 //     Minor mods for STL compatibility.
247 //     There's no copyright associated with these so I assume they're
248 //     covered by zlib's.
249 //
250 //   fgstream.cxx
251 //   fgstream.hxx
252 //     FlightGear input stream using gz_ifstream.  Tries to open the
253 //     given filename.  If that fails then filename is examined and a
254 //     ".gz" suffix is removed or appended and that file is opened.
255 //
256 //   stopwatch.hxx
257 //     A simple timer for benchmarking.  Not used in production code.
258 //     Taken from the Blitz++ project.  Covered by GPL.
259 //
260 //   strutils.cxx
261 //   strutils.hxx
262 //     Some simple string manipulation routines.
263 //
264 // Simulator/Airports/
265 //   Load airports database using fgstream.
266 //   Changed fgAIRPORTS to use set<> instead of map<>.
267 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
268 //   Returns true if found.
269 //
270 // Simulator/Astro/
271 //   Modified fgStarsInit() to load stars database using fgstream.
272 //
273 // Simulator/Objects/
274 //   Modified fgObjLoad() to use fgstream.
275 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
276 //   Many changes to fgMATERIAL.
277 //   Some changes to fgFRAGMENT but I forget what!
278 //
279 // Revision 1.1  1998/08/25 16:51:24  curt
280 // Moved from ../Scenery
281 //
282 // Revision 1.10  1998/07/24 21:42:06  curt
283 // material.cxx: whups, double method declaration with no definition.
284 // obj.cxx: tweaks to avoid errors in SGI's CC.
285 // tile.cxx: optimizations by Norman Vine.
286 // tilemgr.cxx: optimizations by Norman Vine.
287 //
288 // Revision 1.9  1998/07/06 21:34:33  curt
289 // Added using namespace std for compilers that support this.
290 //
291 // Revision 1.8  1998/06/17 21:36:39  curt
292 // Load and manage multiple textures defined in the Materials library.
293 // Boost max material fagments for each material property to 800.
294 // Multiple texture support when rendering.
295 //
296 // Revision 1.7  1998/06/12 00:58:04  curt
297 // Build only static libraries.
298 // Declare memmove/memset for Sloaris.
299 //
300 // Revision 1.6  1998/06/06 01:09:31  curt
301 // I goofed on the log message in the last commit ... now fixed.
302 //
303 // Revision 1.5  1998/06/06 01:07:17  curt
304 // Increased per material fragment list size from 100 to 400.
305 // Now correctly draw viewable fragments in per material order.
306 //
307 // Revision 1.4  1998/06/05 22:39:53  curt
308 // Working on sorting by, and rendering by material properties.
309 //
310 // Revision 1.3  1998/06/03 00:47:50  curt
311 // No .h for STL includes.
312 // Minor view culling optimizations.
313 //
314 // Revision 1.2  1998/06/01 17:56:20  curt
315 // Incremental additions to material.cxx (not fully functional)
316 // Tweaked vfc_ratio math to avoid divide by zero.
317 //
318 // Revision 1.1  1998/05/30 01:56:45  curt
319 // Added material.cxx material.hxx
320 //