]> git.mxchange.org Git - flightgear.git/blob - Objects/obj.cxx
Converted to new logstream debugging facility. This allows release
[flightgear.git] / Objects / obj.cxx
1 // obj.cxx -- routines to handle "sorta" WaveFront .obj format files.
2 //
3 // Written by Curtis Olson, started October 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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 <stdio.h>
34 #include <string.h>
35 #include <GL/glut.h>
36 #include <XGL/xgl.h>
37
38 #if defined ( __sun__ )
39 extern "C" void *memmove(void *, const void *, size_t);
40 extern "C" void *memset(void *, int, size_t);
41 #endif
42
43 #include <string>       // Standard C++ library
44 #include <map>          // STL
45 #include <ctype.h>      // isdigit()
46
47 #ifdef NEEDNAMESPACESTD
48 using namespace std;
49 #endif
50
51 #include <Debug/logstream.hxx>
52 #include <Include/fg_constants.h>
53 #include <Include/fg_zlib.h>
54 #include <Main/options.hxx>
55 #include <Math/mat3.h>
56 #include <Math/fg_random.h>
57 #include <Math/point3d.hxx>
58 #include <Math/polar3d.hxx>
59 #include <Misc/stopwatch.hxx>
60 #include <Misc/fgstream.hxx>
61 #include <Scenery/tile.hxx>
62
63 #include "material.hxx"
64 #include "obj.hxx"
65
66
67 static double normals[MAX_NODES][3];
68
69
70 // given three points defining a triangle, calculate the normal
71 static void calc_normal(double p1[3], double p2[3], 
72                         double p3[3], double normal[3])
73 {
74     double v1[3], v2[3];
75     double temp;
76
77     v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
78     v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
79
80     MAT3cross_product(normal, v1, v2);
81     MAT3_NORMALIZE_VEC(normal,temp);
82
83     // fgPrintf( FG_TERRAIN, FG_DEBUG, "  Normal = %.2f %.2f %.2f\n", 
84     //           normal[0], normal[1], normal[2]);
85 }
86
87
88 #define FG_TEX_CONSTANT 69.0
89
90
91 // Calculate texture coordinates for a given point.
92 static Point3D calc_tex_coords(double *node, const Point3D& ref) {
93     Point3D cp;
94     Point3D pp;
95     // double tmplon, tmplat;
96
97     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
98     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
99
100     cp = Point3D( node[0] + ref.x(),
101                   node[1] + ref.y(),
102                   node[2] + ref.z() );
103
104     pp = fgCartToPolar3d(cp);
105
106     // tmplon = pp.lon() * RAD_TO_DEG;
107     // tmplat = pp.lat() * RAD_TO_DEG;
108     // cout << tmplon << " " << tmplat << endl;
109
110     pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
111     pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
112
113     if ( pp.x() < 0.0 ) {
114         pp.setx( pp.x() + 11.0 );
115     }
116
117     if ( pp.y() < 0.0 ) {
118         pp.sety( pp.y() + 11.0 );
119     }
120
121     // cout << pp << endl;
122
123     return(pp);
124 }
125
126
127 // Load a .obj file and build the GL fragment list
128 int fgObjLoad( const string& path, fgTILE *t) {
129     fgFRAGMENT fragment;
130     Point3D pp;
131     double approx_normal[3], normal[3], scale;
132     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
133     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
134     GLint display_list;
135     int shading;
136     int in_fragment, in_faces, vncount, n1, n2, n3, n4;
137     int last1, last2, odd;
138     double (*nodes)[3];
139     Point3D center;
140
141     // printf("loading %s\n", path.c_str() );
142
143     // Attempt to open "path.gz" or "path"
144     fg_gzifstream in( path );
145     if ( ! in )
146     {
147         // Attempt to open "path.obj" or "path.obj.gz"
148         in.open( path + ".obj" );
149         if ( ! in )
150         {
151             FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
152             return 0;
153         }
154     }
155
156     shading = current_options.get_shading();
157
158     in_fragment = 0;
159     t->ncount = 1;
160     vncount = 1;
161     t->bounding_radius = 0.0;
162     nodes = t->nodes;
163     center = t->center;
164
165     StopWatch stopwatch;
166     stopwatch.start();
167
168     // ignore initial comments and blank lines. (priming the pump)
169     in >> skipcomment;
170
171     while ( ! in.eof() )
172     {
173
174         string token;
175         in >> token;
176
177         // printf("token = %s\n", token.c_str() );
178
179         if ( token == "gbs" )
180         {
181             // reference point (center offset)
182             in >> t->center >> t->bounding_radius;
183             center = t->center;
184         }
185         else if ( token == "bs" )
186         {
187             // reference point (center offset)
188             in >> fragment.center;
189             in >> fragment.bounding_radius;
190         }
191         else if ( token == "vn" )
192         {
193             // vertex normal
194             if ( vncount < MAX_NODES ) {
195                 in >> normals[vncount][0]
196                    >> normals[vncount][1]
197                    >> normals[vncount][2];
198                 vncount++;
199             } else {
200                 FG_LOG( FG_TERRAIN, FG_ALERT, 
201                         "Read too many vertex normals ... dying :-(" );
202                 exit(-1);
203             }
204         }
205         else if ( token[0] == 'v' )
206         {
207             // node (vertex)
208             if ( t->ncount < MAX_NODES ) {
209                 in >> t->nodes[t->ncount][0]
210                    >> t->nodes[t->ncount][1]
211                    >> t->nodes[t->ncount][2];
212                 t->ncount++;
213             } else {
214                 FG_LOG( FG_TERRAIN, FG_ALERT, 
215                         "Read too many nodes ... dying :-(");
216                 exit(-1);
217             }
218         }
219         else if ( token == "usemtl" )
220         {
221             // material property specification
222
223             // this also signals the start of a new fragment
224             if ( in_fragment ) {
225                 // close out the previous structure and start the next
226                 xglEnd();
227                 xglEndList();
228                 // printf("xglEnd(); xglEndList();\n");
229
230                 // update fragment
231                 fragment.display_list = display_list;
232
233                 // push this fragment onto the tile's object list
234                 t->fragment_list.push_back(fragment);
235             } else {
236                 in_fragment = 1;
237             }
238
239             // printf("start of fragment (usemtl)\n");
240
241             display_list = xglGenLists(1);
242             xglNewList(display_list, GL_COMPILE);
243             // printf("xglGenLists(); xglNewList();\n");
244             in_faces = 0;
245
246             // reset the existing face list
247             // printf("cleaning a fragment with %d faces\n", 
248             //        fragment.faces.size());
249             fragment.init();
250
251             // scan the material line
252             string material;
253             in >> material;
254             fragment.tile_ptr = t;
255
256             // find this material in the properties list
257             if ( ! material_mgr.find( material, fragment.material_ptr )) {
258                 FG_LOG( FG_TERRAIN, FG_ALERT, 
259                         "Ack! unknown usemtl name = " << material 
260                         << " in " << path );
261             }
262
263             // initialize the fragment transformation matrix
264             /*
265             for ( i = 0; i < 16; i++ ) {
266                 fragment.matrix[i] = 0.0;
267             }
268             fragment.matrix[0] = fragment.matrix[5] =
269                 fragment.matrix[10] = fragment.matrix[15] = 1.0;
270             */
271         } else if ( token[0] == 't' ) {
272             // start a new triangle strip
273
274             n1 = n2 = n3 = n4 = 0;
275
276             // fgPrintf( FG_TERRAIN, FG_DEBUG, "    new tri strip = %s", line);
277             in >> n1 >> n2 >> n3;
278             fragment.add_face(n1, n2, n3);
279
280             // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
281
282             xglBegin(GL_TRIANGLE_STRIP);
283             // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
284
285             odd = 1; 
286             scale = 1.0;
287
288             if ( shading ) {
289                 // Shading model is "GL_SMOOTH" so use precalculated
290                 // (averaged) normals
291                 MAT3_SCALE_VEC(normal, normals[n1], scale);
292                 xglNormal3dv(normal);
293                 pp = calc_tex_coords(nodes[n1], center);
294                 xglTexCoord2f(pp.lon(), pp.lat());
295                 // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
296                 xglVertex3dv(nodes[n1]);                
297
298                 MAT3_SCALE_VEC(normal, normals[n2], scale);
299                 xglNormal3dv(normal);
300                 pp = calc_tex_coords(nodes[n2], center);
301                 xglTexCoord2f(pp.lon(), pp.lat());
302                 //xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
303                 xglVertex3dv(nodes[n2]);                                
304
305                 MAT3_SCALE_VEC(normal, normals[n3], scale);
306                 xglNormal3dv(normal);
307                 pp = calc_tex_coords(nodes[n3], center);
308                 xglTexCoord2f(pp.lon(), pp.lat());
309                 // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
310                 xglVertex3dv(nodes[n3]);
311             } else {
312                 // Shading model is "GL_FLAT" so calculate per face
313                 // normals on the fly.
314                 if ( odd ) {
315                     calc_normal(nodes[n1], nodes[n2], 
316                                 nodes[n3], approx_normal);
317                 } else {
318                     calc_normal(nodes[n2], nodes[n1], 
319                                 nodes[n3], approx_normal);
320                 }
321                 MAT3_SCALE_VEC(normal, approx_normal, scale);
322                 xglNormal3dv(normal);
323
324                 pp = calc_tex_coords(nodes[n1], center);
325                 xglTexCoord2f(pp.lon(), pp.lat());
326                 // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
327                 xglVertex3dv(nodes[n1]);                
328
329                 pp = calc_tex_coords(nodes[n2], center);
330                 xglTexCoord2f(pp.lon(), pp.lat());
331                 // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
332                 xglVertex3dv(nodes[n2]);                
333
334                 pp = calc_tex_coords(nodes[n3], center);
335                 xglTexCoord2f(pp.lon(), pp.lat());
336                 // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
337                 xglVertex3dv(nodes[n3]);                
338             }
339             // printf("some normals, texcoords, and vertices\n");
340
341             odd = 1 - odd;
342             last1 = n2;
343             last2 = n3;
344
345             // There can be three or four values 
346             char c;
347             while ( in.get(c) )
348             {
349                 if ( c == '\n' )
350                     break; // only the one
351
352                 if ( isdigit(c) )
353                 {
354                     in.putback(c);
355                     in >> n4;
356                     break;
357                 }
358             }
359
360             if ( n4 > 0 ) {
361                 fragment.add_face(n3, n2, n4);
362
363                 if ( shading ) {
364                     // Shading model is "GL_SMOOTH"
365                     MAT3_SCALE_VEC(normal, normals[n4], scale);
366                 } else {
367                     // Shading model is "GL_FLAT"
368                     calc_normal(nodes[n3], nodes[n2], nodes[n4], 
369                                 approx_normal);
370                     MAT3_SCALE_VEC(normal, approx_normal, scale);
371                 }
372                 xglNormal3dv(normal);
373                 pp = calc_tex_coords(nodes[n4], center);
374                 xglTexCoord2f(pp.lon(), pp.lat());
375                 // xglVertex3d(t->nodes[n4][0],t->nodes[n4][1],t->nodes[n4][2]);
376                 xglVertex3dv(nodes[n4]);                
377
378                 odd = 1 - odd;
379                 last1 = n3;
380                 last2 = n4;
381                 // printf("a normal, texcoord, and vertex (4th)\n");
382             }
383         } else if ( token[0] == 'f' ) {
384             // unoptimized face
385
386             if ( !in_faces ) {
387                 xglBegin(GL_TRIANGLES);
388                 // printf("xglBegin(triangles)\n");
389                 in_faces = 1;
390             }
391
392             // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
393             in >> n1 >> n2 >> n3;
394             fragment.add_face(n1, n2, n3);
395
396             // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
397             xglNormal3dv(normals[n1]);
398             pp = calc_tex_coords(nodes[n1], center);
399             xglTexCoord2f(pp.lon(), pp.lat());
400             // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
401             xglVertex3dv(nodes[n1]);
402
403             // xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
404             xglNormal3dv(normals[n2]);
405             pp = calc_tex_coords(nodes[n2], center);
406             xglTexCoord2f(pp.lon(), pp.lat());
407             // xglVertex3d(t->nodes[n2][0], t->nodes[n2][1], t->nodes[n2][2]);
408             xglVertex3dv(nodes[n2]);
409                 
410             // xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
411             xglNormal3dv(normals[n3]);
412             pp = calc_tex_coords(nodes[n3], center);
413             xglTexCoord2f(pp.lon(), pp.lat());
414             // xglVertex3d(t->nodes[n3][0], t->nodes[n3][1], t->nodes[n3][2]);
415             xglVertex3dv(nodes[n3]);
416             // printf("some normals, texcoords, and vertices (tris)\n");
417         } else if ( token[0] == 'q' ) {
418             // continue a triangle strip
419             n1 = n2 = 0;
420
421             // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
422             //           line);
423             in >> n1;
424
425             // There can be one or two values 
426             char c;
427             while ( in.get(c) )
428             {
429                 if ( c == '\n' )
430                     break; // only the one
431
432                 if ( isdigit(c) )
433                 {
434                     in.putback(c);
435                     in >> n2;
436                     break;
437                 }
438             }
439             // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
440
441             if ( odd ) {
442                 fragment.add_face(last1, last2, n1);
443             } else {
444                 fragment.add_face(last2, last1, n1);
445             }
446
447             if ( shading ) {
448                 // Shading model is "GL_SMOOTH"
449                 MAT3_SCALE_VEC(normal, normals[n1], scale);
450             } else {
451                 // Shading model is "GL_FLAT"
452                 if ( odd ) {
453                     calc_normal(nodes[last1], nodes[last2], nodes[n1], 
454                                 approx_normal);
455                 } else {
456                     calc_normal(nodes[last2], nodes[last1], nodes[n1], 
457                                 approx_normal);
458                 }
459                 MAT3_SCALE_VEC(normal, approx_normal, scale);
460             }
461             xglNormal3dv(normal);
462
463             pp = calc_tex_coords(nodes[n1], center);
464             xglTexCoord2f(pp.lon(), pp.lat());
465             // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
466             xglVertex3dv(nodes[n1]);
467             // printf("a normal, texcoord, and vertex (4th)\n");
468    
469             odd = 1 - odd;
470             last1 = last2;
471             last2 = n1;
472
473             if ( n2 > 0 ) {
474                 // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
475
476                 if ( odd ) {
477                     fragment.add_face(last1, last2, n2);
478                 } else {
479                     fragment.add_face(last2, last1, n2);
480                 }
481
482                 if ( shading ) {
483                     // Shading model is "GL_SMOOTH"
484                     MAT3_SCALE_VEC(normal, normals[n2], scale);
485                 } else {
486                     // Shading model is "GL_FLAT"
487                     if ( odd ) {
488                         calc_normal(nodes[last1], nodes[last2], 
489                                     nodes[n2], approx_normal);
490                     } else {
491                         calc_normal(nodes[last2], nodes[last1], 
492                                     nodes[n2], approx_normal);
493                     }
494                     MAT3_SCALE_VEC(normal, approx_normal, scale);
495                 }
496                 xglNormal3dv(normal);
497                 
498                 pp = calc_tex_coords(nodes[n2], center);
499                 xglTexCoord2f(pp.lon(), pp.lat());
500                 // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
501                 xglVertex3dv(nodes[n2]);                
502                 // printf("a normal, texcoord, and vertex (4th)\n");
503
504                 odd = 1 -odd;
505                 last1 = last2;
506                 last2 = n2;
507             }
508         } else {
509             FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
510                     << path << " = " << token );
511         }
512
513         // eat comments and blank lines before start of while loop so
514         // if we are done with useful input it is noticed before hand.
515         in >> skipcomment;
516     }
517
518     if ( in_fragment ) {
519         // close out the previous structure and start the next
520         xglEnd();
521         xglEndList();
522         // printf("xglEnd(); xglEndList();\n");
523
524         // update fragment
525         fragment.display_list = display_list;
526         
527         // push this fragment onto the tile's object list
528         t->fragment_list.push_back(fragment);
529     }
530
531     // Draw normal vectors (for visually verifying normals)
532     /*
533     xglBegin(GL_LINES);
534     xglColor3f(0.0, 0.0, 0.0);
535     for ( i = 0; i < t->ncount; i++ ) {
536         xglVertex3d(t->nodes[i][0],
537                     t->nodes[i][1] ,
538                     t->nodes[i][2]);
539         xglVertex3d(t->nodes[i][0] + 500*normals[i][0],
540                     t->nodes[i][1] + 500*normals[i][1],
541                     t->nodes[i][2] + 500*normals[i][2]);
542     } 
543     xglEnd();
544     */   
545
546     stopwatch.stop();
547     FG_LOG( FG_TERRAIN, FG_INFO, 
548             "Loaded " << path << " in " 
549             << stopwatch.elapsedSeconds() << " seconds" );
550
551     // printf("end of tile\n");
552
553     return(1);
554 }
555
556
557 // $Log$
558 // Revision 1.10  1998/11/06 21:18:18  curt
559 // Converted to new logstream debugging facility.  This allows release
560 // builds with no messages at all (and no performance impact) by using
561 // the -DFG_NDEBUG flag.
562 //
563 // Revision 1.9  1998/11/06 14:47:06  curt
564 // Changes to track Bernie's updates to fgstream.
565 //
566 // Revision 1.8  1998/10/20 18:33:55  curt
567 // Tweaked texture coordinates, but we still have some problems. :-(
568 //
569 // Revision 1.7  1998/10/20 15:48:44  curt
570 // Removed an extraneous output message.
571 //
572 // Revision 1.6  1998/10/18 01:17:21  curt
573 // Point3D tweaks.
574 //
575 // Revision 1.5  1998/10/16 00:54:39  curt
576 // Converted to Point3D class.
577 //
578 // Revision 1.4  1998/09/15 01:35:07  curt
579 // cleaned up my fragment.num_faces hack :-) to use the STL (no need in
580 // duplicating work.)
581 // Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
582 // removed some unneeded stuff from fgTileMgrCurElev()
583 //
584 // Revision 1.3  1998/09/03 21:27:03  curt
585 // Fixed a serious bug caused by not-quite-correct comment/white space eating
586 // which resulted in mismatched glBegin() glEnd() pairs, incorrect display lists,
587 // and ugly display artifacts.
588 //
589 // Revision 1.2  1998/09/01 19:03:09  curt
590 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
591 //  - The new classes in libmisc.tgz define a stream interface into zlib.
592 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
593 //    to something more appropriate.  However you'll have to change the
594 //    include directives in all the other files.  Additionally you'll have
595 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
596 //
597 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
598 //    test so I've included the required changes in config.tgz.
599 //
600 //    There are a fair few changes to Simulator/Objects as I've moved
601 //    things around.  Loading tiles is quicker but thats not where the delay
602 //    is.  Tile loading takes a few tenths of a second per file on a P200
603 //    but it seems to be the post-processing that leads to a noticeable
604 //    blip in framerate.  I suppose its time to start profiling to see where
605 //    the delays are.
606 //
607 //    I've included a brief description of each archives contents.
608 //
609 // Lib/Misc/
610 //   zfstream.cxx
611 //   zfstream.hxx
612 //     C++ stream interface into zlib.
613 //     Taken from zlib-1.1.3/contrib/iostream/.
614 //     Minor mods for STL compatibility.
615 //     There's no copyright associated with these so I assume they're
616 //     covered by zlib's.
617 //
618 //   fgstream.cxx
619 //   fgstream.hxx
620 //     FlightGear input stream using gz_ifstream.  Tries to open the
621 //     given filename.  If that fails then filename is examined and a
622 //     ".gz" suffix is removed or appended and that file is opened.
623 //
624 //   stopwatch.hxx
625 //     A simple timer for benchmarking.  Not used in production code.
626 //     Taken from the Blitz++ project.  Covered by GPL.
627 //
628 //   strutils.cxx
629 //   strutils.hxx
630 //     Some simple string manipulation routines.
631 //
632 // Simulator/Airports/
633 //   Load airports database using fgstream.
634 //   Changed fgAIRPORTS to use set<> instead of map<>.
635 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
636 //   Returns true if found.
637 //
638 // Simulator/Astro/
639 //   Modified fgStarsInit() to load stars database using fgstream.
640 //
641 // Simulator/Objects/
642 //   Modified fgObjLoad() to use fgstream.
643 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
644 //   Many changes to fgMATERIAL.
645 //   Some changes to fgFRAGMENT but I forget what!
646 //
647 // Revision 1.1  1998/08/25 16:51:25  curt
648 // Moved from ../Scenery
649 //
650 // Revision 1.23  1998/08/20 15:16:43  curt
651 // obj.cxx: use more explicit parenthases.
652 // texload.[ch]: use const in function definitions where appropriate.
653 //
654 // Revision 1.22  1998/08/20 15:12:03  curt
655 // Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
656 // the need for "void" pointers and casts.
657 // Quick hack to count the number of scenery polygons that are being drawn.
658 //
659 // Revision 1.21  1998/08/12 21:13:04  curt
660 // material.cxx: don't load textures if they are disabled
661 // obj.cxx: optimizations from Norman Vine
662 // tile.cxx: minor tweaks
663 // tile.hxx: addition of num_faces
664 // tilemgr.cxx: minor tweaks
665 //
666 // Revision 1.20  1998/07/24 21:42:07  curt
667 // material.cxx: whups, double method declaration with no definition.
668 // obj.cxx: tweaks to avoid errors in SGI's CC.
669 // tile.cxx: optimizations by Norman Vine.
670 // tilemgr.cxx: optimizations by Norman Vine.
671 //
672 // Revision 1.19  1998/07/13 21:01:58  curt
673 // Wrote access functions for current fgOPTIONS.
674 //
675 // Revision 1.18  1998/07/12 03:18:27  curt
676 // Added ground collision detection.  This involved:
677 // - saving the entire vertex list for each tile with the tile records.
678 // - saving the face list for each fragment with the fragment records.
679 // - code to intersect the current vertical line with the proper face in
680 //   an efficient manner as possible.
681 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
682 //
683 // Revision 1.17  1998/07/08 14:47:21  curt
684 // Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
685 // polare3d.h renamed to polar3d.hxx
686 // fg{Cartesian,Polar}Point3d consolodated.
687 // Added some initial support for calculating local current ground elevation.
688 //
689 // Revision 1.16  1998/07/06 21:34:33  curt
690 // Added using namespace std for compilers that support this.
691 //
692 // Revision 1.15  1998/07/04 00:54:28  curt
693 // Added automatic mipmap generation.
694 //
695 // When rendering fragments, use saved model view matrix from associated tile
696 // rather than recalculating it with push() translate() pop().
697 //
698 // Revision 1.14  1998/06/17 21:36:40  curt
699 // Load and manage multiple textures defined in the Materials library.
700 // Boost max material fagments for each material property to 800.
701 // Multiple texture support when rendering.
702 //
703 // Revision 1.13  1998/06/12 00:58:05  curt
704 // Build only static libraries.
705 // Declare memmove/memset for Sloaris.
706 //
707 // Revision 1.12  1998/06/08 17:57:54  curt
708 // Working first pass at material proporty sorting.
709 //
710 // Revision 1.11  1998/06/06 01:09:31  curt
711 // I goofed on the log message in the last commit ... now fixed.
712 //
713 // Revision 1.10  1998/06/06 01:07:17  curt
714 // Increased per material fragment list size from 100 to 400.
715 // Now correctly draw viewable fragments in per material order.
716 //
717 // Revision 1.9  1998/06/05 22:39:54  curt
718 // Working on sorting by, and rendering by material properties.
719 //
720 // Revision 1.8  1998/06/05 18:19:18  curt
721 // Recognize file, file.gz, and file.obj as scenery object files.
722 //
723 // Revision 1.7  1998/05/24 02:49:09  curt
724 // Implimented fragment level view frustum culling.
725 //
726 // Revision 1.6  1998/05/23 14:09:20  curt
727 // Added tile.cxx and tile.hxx.
728 // Working on rewriting the tile management system so a tile is just a list
729 // fragments, and the fragment record contains the display list for that 
730 // fragment.
731 //
732 // Revision 1.5  1998/05/20 20:53:53  curt
733 // Moved global ref point and radius (bounding sphere info, and offset) to
734 // data file rather than calculating it on the fly.
735 // Fixed polygon winding problem in scenery generation stage rather than
736 // compensating for it on the fly.
737 // Made a fgTILECACHE class.
738 //
739 // Revision 1.4  1998/05/16 13:09:57  curt
740 // Beginning to add support for view frustum culling.
741 // Added some temporary code to calculate bouding radius, until the
742 //   scenery generation tools and scenery can be updated.
743 //
744 // Revision 1.3  1998/05/03 00:48:01  curt
745 // Updated texture coordinate fmod() parameter.
746 //
747 // Revision 1.2  1998/05/02 01:52:14  curt
748 // Playing around with texture coordinates.
749 //
750 // Revision 1.1  1998/04/30 12:35:28  curt
751 // Added a command line rendering option specify smooth/flat shading.
752 //
753 // Revision 1.35  1998/04/28 21:43:26  curt
754 // Wrapped zlib calls up so we can conditionally comment out zlib support.
755 //
756 // Revision 1.34  1998/04/28 01:21:42  curt
757 // Tweaked texture parameter calculations to keep the number smaller.  This
758 // avoids the "swimming" problem.
759 // Type-ified fgTIME and fgVIEW.
760 //
761 // Revision 1.33  1998/04/27 15:58:15  curt
762 // Screwing around with texture coordinate generation ... still needs work.
763 //
764 // Revision 1.32  1998/04/27 03:30:13  curt
765 // Minor transformation adjustments to try to keep scenery tiles closer to
766 // (0, 0, 0)  GLfloats run out of precision at the distances we need to model
767 // the earth, but we can do a bunch of pre-transformations using double math
768 // and then cast to GLfloat once everything is close in where we have less
769 // precision problems.
770 //
771 // Revision 1.31  1998/04/25 15:09:57  curt
772 // Changed "r" to "rb" in gzopen() options.  This fixes bad behavior in win32.
773 //
774 // Revision 1.30  1998/04/24 14:21:08  curt
775 // Added "file.obj.gz" support.
776 //
777 // Revision 1.29  1998/04/24 00:51:07  curt
778 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
779 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
780 // or "file.obz" (compressed.)
781 //
782 // Revision 1.28  1998/04/22 13:22:44  curt
783 // C++ - ifing the code a bit.
784 //
785 // Revision 1.27  1998/04/18 04:13:17  curt
786 // Added zlib on the fly decompression support for loading scenery objects.
787 //
788 // Revision 1.26  1998/04/03 22:11:36  curt
789 // Converting to Gnu autoconf system.
790 //
791 // Revision 1.25  1998/03/14 00:30:50  curt
792 // Beginning initial terrain texturing experiments.
793 //
794 // Revision 1.24  1998/02/09 21:30:18  curt
795 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
796 //
797 // Revision 1.23  1998/02/09 15:07:52  curt
798 // Minor tweaks.
799 //
800 // Revision 1.22  1998/02/01 03:39:54  curt
801 // Minor tweaks.
802 //
803 // Revision 1.21  1998/01/31 00:43:25  curt
804 // Added MetroWorks patches from Carmen Volpe.
805 //
806 // Revision 1.20  1998/01/29 00:51:39  curt
807 // First pass at tile cache, dynamic tile loading and tile unloading now works.
808 //
809 // Revision 1.19  1998/01/27 03:26:42  curt
810 // Playing with new fgPrintf command.
811 //
812 // Revision 1.18  1998/01/19 19:27:16  curt
813 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
814 // This should simplify things tremendously.
815 //
816 // Revision 1.17  1998/01/13 00:23:10  curt
817 // Initial changes to support loading and management of scenery tiles.  Note,
818 // there's still a fair amount of work left to be done.
819 //
820 // Revision 1.16  1997/12/30 23:09:40  curt
821 // Worked on winding problem without luck, so back to calling glFrontFace()
822 // 3 times for each scenery area.
823 //
824 // Revision 1.15  1997/12/30 20:47:51  curt
825 // Integrated new event manager with subsystem initializations.
826 //
827 // Revision 1.14  1997/12/30 01:38:46  curt
828 // Switched back to per vertex normals and smooth shading for terrain.
829 //
830 // Revision 1.13  1997/12/18 23:32:36  curt
831 // First stab at sky dome actually starting to look reasonable. :-)
832 //
833 // Revision 1.12  1997/12/17 23:13:47  curt
834 // Began working on rendering the sky.
835 //
836 // Revision 1.11  1997/12/15 23:55:01  curt
837 // Add xgl wrappers for debugging.
838 // Generate terrain normals on the fly.
839 //
840 // Revision 1.10  1997/12/12 21:41:28  curt
841 // More light/material property tweaking ... still a ways off.
842 //
843 // Revision 1.9  1997/12/12 19:52:57  curt
844 // Working on lightling and material properties.
845 //
846 // Revision 1.8  1997/12/10 01:19:51  curt
847 // Tweaks for verion 0.15 release.
848 //
849 // Revision 1.7  1997/12/08 22:51:17  curt
850 // Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
851 // admission of defeat.  I will eventually go back and get all the stripes
852 // wound the same way (ccw).
853 //
854 // Revision 1.6  1997/11/25 19:25:35  curt
855 // Changes to integrate Durk's moon/sun code updates + clean up.
856 //
857 // Revision 1.5  1997/11/15 18:16:39  curt
858 // minor tweaks.
859 //
860 // Revision 1.4  1997/11/14 00:26:49  curt
861 // Transform scenery coordinates earlier in pipeline when scenery is being
862 // created, not when it is being loaded.  Precalculate normals for each node
863 // as average of the normals of each containing polygon so Garoude shading is
864 // now supportable.
865 //
866 // Revision 1.3  1997/10/31 04:49:12  curt
867 // Tweaking vertex orders.
868 //
869 // Revision 1.2  1997/10/30 12:38:45  curt
870 // Working on new scenery subsystem.
871 //
872 // Revision 1.1  1997/10/28 21:14:54  curt
873 // Initial revision.
874
875