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