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