]> git.mxchange.org Git - flightgear.git/blob - Simulator/Objects/material.cxx
Merge Include as subdirectory
[flightgear.git] / Simulator / Objects / material.cxx
1 // material.cxx -- 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 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include <Include/compiler.h>
37
38 #include <string.h>
39 #include STL_STRING
40
41 #include <Debug/logstream.hxx>
42 #include <Misc/fgstream.hxx>
43 #include <Main/options.hxx>
44 #include <Main/views.hxx>
45 #include <Scenery/tile.hxx>
46
47 #include "material.hxx"
48 #include "fragment.hxx"
49 #include "texload.h"
50
51 FG_USING_STD(string);
52
53
54 // global material management class
55 fgMATERIAL_MGR material_mgr;
56
57
58 // Constructor
59 fgMATERIAL::fgMATERIAL ( void )
60     : texture_name(""),
61       alpha(0)
62       // , list_size(0)
63 {
64     ambient[0]  = ambient[1]  = ambient[2]  = ambient[3]  = 0.0;
65     diffuse[0]  = diffuse[1]  = diffuse[2]  = diffuse[3]  = 0.0;
66     specular[0] = specular[1] = specular[2] = specular[3] = 0.0;
67     emissive[0] = emissive[1] = emissive[2] = emissive[3] = 0.0;
68 }
69
70 /*
71 int
72 fgMATERIAL::append_sort_list( fgFRAGMENT *object )
73 {
74     if ( list_size < FG_MAX_MATERIAL_FRAGS ) {
75         list[ list_size++ ] = object;
76         return 1;
77     } else {
78         return 0;
79     }
80 }
81 */
82
83 istream&
84 operator >> ( istream& in, fgMATERIAL& m )
85 {
86     string token;
87
88     for (;;) {
89         in >> token;
90         if ( token == "texture" )
91         {
92             in >> token >> m.texture_name;
93         }
94         else if ( token == "ambient" )
95         {
96             in >> token >> m.ambient[0] >> m.ambient[1]
97                         >> m.ambient[2] >> m.ambient[3];
98         }
99         else if ( token == "diffuse" )
100         {
101             in >> token >> m.diffuse[0] >> m.diffuse[1]
102                         >> m.diffuse[2] >> m.diffuse[3];
103         }
104         else if ( token == "specular" )
105         {
106             in >> token >> m.specular[0] >> m.specular[1]
107                         >> m.specular[2] >> m.specular[3];
108         }
109         else if ( token == "emissive" )
110         {
111             in >> token >> m.emissive[0] >> m.emissive[1]
112                         >> m.emissive[2] >> m.emissive[3];
113         }
114         else if ( token == "alpha" )
115         {
116             in >> token >> token;
117             if ( token == "yes" )
118                 m.alpha = 1;
119             else if ( token == "no" )
120                 m.alpha = 0;
121             else
122             {
123                 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
124             }
125         }
126         else if ( token[0] == '}' )
127         {
128             break;
129         }
130     }
131
132     return in;
133 }
134
135 void
136 fgMATERIAL::load_texture()
137 {
138         GLubyte *texbuf;
139         int width, height;
140
141         // create the texture object and bind it
142 #ifdef GL_VERSION_1_1
143         xglGenTextures(1, &texture_id );
144         xglBindTexture(GL_TEXTURE_2D, texture_id );
145 #elif GL_EXT_texture_object
146         xglGenTexturesEXT(1, &texture_id );
147         xglBindTextureEXT(GL_TEXTURE_2D, texture_id );
148 #else
149 #  error port me
150 #endif
151
152         // set the texture parameters for this texture
153         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
154         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
155         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
156                           GL_LINEAR );
157         // xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
158         //                   GL_NEAREST_MIPMAP_NEAREST );
159         xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
160                           /* GL_LINEAR */ 
161                           /* GL_NEAREST_MIPMAP_LINEAR */
162                           GL_LINEAR_MIPMAP_LINEAR ) ;
163
164         /* load in the texture data */
165         string tpath = current_options.get_fg_root() + "/Textures/" + 
166             texture_name + ".rgb";
167         string fg_tpath = tpath + ".gz";
168
169         if ( alpha == 0 ) {
170             // load rgb texture
171
172             // Try uncompressed
173             if ( (texbuf = 
174                   read_rgb_texture(tpath.c_str(), &width, &height)) == 
175                  NULL )
176             {
177                 // Try compressed
178                 if ( (texbuf = 
179                       read_rgb_texture(fg_tpath.c_str(), &width, &height)) 
180                      == NULL )
181                 {
182                     FG_LOG( FG_GENERAL, FG_ALERT, 
183                             "Error in loading texture " << tpath );
184                     exit(-1);
185                 } 
186             } 
187
188             /* xglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
189                GL_RGB, GL_UNSIGNED_BYTE, texbuf); */
190
191             gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, width, height, 
192                                GL_RGB, GL_UNSIGNED_BYTE, texbuf );
193         } else if ( alpha == 1 ) {
194             // load rgba (alpha) texture
195
196             // Try uncompressed
197             if ( (texbuf = 
198                   read_alpha_texture(tpath.c_str(), &width, &height))
199                  == NULL )
200             {
201                 // Try compressed
202                 if ((texbuf = 
203                      read_alpha_texture(fg_tpath.c_str(), &width, &height))
204                     == NULL )
205                 {
206                     FG_LOG( FG_GENERAL, FG_ALERT, 
207                             "Error in loading texture " << tpath );
208                     exit(-1);
209                 } 
210             } 
211
212             xglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
213                           GL_RGBA, GL_UNSIGNED_BYTE, texbuf);
214         }
215 }
216
217
218 // Destructor
219 fgMATERIAL::~fgMATERIAL ( void ) {
220 }
221
222
223 // Constructor
224 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
225     textures_loaded = false;
226 }
227
228
229 void
230 fgMATERIAL::render_fragments()
231 {
232     int tris_rendered = current_view.get_tris_rendered();
233
234     // cout << "rendering " + texture_name + " = " << list_size << "\n";
235
236     if ( empty() )
237         return;
238
239     if ( current_options.get_textures() )
240     {
241 #ifdef GL_VERSION_1_1
242         xglBindTexture(GL_TEXTURE_2D, texture_id);
243 #elif GL_EXT_texture_object
244         xglBindTextureEXT(GL_TEXTURE_2D, texture_id);
245 #else
246 #  error port me
247 #endif
248     } else {
249         xglMaterialfv (GL_FRONT, GL_AMBIENT, ambient);
250         xglMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
251     }
252
253     fgTILE* last_tile_ptr = NULL;
254     frag_list_iterator current = list.begin();
255     frag_list_iterator last = list.end();
256
257     for ( ; current != last; ++current ) {
258         fgFRAGMENT* frag_ptr = *current;
259         tris_rendered += frag_ptr->num_faces();
260         if ( frag_ptr->tile_ptr != last_tile_ptr )
261         {
262             // new tile, new translate
263             last_tile_ptr = frag_ptr->tile_ptr;
264             xglLoadMatrixd( frag_ptr->tile_ptr->model_view );
265         }
266
267         // Woohoo!!!  We finally get to draw something!
268         // printf("  display_list = %d\n", frag_ptr->display_list);
269         xglCallList( frag_ptr->display_list );
270     }
271
272     current_view.set_tris_rendered( tris_rendered );
273 }
274
275
276 // Load a library of material properties
277 int
278 fgMATERIAL_MGR::load_lib ( void )
279 {
280     string material_name;
281
282     // build the path name to the material db
283     string mpath = current_options.get_fg_root() + "/materials";
284     fg_gzifstream in( mpath );
285     if ( ! in ) {
286         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath );
287         exit(-1);
288     }
289
290     while ( ! in.eof() ) {
291         // printf("%s", line);
292
293         // strip leading white space and comments
294         in >> skipcomment;
295
296         // set to zero to prevent its value accidently being '{'
297         // after a failed >> operation.
298         char token = 0;
299
300         in >> material_name >> token;
301
302         if ( token == '{' ) {
303             FG_LOG( FG_TERRAIN, FG_INFO,
304                     "  Loading material " << material_name );
305             fgMATERIAL m;
306             in >> m;
307
308             if ( current_options.get_textures() ) {
309                 m.load_texture();
310             }
311
312             material_mgr.material_map[material_name] = m;
313         }
314     }
315
316     if ( current_options.get_textures() ) {
317         textures_loaded = true;
318     }
319
320     return(1);
321 }
322
323
324 // Initialize the transient list of fragments for each material property
325 void
326 fgMATERIAL_MGR::init_transient_material_lists( void )
327 {
328     iterator last = end();
329     for ( iterator it = begin(); it != last; ++it )
330     {
331         (*it).second.init_sort_list();
332     }
333 }
334
335
336 bool
337 fgMATERIAL_MGR::find( const string& material, fgMATERIAL*& mtl_ptr )
338 {
339     iterator it = material_map.find( material );
340     if ( it != end() )
341     {
342         mtl_ptr = &((*it).second);
343         return true;
344     }
345
346     return false;
347 }
348
349
350 // Destructor
351 fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
352 }
353
354
355 void
356 fgMATERIAL_MGR::render_fragments()
357 {
358     current_view.set_tris_rendered( 0 );
359
360     iterator last = end();
361     for ( iterator current = begin(); current != last; ++current )
362         (*current).second.render_fragments();
363 }
364
365
366 // $Log$
367 // Revision 1.14  1999/03/02 01:03:24  curt
368 // Tweaks for building with native SGI compilers.
369 //
370 // Revision 1.13  1999/02/26 22:09:58  curt
371 // Added initial support for native SGI compilers.
372 //
373 // Revision 1.12  1998/12/09 18:50:30  curt
374 // Converted "class fgVIEW" to "class FGView" and updated to make data
375 // members private and make required accessor functions.
376 //
377 // Revision 1.11  1998/11/07 19:07:12  curt
378 // Enable release builds using the --without-logging option to the configure
379 // script.  Also a couple log message cleanups, plus some C to C++ comment
380 // conversion.
381 //
382 // Revision 1.10  1998/11/06 21:18:17  curt
383 // Converted to new logstream debugging facility.  This allows release
384 // builds with no messages at all (and no performance impact) by using
385 // the -DFG_NDEBUG flag.
386 //
387 // Revision 1.9  1998/11/06 14:47:05  curt
388 // Changes to track Bernie's updates to fgstream.
389 //
390 // Revision 1.8  1998/10/12 23:49:17  curt
391 // Changes from NHV to make the code more dynamic with fewer hard coded limits.
392 //
393 // Revision 1.7  1998/09/17 18:35:52  curt
394 // Tweaks and optimizations by Norman Vine.
395 //
396 // Revision 1.6  1998/09/15 01:35:05  curt
397 // cleaned up my fragment.num_faces hack :-) to use the STL (no need in
398 // duplicating work.)
399 // Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
400 // removed some unneeded stuff from fgTileMgrCurElev()
401 //
402 // Revision 1.5  1998/09/10 19:07:11  curt
403 // /Simulator/Objects/fragment.hxx
404 //   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
405 //
406 // ./Simulator/Objects/material.cxx
407 // ./Simulator/Objects/material.hxx
408 //   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
409 //   data members - that should keep the rabble happy :)
410 //
411 // ./Simulator/Scenery/tilemgr.cxx
412 //   In viewable() delay evaluation of eye[0] and eye[1] in until they're
413 //   actually needed.
414 //   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
415 //   method.
416 //
417 // ./Include/fg_stl_config.h
418 // ./Include/auto_ptr.hxx
419 //   Added support for g++ 2.7.
420 //   Further changes to other files are forthcoming.
421 //
422 // Brief summary of changes required for g++ 2.7.
423 //   operator->() not supported by iterators: use (*i).x instead of i->x
424 //   default template arguments not supported,
425 //   <functional> doesn't have mem_fun_ref() needed by callbacks.
426 //   some std include files have different names.
427 //   template member functions not supported.
428 //
429 // Revision 1.4  1998/09/01 19:03:08  curt
430 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
431 //  - The new classes in libmisc.tgz define a stream interface into zlib.
432 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
433 //    to something more appropriate.  However you'll have to change the
434 //    include directives in all the other files.  Additionally you'll have
435 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
436 //
437 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
438 //    test so I've included the required changes in config.tgz.
439 //
440 //    There are a fair few changes to Simulator/Objects as I've moved
441 //    things around.  Loading tiles is quicker but thats not where the delay
442 //    is.  Tile loading takes a few tenths of a second per file on a P200
443 //    but it seems to be the post-processing that leads to a noticeable
444 //    blip in framerate.  I suppose its time to start profiling to see where
445 //    the delays are.
446 //
447 //    I've included a brief description of each archives contents.
448 //
449 // Lib/Misc/
450 //   zfstream.cxx
451 //   zfstream.hxx
452 //     C++ stream interface into zlib.
453 //     Taken from zlib-1.1.3/contrib/iostream/.
454 //     Minor mods for STL compatibility.
455 //     There's no copyright associated with these so I assume they're
456 //     covered by zlib's.
457 //
458 //   fgstream.cxx
459 //   fgstream.hxx
460 //     FlightGear input stream using gz_ifstream.  Tries to open the
461 //     given filename.  If that fails then filename is examined and a
462 //     ".gz" suffix is removed or appended and that file is opened.
463 //
464 //   stopwatch.hxx
465 //     A simple timer for benchmarking.  Not used in production code.
466 //     Taken from the Blitz++ project.  Covered by GPL.
467 //
468 //   strutils.cxx
469 //   strutils.hxx
470 //     Some simple string manipulation routines.
471 //
472 // Simulator/Airports/
473 //   Load airports database using fgstream.
474 //   Changed fgAIRPORTS to use set<> instead of map<>.
475 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
476 //   Returns true if found.
477 //
478 // Simulator/Astro/
479 //   Modified fgStarsInit() to load stars database using fgstream.
480 //
481 // Simulator/Objects/
482 //   Modified fgObjLoad() to use fgstream.
483 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
484 //   Many changes to fgMATERIAL.
485 //   Some changes to fgFRAGMENT but I forget what!
486 //
487 // Revision 1.3  1998/08/27 17:02:09  curt
488 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
489 // - use strings for fg_root and airport_id and added methods to return
490 //   them as strings,
491 // - inlined all access methods,
492 // - made the parsing functions private methods,
493 // - deleted some unused functions.
494 // - propogated some of these changes out a bit further.
495 //
496 // Revision 1.2  1998/08/25 20:53:33  curt
497 // Shuffled $FG_ROOT file layout.
498 //
499 // Revision 1.1  1998/08/25 16:51:24  curt
500 // Moved from ../Scenery
501 //
502 // Revision 1.13  1998/08/24 20:11:39  curt
503 // Tweaks ...
504 //
505 // Revision 1.12  1998/08/12 21:41:27  curt
506 // Need to negate the test for textures so that textures aren't loaded when
507 // they are disabled rather than visa versa ... :-)
508 //
509 // Revision 1.11  1998/08/12 21:13:03  curt
510 // material.cxx: don't load textures if they are disabled
511 // obj.cxx: optimizations from Norman Vine
512 // tile.cxx: minor tweaks
513 // tile.hxx: addition of num_faces
514 // tilemgr.cxx: minor tweaks
515 //
516 // Revision 1.10  1998/07/24 21:42:06  curt
517 // material.cxx: whups, double method declaration with no definition.
518 // obj.cxx: tweaks to avoid errors in SGI's CC.
519 // tile.cxx: optimizations by Norman Vine.
520 // tilemgr.cxx: optimizations by Norman Vine.
521 //
522 // Revision 1.9  1998/07/13 21:01:57  curt
523 // Wrote access functions for current fgOPTIONS.
524 //
525 // Revision 1.8  1998/07/08 14:47:20  curt
526 // Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
527 // polare3d.h renamed to polar3d.hxx
528 // fg{Cartesian,Polar}Point3d consolodated.
529 // Added some initial support for calculating local current ground elevation.
530 //
531 // Revision 1.7  1998/07/04 00:54:28  curt
532 // Added automatic mipmap generation.
533 //
534 // When rendering fragments, use saved model view matrix from associated tile
535 // rather than recalculating it with push() translate() pop().
536 //
537 // Revision 1.6  1998/06/27 16:54:59  curt
538 // Check for GL_VERSION_1_1 or GL_EXT_texture_object to decide whether to use
539 //   "EXT" versions of texture management routines.
540 //
541 // Revision 1.5  1998/06/17 21:36:39  curt
542 // Load and manage multiple textures defined in the Materials library.
543 // Boost max material fagments for each material property to 800.
544 // Multiple texture support when rendering.
545 //
546 // Revision 1.4  1998/06/12 00:58:04  curt
547 // Build only static libraries.
548 // Declare memmove/memset for Sloaris.
549 //
550 // Revision 1.3  1998/06/05 22:39:53  curt
551 // Working on sorting by, and rendering by material properties.
552 //
553 // Revision 1.2  1998/06/01 17:56:20  curt
554 // Incremental additions to material.cxx (not fully functional)
555 // Tweaked vfc_ratio math to avoid divide by zero.
556 //
557 // Revision 1.1  1998/05/30 01:56:45  curt
558 // Added material.cxx material.hxx
559 //