1 // material.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
22 // (Log is kept at end of this file)
39 #include "Include/fg_stl_config.h"
40 #include <Debug/fg_debug.h>
41 #include <Main/options.hxx>
42 #include <Misc/fgstream.hxx>
43 #include <Main/views.hxx>
44 #include <Scenery/tile.hxx>
46 #include "material.hxx"
47 #include "fragment.hxx"
51 // global material management class
52 fgMATERIAL_MGR material_mgr;
56 fgMATERIAL::fgMATERIAL ( void )
61 ambient[0] = ambient[1] = ambient[2] = ambient[3] = 0.0;
62 diffuse[0] = diffuse[1] = diffuse[2] = diffuse[3] = 0.0;
63 specular[0] = specular[1] = specular[2] = specular[3] = 0.0;
64 emissive[0] = emissive[1] = emissive[2] = emissive[3] = 0.0;
69 fgMATERIAL::append_sort_list( fgFRAGMENT *object )
71 if ( list_size < FG_MAX_MATERIAL_FRAGS ) {
72 list[ list_size++ ] = object;
80 operator >> ( istream& in, fgMATERIAL& m )
86 if ( token == "texture" )
88 in >> token >> m.texture_name;
90 else if ( token == "ambient" )
92 in >> token >> m.ambient[0] >> m.ambient[1]
93 >> m.ambient[2] >> m.ambient[3];
95 else if ( token == "diffuse" )
97 in >> token >> m.diffuse[0] >> m.diffuse[1]
98 >> m.diffuse[2] >> m.diffuse[3];
100 else if ( token == "specular" )
102 in >> token >> m.specular[0] >> m.specular[1]
103 >> m.specular[2] >> m.specular[3];
105 else if ( token == "emissive" )
107 in >> token >> m.emissive[0] >> m.emissive[1]
108 >> m.emissive[2] >> m.emissive[3];
110 else if ( token == "alpha" )
112 in >> token >> token;
113 if ( token == "yes" )
115 else if ( token == "no" )
119 fgPrintf( FG_TERRAIN, FG_INFO,
120 "Bad alpha value '%s'\n", token.c_str() );
123 else if ( token[0] == '}' )
133 fgMATERIAL::load_texture()
138 // create the texture object and bind it
139 #ifdef GL_VERSION_1_1
140 xglGenTextures(1, &texture_id );
141 xglBindTexture(GL_TEXTURE_2D, texture_id );
142 #elif GL_EXT_texture_object
143 xglGenTexturesEXT(1, &texture_id );
144 xglBindTextureEXT(GL_TEXTURE_2D, texture_id );
149 // set the texture parameters for this texture
150 xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
151 xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
152 xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
154 // xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
155 // GL_NEAREST_MIPMAP_NEAREST );
156 xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
158 /* GL_NEAREST_MIPMAP_LINEAR */
159 GL_LINEAR_MIPMAP_LINEAR ) ;
161 /* load in the texture data */
162 string tpath = current_options.get_fg_root() + "/Textures/" +
163 texture_name + ".rgb";
164 string fg_tpath = tpath + ".gz";
171 read_rgb_texture(tpath.c_str(), &width, &height)) ==
176 read_rgb_texture(fg_tpath.c_str(), &width, &height))
179 fgPrintf( FG_GENERAL, FG_EXIT,
180 "Error in loading texture %s\n",
186 /* xglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
187 GL_RGB, GL_UNSIGNED_BYTE, texbuf); */
189 gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, width, height,
190 GL_RGB, GL_UNSIGNED_BYTE, texbuf );
191 } else if ( alpha == 1 ) {
192 // load rgba (alpha) texture
196 read_alpha_texture(tpath.c_str(), &width, &height))
201 read_alpha_texture(fg_tpath.c_str(), &width, &height))
204 fgPrintf( FG_GENERAL, FG_EXIT,
205 "Error in loading texture %s\n",
211 xglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
212 GL_RGBA, GL_UNSIGNED_BYTE, texbuf);
218 fgMATERIAL::~fgMATERIAL ( void ) {
223 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
224 textures_loaded = false;
229 fgMATERIAL::render_fragments()
231 // cout << "rendering " + texture_name + " = " << list_size << "\n";
236 if ( current_options.get_textures() )
238 #ifdef GL_VERSION_1_1
239 xglBindTexture(GL_TEXTURE_2D, texture_id);
240 #elif GL_EXT_texture_object
241 xglBindTextureEXT(GL_TEXTURE_2D, texture_id);
246 xglMaterialfv (GL_FRONT, GL_AMBIENT, ambient);
247 xglMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
250 fgTILE* last_tile_ptr = NULL;
251 for ( size_t i = 0; i < list_size; ++i )
253 fgFRAGMENT* frag_ptr = list[i];
254 current_view.tris_rendered += frag_ptr->num_faces();
255 if ( frag_ptr->tile_ptr != last_tile_ptr )
257 // new tile, new translate
258 last_tile_ptr = frag_ptr->tile_ptr;
259 xglLoadMatrixd( frag_ptr->tile_ptr->model_view );
262 // Woohoo!!! We finally get to draw something!
263 // printf(" display_list = %d\n", frag_ptr->display_list);
264 xglCallList( frag_ptr->display_list );
269 // Load a library of material properties
271 fgMATERIAL_MGR::load_lib ( void )
273 string material_name;
275 // build the path name to the material db
276 string mpath = current_options.get_fg_root() + "/materials";
277 fg_gzifstream in( mpath );
279 fgPrintf( FG_GENERAL, FG_EXIT, "Cannot open file: %s\n",
282 while ( ! in.eof() ) {
283 // printf("%s", line);
285 // strip leading white space and comments
288 // set to zero to prevent its value accidently being '{'
289 // after a failed >> operation.
292 in.stream() >> material_name >> token;
294 if ( token == '{' ) {
295 printf( " Loading material %s\n", material_name.c_str() );
299 if ( current_options.get_textures() ) {
303 material_mgr.material_map[material_name] = m;
307 if ( current_options.get_textures() ) {
308 textures_loaded = true;
315 // Initialize the transient list of fragments for each material property
317 fgMATERIAL_MGR::init_transient_material_lists( void )
319 iterator last = end();
320 for ( iterator it = begin(); it != last; ++it )
322 (*it).second.init_sort_list();
328 fgMATERIAL_MGR::find( const string& material, fgMATERIAL*& mtl_ptr )
330 iterator it = material_map.find( material );
333 mtl_ptr = &((*it).second);
342 fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
347 fgMATERIAL_MGR::render_fragments()
349 current_view.tris_rendered = 0;
350 iterator last = end();
351 for ( iterator current = begin(); current != last; ++current )
352 (*current).second.render_fragments();
357 // Revision 1.7 1998/09/17 18:35:52 curt
358 // Tweaks and optimizations by Norman Vine.
360 // Revision 1.6 1998/09/15 01:35:05 curt
361 // cleaned up my fragment.num_faces hack :-) to use the STL (no need in
362 // duplicating work.)
363 // Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
364 // removed some unneeded stuff from fgTileMgrCurElev()
366 // Revision 1.5 1998/09/10 19:07:11 curt
367 // /Simulator/Objects/fragment.hxx
368 // Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
370 // ./Simulator/Objects/material.cxx
371 // ./Simulator/Objects/material.hxx
372 // Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
373 // data members - that should keep the rabble happy :)
375 // ./Simulator/Scenery/tilemgr.cxx
376 // In viewable() delay evaluation of eye[0] and eye[1] in until they're
378 // Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
381 // ./Include/fg_stl_config.h
382 // ./Include/auto_ptr.hxx
383 // Added support for g++ 2.7.
384 // Further changes to other files are forthcoming.
386 // Brief summary of changes required for g++ 2.7.
387 // operator->() not supported by iterators: use (*i).x instead of i->x
388 // default template arguments not supported,
389 // <functional> doesn't have mem_fun_ref() needed by callbacks.
390 // some std include files have different names.
391 // template member functions not supported.
393 // Revision 1.4 1998/09/01 19:03:08 curt
394 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
395 // - The new classes in libmisc.tgz define a stream interface into zlib.
396 // I've put these in a new directory, Lib/Misc. Feel free to rename it
397 // to something more appropriate. However you'll have to change the
398 // include directives in all the other files. Additionally you'll have
399 // add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
401 // The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
402 // test so I've included the required changes in config.tgz.
404 // There are a fair few changes to Simulator/Objects as I've moved
405 // things around. Loading tiles is quicker but thats not where the delay
406 // is. Tile loading takes a few tenths of a second per file on a P200
407 // but it seems to be the post-processing that leads to a noticeable
408 // blip in framerate. I suppose its time to start profiling to see where
411 // I've included a brief description of each archives contents.
416 // C++ stream interface into zlib.
417 // Taken from zlib-1.1.3/contrib/iostream/.
418 // Minor mods for STL compatibility.
419 // There's no copyright associated with these so I assume they're
420 // covered by zlib's.
424 // FlightGear input stream using gz_ifstream. Tries to open the
425 // given filename. If that fails then filename is examined and a
426 // ".gz" suffix is removed or appended and that file is opened.
429 // A simple timer for benchmarking. Not used in production code.
430 // Taken from the Blitz++ project. Covered by GPL.
434 // Some simple string manipulation routines.
436 // Simulator/Airports/
437 // Load airports database using fgstream.
438 // Changed fgAIRPORTS to use set<> instead of map<>.
439 // Added bool fgAIRPORTS::search() as a neater way doing the lookup.
440 // Returns true if found.
443 // Modified fgStarsInit() to load stars database using fgstream.
445 // Simulator/Objects/
446 // Modified fgObjLoad() to use fgstream.
447 // Modified fgMATERIAL_MGR::load_lib() to use fgstream.
448 // Many changes to fgMATERIAL.
449 // Some changes to fgFRAGMENT but I forget what!
451 // Revision 1.3 1998/08/27 17:02:09 curt
452 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
453 // - use strings for fg_root and airport_id and added methods to return
455 // - inlined all access methods,
456 // - made the parsing functions private methods,
457 // - deleted some unused functions.
458 // - propogated some of these changes out a bit further.
460 // Revision 1.2 1998/08/25 20:53:33 curt
461 // Shuffled $FG_ROOT file layout.
463 // Revision 1.1 1998/08/25 16:51:24 curt
464 // Moved from ../Scenery
466 // Revision 1.13 1998/08/24 20:11:39 curt
469 // Revision 1.12 1998/08/12 21:41:27 curt
470 // Need to negate the test for textures so that textures aren't loaded when
471 // they are disabled rather than visa versa ... :-)
473 // Revision 1.11 1998/08/12 21:13:03 curt
474 // material.cxx: don't load textures if they are disabled
475 // obj.cxx: optimizations from Norman Vine
476 // tile.cxx: minor tweaks
477 // tile.hxx: addition of num_faces
478 // tilemgr.cxx: minor tweaks
480 // Revision 1.10 1998/07/24 21:42:06 curt
481 // material.cxx: whups, double method declaration with no definition.
482 // obj.cxx: tweaks to avoid errors in SGI's CC.
483 // tile.cxx: optimizations by Norman Vine.
484 // tilemgr.cxx: optimizations by Norman Vine.
486 // Revision 1.9 1998/07/13 21:01:57 curt
487 // Wrote access functions for current fgOPTIONS.
489 // Revision 1.8 1998/07/08 14:47:20 curt
490 // Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
491 // polare3d.h renamed to polar3d.hxx
492 // fg{Cartesian,Polar}Point3d consolodated.
493 // Added some initial support for calculating local current ground elevation.
495 // Revision 1.7 1998/07/04 00:54:28 curt
496 // Added automatic mipmap generation.
498 // When rendering fragments, use saved model view matrix from associated tile
499 // rather than recalculating it with push() translate() pop().
501 // Revision 1.6 1998/06/27 16:54:59 curt
502 // Check for GL_VERSION_1_1 or GL_EXT_texture_object to decide whether to use
503 // "EXT" versions of texture management routines.
505 // Revision 1.5 1998/06/17 21:36:39 curt
506 // Load and manage multiple textures defined in the Materials library.
507 // Boost max material fagments for each material property to 800.
508 // Multiple texture support when rendering.
510 // Revision 1.4 1998/06/12 00:58:04 curt
511 // Build only static libraries.
512 // Declare memmove/memset for Sloaris.
514 // Revision 1.3 1998/06/05 22:39:53 curt
515 // Working on sorting by, and rendering by material properties.
517 // Revision 1.2 1998/06/01 17:56:20 curt
518 // Incremental additions to material.cxx (not fully functional)
519 // Tweaked vfc_ratio math to avoid divide by zero.
521 // Revision 1.1 1998/05/30 01:56:45 curt
522 // Added material.cxx material.hxx