]> git.mxchange.org Git - flightgear.git/blobdiff - Objects/obj.cxx
Converted to new logstream debugging facility. This allows release
[flightgear.git] / Objects / obj.cxx
index ac1ab9a1bd1ab204746e9448a35f1fc349a80feb..64a79b1ef748a8fec68bbcc3e1ee0c0d0b5982c0 100644 (file)
@@ -48,12 +48,13 @@ extern "C" void *memset(void *, int, size_t);
 using namespace std;
 #endif
 
-#include <Debug/fg_debug.h>
+#include <Debug/logstream.hxx>
 #include <Include/fg_constants.h>
 #include <Include/fg_zlib.h>
 #include <Main/options.hxx>
 #include <Math/mat3.h>
 #include <Math/fg_random.h>
+#include <Math/point3d.hxx>
 #include <Math/polar3d.hxx>
 #include <Misc/stopwatch.hxx>
 #include <Misc/fgstream.hxx>
@@ -84,22 +85,40 @@ static void calc_normal(double p1[3], double p2[3],
 }
 
 
-#define FG_TEX_CONSTANT 128.0
+#define FG_TEX_CONSTANT 69.0
 
 
 // Calculate texture coordinates for a given point.
-fgPoint3d calc_tex_coords(double *node, fgPoint3d *ref) {
-    fgPoint3d cp;
-    fgPoint3d pp;
+static Point3D calc_tex_coords(double *node, const Point3D& ref) {
+    Point3D cp;
+    Point3D pp;
+    // double tmplon, tmplat;
 
-    cp.x = node[0] + ref->x; 
-    cp.y = node[1] + ref->y;
-    cp.z = node[2] + ref->z;
+    // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
+    // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
+
+    cp = Point3D( node[0] + ref.x(),
+                 node[1] + ref.y(),
+                 node[2] + ref.z() );
 
     pp = fgCartToPolar3d(cp);
 
-    pp.lon = fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.lon, 25.0);
-    pp.lat = fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.lat, 25.0);
+    // tmplon = pp.lon() * RAD_TO_DEG;
+    // tmplat = pp.lat() * RAD_TO_DEG;
+    // cout << tmplon << " " << tmplat << endl;
+
+    pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
+    pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
+
+    if ( pp.x() < 0.0 ) {
+       pp.setx( pp.x() + 11.0 );
+    }
+
+    if ( pp.y() < 0.0 ) {
+       pp.sety( pp.y() + 11.0 );
+    }
+
+    // cout << pp << endl;
 
     return(pp);
 }
@@ -108,7 +127,7 @@ fgPoint3d calc_tex_coords(double *node, fgPoint3d *ref) {
 // Load a .obj file and build the GL fragment list
 int fgObjLoad( const string& path, fgTILE *t) {
     fgFRAGMENT fragment;
-    fgPoint3d pp;
+    Point3D pp;
     double approx_normal[3], normal[3], scale;
     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
@@ -117,7 +136,9 @@ int fgObjLoad( const string& path, fgTILE *t) {
     int in_fragment, in_faces, vncount, n1, n2, n3, n4;
     int last1, last2, odd;
     double (*nodes)[3];
-    fgPoint3d *center;
+    Point3D center;
+
+    // printf("loading %s\n", path.c_str() );
 
     // Attempt to open "path.gz" or "path"
     fg_gzifstream in( path );
@@ -127,8 +148,7 @@ int fgObjLoad( const string& path, fgTILE *t) {
        in.open( path + ".obj" );
        if ( ! in )
        {
-           fgPrintf( FG_TERRAIN, FG_ALERT, 
-                     "Cannot open file: %s\n", path.c_str() );
+           FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
            return 0;
        }
     }
@@ -140,57 +160,60 @@ int fgObjLoad( const string& path, fgTILE *t) {
     vncount = 1;
     t->bounding_radius = 0.0;
     nodes = t->nodes;
-    center = &t->center;
+    center = t->center;
 
     StopWatch stopwatch;
     stopwatch.start();
+
+    // ignore initial comments and blank lines. (priming the pump)
+    in >> skipcomment;
+
     while ( ! in.eof() )
     {
-       // ignore comments and blank lines.
-       in.eat_comments();
 
        string token;
-       in.stream() >> token;
+       in >> token;
+
+       // printf("token = %s\n", token.c_str() );
+
        if ( token == "gbs" )
        {
            // reference point (center offset)
-           in.stream() >> t->center.x
-                       >> t->center.y
-                       >> t->center.z
-                       >> t->bounding_radius;
+           in >> t->center >> t->bounding_radius;
+           center = t->center;
        }
        else if ( token == "bs" )
        {
            // reference point (center offset)
-           in.stream() >> fragment.center.x
-                       >> fragment.center.y
-                       >> fragment.center.z
-                       >> fragment.bounding_radius;
+           in >> fragment.center;
+           in >> fragment.bounding_radius;
        }
        else if ( token == "vn" )
        {
            // vertex normal
            if ( vncount < MAX_NODES ) {
-               in.stream() >> normals[vncount][0]
-                           >> normals[vncount][1]
-                           >> normals[vncount][2];
+               in >> normals[vncount][0]
+                  >> normals[vncount][1]
+                  >> normals[vncount][2];
                vncount++;
            } else {
-               fgPrintf( FG_TERRAIN, FG_EXIT, 
-                         "Read too many vertex normals ... dying :-(\n");
+               FG_LOG( FG_TERRAIN, FG_ALERT, 
+                       "Read too many vertex normals ... dying :-(" );
+               exit(-1);
            }
        }
        else if ( token[0] == 'v' )
        {
            // node (vertex)
            if ( t->ncount < MAX_NODES ) {
-               in.stream() >> t->nodes[t->ncount][0]
-                           >> t->nodes[t->ncount][1]
-                           >> t->nodes[t->ncount][2];
+               in >> t->nodes[t->ncount][0]
+                  >> t->nodes[t->ncount][1]
+                  >> t->nodes[t->ncount][2];
                t->ncount++;
            } else {
-               fgPrintf( FG_TERRAIN, FG_EXIT, 
-                         "Read too many nodes ... dying :-(\n");
+               FG_LOG( FG_TERRAIN, FG_ALERT, 
+                       "Read too many nodes ... dying :-(");
+               exit(-1);
            }
        }
        else if ( token == "usemtl" )
@@ -202,6 +225,7 @@ int fgObjLoad( const string& path, fgTILE *t) {
                // close out the previous structure and start the next
                xglEnd();
                xglEndList();
+               // printf("xglEnd(); xglEndList();\n");
 
                // update fragment
                fragment.display_list = display_list;
@@ -212,8 +236,11 @@ int fgObjLoad( const string& path, fgTILE *t) {
                in_fragment = 1;
            }
 
+           // printf("start of fragment (usemtl)\n");
+
            display_list = xglGenLists(1);
            xglNewList(display_list, GL_COMPILE);
+           // printf("xglGenLists(); xglNewList();\n");
            in_faces = 0;
 
            // reset the existing face list
@@ -223,14 +250,14 @@ int fgObjLoad( const string& path, fgTILE *t) {
 
            // scan the material line
            string material;
-           in.stream() >> material;
+           in >> material;
            fragment.tile_ptr = t;
 
            // find this material in the properties list
            if ( ! material_mgr.find( material, fragment.material_ptr )) {
-               fgPrintf( FG_TERRAIN, FG_ALERT, 
-                         "Ack! unknown usemtl name = %s in %s\n",
-                         material.c_str(), path.c_str() );
+               FG_LOG( FG_TERRAIN, FG_ALERT, 
+                       "Ack! unknown usemtl name = " << material 
+                       << " in " << path );
            }
 
            // initialize the fragment transformation matrix
@@ -247,12 +274,13 @@ int fgObjLoad( const string& path, fgTILE *t) {
            n1 = n2 = n3 = n4 = 0;
 
            // fgPrintf( FG_TERRAIN, FG_DEBUG, "    new tri strip = %s", line);
-           in.stream() >> n1 >> n2 >> n3;
+           in >> n1 >> n2 >> n3;
            fragment.add_face(n1, n2, n3);
 
            // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
 
            xglBegin(GL_TRIANGLE_STRIP);
+           // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
 
            odd = 1; 
            scale = 1.0;
@@ -263,23 +291,23 @@ int fgObjLoad( const string& path, fgTILE *t) {
                MAT3_SCALE_VEC(normal, normals[n1], scale);
                xglNormal3dv(normal);
                pp = calc_tex_coords(nodes[n1], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
                xglVertex3dv(nodes[n1]);                
 
                MAT3_SCALE_VEC(normal, normals[n2], scale);
                xglNormal3dv(normal);
                pp = calc_tex_coords(nodes[n2], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                //xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
                xglVertex3dv(nodes[n2]);                                
 
                MAT3_SCALE_VEC(normal, normals[n3], scale);
                xglNormal3dv(normal);
                 pp = calc_tex_coords(nodes[n3], center);
-                xglTexCoord2f(pp.lon, pp.lat);
+                xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
-               xglVertex3dv(nodes[n3]);                
+               xglVertex3dv(nodes[n3]);
            } else {
                // Shading model is "GL_FLAT" so calculate per face
                // normals on the fly.
@@ -294,25 +322,41 @@ int fgObjLoad( const string& path, fgTILE *t) {
                xglNormal3dv(normal);
 
                pp = calc_tex_coords(nodes[n1], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
                xglVertex3dv(nodes[n1]);                
 
                pp = calc_tex_coords(nodes[n2], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
                xglVertex3dv(nodes[n2]);                
 
                pp = calc_tex_coords(nodes[n3], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
                xglVertex3dv(nodes[n3]);                
            }
+           // printf("some normals, texcoords, and vertices\n");
 
            odd = 1 - odd;
            last1 = n2;
            last2 = n3;
 
+           // There can be three or four values 
+           char c;
+           while ( in.get(c) )
+           {
+               if ( c == '\n' )
+                   break; // only the one
+
+               if ( isdigit(c) )
+               {
+                   in.putback(c);
+                   in >> n4;
+                   break;
+               }
+           }
+
            if ( n4 > 0 ) {
                fragment.add_face(n3, n2, n4);
 
@@ -327,53 +371,56 @@ int fgObjLoad( const string& path, fgTILE *t) {
                }
                xglNormal3dv(normal);
                pp = calc_tex_coords(nodes[n4], center);
-                xglTexCoord2f(pp.lon, pp.lat);
+                xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n4][0],t->nodes[n4][1],t->nodes[n4][2]);
                xglVertex3dv(nodes[n4]);                
 
                odd = 1 - odd;
                last1 = n3;
                last2 = n4;
+               // printf("a normal, texcoord, and vertex (4th)\n");
            }
        } else if ( token[0] == 'f' ) {
            // unoptimized face
 
            if ( !in_faces ) {
                xglBegin(GL_TRIANGLES);
+               // printf("xglBegin(triangles)\n");
                in_faces = 1;
            }
 
            // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
-           in.stream() >> n1 >> n2 >> n3;
+           in >> n1 >> n2 >> n3;
            fragment.add_face(n1, n2, n3);
 
             // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
            xglNormal3dv(normals[n1]);
            pp = calc_tex_coords(nodes[n1], center);
-           xglTexCoord2f(pp.lon, pp.lat);
+           xglTexCoord2f(pp.lon(), pp.lat());
            // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
            xglVertex3dv(nodes[n1]);
 
             // xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
            xglNormal3dv(normals[n2]);
             pp = calc_tex_coords(nodes[n2], center);
-            xglTexCoord2f(pp.lon, pp.lat);
+            xglTexCoord2f(pp.lon(), pp.lat());
            // xglVertex3d(t->nodes[n2][0], t->nodes[n2][1], t->nodes[n2][2]);
            xglVertex3dv(nodes[n2]);
                
             // xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
            xglNormal3dv(normals[n3]);
             pp = calc_tex_coords(nodes[n3], center);
-            xglTexCoord2f(pp.lon, pp.lat);
+            xglTexCoord2f(pp.lon(), pp.lat());
            // xglVertex3d(t->nodes[n3][0], t->nodes[n3][1], t->nodes[n3][2]);
            xglVertex3dv(nodes[n3]);
+           // printf("some normals, texcoords, and vertices (tris)\n");
        } else if ( token[0] == 'q' ) {
            // continue a triangle strip
            n1 = n2 = 0;
 
            // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
            //           line);
-           in.stream() >> n1;
+           in >> n1;
 
            // There can be one or two values 
            char c;
@@ -385,7 +432,7 @@ int fgObjLoad( const string& path, fgTILE *t) {
                if ( isdigit(c) )
                {
                    in.putback(c);
-                   in.stream() >> n2;
+                   in >> n2;
                    break;
                }
            }
@@ -400,7 +447,6 @@ int fgObjLoad( const string& path, fgTILE *t) {
            if ( shading ) {
                // Shading model is "GL_SMOOTH"
                MAT3_SCALE_VEC(normal, normals[n1], scale);
-               xglNormal3dv(normal);
            } else {
                // Shading model is "GL_FLAT"
                if ( odd ) {
@@ -411,14 +457,15 @@ int fgObjLoad( const string& path, fgTILE *t) {
                                approx_normal);
                }
                MAT3_SCALE_VEC(normal, approx_normal, scale);
-               xglNormal3dv(normal);
            }
+           xglNormal3dv(normal);
 
             pp = calc_tex_coords(nodes[n1], center);
-            xglTexCoord2f(pp.lon, pp.lat);
+            xglTexCoord2f(pp.lon(), pp.lat());
            // xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
            xglVertex3dv(nodes[n1]);
-    
+           // printf("a normal, texcoord, and vertex (4th)\n");
+   
            odd = 1 - odd;
            last1 = last2;
            last2 = n1;
@@ -435,7 +482,6 @@ int fgObjLoad( const string& path, fgTILE *t) {
                if ( shading ) {
                    // Shading model is "GL_SMOOTH"
                    MAT3_SCALE_VEC(normal, normals[n2], scale);
-                   xglNormal3dv(normal);
                } else {
                    // Shading model is "GL_FLAT"
                    if ( odd ) {
@@ -446,31 +492,34 @@ int fgObjLoad( const string& path, fgTILE *t) {
                                    nodes[n2], approx_normal);
                    }
                    MAT3_SCALE_VEC(normal, approx_normal, scale);
-                   xglNormal3dv(normal);
                }
-
+               xglNormal3dv(normal);
+               
                pp = calc_tex_coords(nodes[n2], center);
-               xglTexCoord2f(pp.lon, pp.lat);
+               xglTexCoord2f(pp.lon(), pp.lat());
                // xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
                xglVertex3dv(nodes[n2]);                
+               // printf("a normal, texcoord, and vertex (4th)\n");
 
                odd = 1 -odd;
                last1 = last2;
                last2 = n2;
            }
        } else {
-           fgPrintf( FG_TERRAIN, FG_WARN, "Unknown token in %s = %s\n", 
-                     path.c_str(), token.c_str() );
+           FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
+                   << path << " = " << token );
        }
+
+       // eat comments and blank lines before start of while loop so
+       // if we are done with useful input it is noticed before hand.
+       in >> skipcomment;
     }
-    stopwatch.stop();
-    fgPrintf( FG_TERRAIN, FG_INFO, "Loaded %s in %f seconds\n",
-             path.c_str(), stopwatch.elapsedSeconds() );
 
     if ( in_fragment ) {
        // close out the previous structure and start the next
        xglEnd();
        xglEndList();
+       // printf("xglEnd(); xglEndList();\n");
 
        // update fragment
        fragment.display_list = display_list;
@@ -494,11 +543,49 @@ int fgObjLoad( const string& path, fgTILE *t) {
     xglEnd();
     */   
 
+    stopwatch.stop();
+    FG_LOG( FG_TERRAIN, FG_INFO, 
+           "Loaded " << path << " in " 
+           << stopwatch.elapsedSeconds() << " seconds" );
+
+    // printf("end of tile\n");
+
     return(1);
 }
 
 
 // $Log$
+// Revision 1.10  1998/11/06 21:18:18  curt
+// Converted to new logstream debugging facility.  This allows release
+// builds with no messages at all (and no performance impact) by using
+// the -DFG_NDEBUG flag.
+//
+// Revision 1.9  1998/11/06 14:47:06  curt
+// Changes to track Bernie's updates to fgstream.
+//
+// Revision 1.8  1998/10/20 18:33:55  curt
+// Tweaked texture coordinates, but we still have some problems. :-(
+//
+// Revision 1.7  1998/10/20 15:48:44  curt
+// Removed an extraneous output message.
+//
+// Revision 1.6  1998/10/18 01:17:21  curt
+// Point3D tweaks.
+//
+// Revision 1.5  1998/10/16 00:54:39  curt
+// Converted to Point3D class.
+//
+// Revision 1.4  1998/09/15 01:35:07  curt
+// cleaned up my fragment.num_faces hack :-) to use the STL (no need in
+// duplicating work.)
+// Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
+// removed some unneeded stuff from fgTileMgrCurElev()
+//
+// Revision 1.3  1998/09/03 21:27:03  curt
+// Fixed a serious bug caused by not-quite-correct comment/white space eating
+// which resulted in mismatched glBegin() glEnd() pairs, incorrect display lists,
+// and ugly display artifacts.
+//
 // Revision 1.2  1998/09/01 19:03:09  curt
 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
 //  - The new classes in libmisc.tgz define a stream interface into zlib.