]> git.mxchange.org Git - flightgear.git/blobdiff - src/Objects/obj.cxx
Changes to properly handle new style tri-strips.
[flightgear.git] / src / Objects / obj.cxx
index d1c19320c4d1bec596c6e0c62d53d2f4afd26c02..4d3cdfb17dbc634d8a63b34c62a30a4a54e2b4ed 100644 (file)
@@ -150,8 +150,8 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
     // set the texture width and height values for this
     // material
     FGMaterial m = fragment.material_ptr->get_m();
-    double tex_width = m.get_xsize();
-    double tex_height = m.get_ysize();
+    // double tex_width = m.get_xsize();
+    // double tex_height = m.get_ysize();
 
     // set ssgState
     state = fragment.material_ptr->get_state();
@@ -176,13 +176,15 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
 
     Point3D rad[4];
-    for ( int i = 0; i < 4; ++i ) {
+    int i;
+    for ( i = 0; i < 4; ++i ) {
        rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
                          geod[i].z() );
     }
 
     Point3D cart[4], rel[4];
-    for ( int i = 0; i < 4; ++i ) {
+    t->nodes.clear();
+    for ( i = 0; i < 4; ++i ) {
        cart[i] = fgGeodToCart(rad[i]);
        rel[i] = cart[i] - center;
        t->nodes.push_back( rel[i] );
@@ -198,7 +200,7 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
 
     // Calculate normals
     Point3D normals[4];
-    for ( int i = 0; i < 4; ++i ) {
+    for ( i = 0; i < 4; ++i ) {
        normals[i] = cart[i];
        double length = normals[i].distance3D( Point3D(0.0) );
        normals[i] /= length;
@@ -207,14 +209,14 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
 
     // Calculate texture coordinates
     Point3D texs[4];
-    for ( int i = 0; i < 4; ++i ) {
+    for ( i = 0; i < 4; ++i ) {
        texs[i] = calc_tex_coords( rel[i], center );
        // cout << "texture coordinate = " << texs[i] << endl;
     }
 
     // Build flight gear structure
-    fragment.add_face(1, 2, 3);
-    fragment.add_face(1, 3, 4);
+    fragment.add_face(0, 1, 2);
+    fragment.add_face(0, 2, 3);
     t->fragment_list.push_back(fragment);
 
     // Build ssg structure
@@ -222,7 +224,7 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
     t->vnlist = new sgVec3 [ 4 ];
     t->tclist = new sgVec2 [ 4 ];
 
-    for ( int i = 0; i < 4; ++i ) {
+    for ( i = 0; i < 4; ++i ) {
        sgSetVec3( t->vtlist[i], 
                   rel[i].x(), rel[i].y(), rel[i].z() );
        sgSetVec3( t->vnlist[i], 
@@ -232,7 +234,7 @@ ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
     
     unsigned short *vindex = new unsigned short [ 4 ];
     unsigned short *tindex = new unsigned short [ 4 ];
-    for ( int i = 0; i < 4; ++i ) {
+    for ( i = 0; i < 4; ++i ) {
        vindex[i] = i;
        tindex[i] = i;
     }
@@ -264,10 +266,12 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
     int vncount, vtcount;
     int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
     int tex;
-    int last1 = 0, last2 = 0, odd = 0;
+    int last1 = 0, last2 = 0;
+    bool odd = false;
     point_list nodes;
     Point3D node;
     Point3D center;
+    double scenery_version = 0.0;
     double tex_width = 1000.0, tex_height = 1000.0;
     bool shared_done = false;
     int_list fan_vertices;
@@ -327,7 +331,11 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
 
            in >> token;
 
-           if ( token == "gbs" ) {
+           if ( token == "Version" ) {
+               // read scenery versions number
+               in >> scenery_version;
+               // cout << "scenery_version = " << scenery_version << endl;
+           } else if ( token == "gbs" ) {
                // reference point (center offset)
                in >> t->center >> t->bounding_radius;
                center = t->center;
@@ -371,7 +379,8 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    }
                    for ( i = 0; i < vtcount; ++i ) {
                        sgSetVec2( t->tclist[i],
-                                  tex_coords[i][0], tex_coords[i][1] );
+                                  tex_coords[i][0],
+                                  tex_coords[i][1] );
                    }
                }
 
@@ -505,7 +514,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                // xglBegin(GL_TRIANGLE_STRIP);
                // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
 
-               odd = 1
+               odd = true
                // scale = 1.0;
 
                if ( shading ) {
@@ -555,7 +564,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                }
                // printf("some normals, texcoords, and vertices\n");
 
-               odd = 1 - odd;
+               odd = !odd;
                last1 = n2;
                last2 = n3;
 
@@ -574,7 +583,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
 
                if ( n4 > 0 ) {
                    fragment.add_face(n3, n2, n4);
-
+                   
                    if ( shading ) {
                        // Shading model is "GL_SMOOTH"
                        // MAT3_SCALE_VEC(normal, normals[n4], scale);
@@ -589,17 +598,18 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    // xglTexCoord2f(pp.lon(), pp.lat());
                    // xglVertex3dv(nodes[n4].get_n());         
                    
-                   odd = 1 - odd;
+                   odd = !odd;
                    last1 = n3;
                    last2 = n4;
                    // printf("a normal, texcoord, and vertex (4th)\n");
                }
-           } else if ( token == "tf" ) {
+           } else if ( (token == "tf") || (token == "ts") ) {
                // triangle fan
                // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
 
                fan_vertices.clear();
                fan_tex_coords.clear();
+               odd = true;
 
                // xglBegin(GL_TRIANGLE_FAN);
 
@@ -609,6 +619,14 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                if ( in.get( c ) && c == '/' ) {
                    in >> tex;
                    fan_tex_coords.push_back( tex );
+                   if ( scenery_version >= 0.4 ) {
+                       if ( tex_width > 0 ) {
+                           t->tclist[tex][0] *= (1000.0 / tex_width);
+                       }
+                       if ( tex_height > 0 ) {
+                           t->tclist[tex][1] *= (1000.0 / tex_height);
+                       }
+                   }
                    pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
                    pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
                } else {
@@ -624,6 +642,14 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                if ( in.get( c ) && c == '/' ) {
                    in >> tex;
                    fan_tex_coords.push_back( tex );
+                   if ( scenery_version >= 0.4 ) {
+                       if ( tex_width > 0 ) {
+                           t->tclist[tex][0] *= (1000.0 / tex_width);
+                       }
+                       if ( tex_height > 0 ) {
+                           t->tclist[tex][1] *= (1000.0 / tex_height);
+                       }
+                   }
                    pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
                    pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
                } else {
@@ -657,6 +683,14 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    if ( in.get( c ) && c == '/' ) {
                        in >> tex;
                        fan_tex_coords.push_back( tex );
+                       if ( scenery_version >= 0.4 ) {
+                           if ( tex_width > 0 ) {
+                               t->tclist[tex][0] *= (1000.0 / tex_width);
+                           }
+                           if ( tex_height > 0 ) {
+                               t->tclist[tex][1] *= (1000.0 / tex_height);
+                           }
+                       }
                        pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
                        pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
                    } else {
@@ -666,7 +700,14 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    // xglTexCoord2f(pp.x(), pp.y());
                    // xglVertex3dv(nodes[n3].get_n());
 
-                   fragment.add_face(n1, n2, n3);
+                   if ( odd ) {
+                       fragment.add_face(n1, n2, n3);
+                   } else {
+                       fragment.add_face(n2, n1, n3);
+                   }
+
+                   odd = !odd;
+                   n1 = n2;
                    n2 = n3;
                }
 
@@ -683,12 +724,24 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
                    tindex[i] = fan_tex_coords[i];
                }
-               ssgLeaf *leaf = 
-                   new ssgVTable ( GL_TRIANGLE_FAN,
-                                   fan_vertices.size(), vindex, t->vtlist,
-                                   fan_vertices.size(), vindex, t->vnlist,
-                                   fan_tex_coords.size(), tindex, t->tclist,
-                                   0, NULL, NULL ) ;
+               ssgLeaf *leaf;
+               if ( token == "tf" ) {
+                   // triangle fan
+                   leaf = 
+                       new ssgVTable ( GL_TRIANGLE_FAN,
+                                       fan_vertices.size(), vindex, t->vtlist,
+                                       fan_vertices.size(), vindex, t->vnlist,
+                                       fan_tex_coords.size(), tindex,t->tclist,
+                                       0, NULL, NULL ) ;
+               } else {
+                   // triangle strip
+                   leaf = 
+                       new ssgVTable ( GL_TRIANGLE_STRIP,
+                                       fan_vertices.size(), vindex, t->vtlist,
+                                       fan_vertices.size(), vindex, t->vnlist,
+                                       fan_tex_coords.size(), tindex,t->tclist,
+                                       0, NULL, NULL ) ;
+               }
                leaf->setState( state );
 
                tile->addKid( leaf );
@@ -772,7 +825,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                // xglVertex3dv(nodes[n1].get_n());
                // printf("a normal, texcoord, and vertex (4th)\n");
    
-               odd = 1 - odd;
+               odd = !odd;
                last1 = last2;
                last2 = n1;
 
@@ -806,7 +859,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    // xglVertex3dv(nodes[n2].get_n());         
                    // printf("a normal, texcoord, and vertex (4th)\n");
 
-                   odd = 1 -odd;
+                   odd = !odd;
                    last1 = last2;
                    last2 = n2;
                }
@@ -856,7 +909,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
     t->nodes = nodes;
 
     stopwatch.stop();
-    FG_LOG( FG_TERRAIN, FG_INFO
+    FG_LOG( FG_TERRAIN, FG_DEBUG
            "Loaded " << path << " in " 
            << stopwatch.elapsedSeconds() << " seconds" );