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