]> git.mxchange.org Git - flightgear.git/blobdiff - src/Objects/obj.cxx
Lighting/ssgSimpleState fixes.
[flightgear.git] / src / Objects / obj.cxx
index 4b41122f45caf6a70d71520c17dcc9d59fb18cc8..d1c19320c4d1bec596c6e0c62d53d2f4afd26c02 100644 (file)
 #  include <math.h>
 #endif
 
-#ifdef HAVE_WINDOWS_H
-#  include <windows.h>
-#endif
-
 #include <stdio.h>
 #include <string.h>
-#include <GL/glut.h>
-#include <XGL/xgl.h>
 
 // #if defined ( __sun__ )
 // extern "C" void *memmove(void *, const void *, size_t);
@@ -55,6 +49,7 @@
 #include <Include/fg_constants.h>
 #include <Main/options.hxx>
 #include <Math/mat3.h>
+#include <Math/fg_geodesy.hxx>
 #include <Math/fg_random.h>
 #include <Math/point3d.hxx>
 #include <Math/polar3d.hxx>
@@ -134,14 +129,136 @@ static Point3D calc_tex_coords(const Point3D& node, const Point3D& ref) {
 }
 
 
-// Load a .obj file and build the GL fragment list
+// Generate a generic ocean tile on the fly
+ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
+    fgFRAGMENT fragment;
+    fragment.init();
+    fragment.tile_ptr = t;
+
+    ssgSimpleState *state = NULL;
+
+    ssgBranch *tile = new ssgBranch () ;
+    tile -> setName ( (char *)path.c_str() ) ;
+
+    // find Ocean material in the properties list
+    if ( ! material_mgr.find( "Ocean", fragment.material_ptr )) {
+       FG_LOG( FG_TERRAIN, FG_ALERT, 
+               "Ack! unknown usemtl name = " << "Ocean" 
+               << " in " << path );
+    }
+
+    // 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();
+
+    // set ssgState
+    state = fragment.material_ptr->get_state();
+
+    // Calculate center point
+    FGBucket b = t->tile_bucket;
+    double clon = b.get_center_lon();
+    double clat = b.get_center_lat();
+    double height = b.get_height();
+    double width = b.get_width();
+
+    Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
+    t->center = center;
+    fragment.center = center;
+    // cout << "center = " << center << endl;;
+    
+    // Caculate corner vertices
+    Point3D geod[4];
+    geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
+    geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
+    geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
+    geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
+
+    Point3D rad[4];
+    for ( int 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 ) {
+       cart[i] = fgGeodToCart(rad[i]);
+       rel[i] = cart[i] - center;
+       t->nodes.push_back( rel[i] );
+       // cout << "corner " << i << " = " << cart[i] << endl;
+    }
+
+    t->ncount = 4;
+
+    // Calculate bounding radius
+    t->bounding_radius = center.distance3D( cart[0] );
+    fragment.bounding_radius = t->bounding_radius;
+    // cout << "bounding radius = " << t->bounding_radius << endl;
+
+    // Calculate normals
+    Point3D normals[4];
+    for ( int i = 0; i < 4; ++i ) {
+       normals[i] = cart[i];
+       double length = normals[i].distance3D( Point3D(0.0) );
+       normals[i] /= length;
+       // cout << "normal = " << normals[i] << endl;
+    }
+
+    // Calculate texture coordinates
+    Point3D texs[4];
+    for ( int 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);
+    t->fragment_list.push_back(fragment);
+
+    // Build ssg structure
+    t->vtlist = new sgVec3 [ 4 ];
+    t->vnlist = new sgVec3 [ 4 ];
+    t->tclist = new sgVec2 [ 4 ];
+
+    for ( int i = 0; i < 4; ++i ) {
+       sgSetVec3( t->vtlist[i], 
+                  rel[i].x(), rel[i].y(), rel[i].z() );
+       sgSetVec3( t->vnlist[i], 
+                  normals[i].x(), normals[i].y(), normals[i].z() );
+       sgSetVec2( t->tclist[i], texs[i].x(), texs[i].y() );
+    }
+    
+    unsigned short *vindex = new unsigned short [ 4 ];
+    unsigned short *tindex = new unsigned short [ 4 ];
+    for ( int i = 0; i < 4; ++i ) {
+       vindex[i] = i;
+       tindex[i] = i;
+    }
+
+    ssgLeaf *leaf = 
+       new ssgVTable ( GL_TRIANGLE_FAN,
+                       4, vindex, t->vtlist,
+                       4, vindex, t->vnlist,
+                       4, tindex, t->tclist,
+                       0, NULL, NULL ) ;
+    leaf->setState( state );
+
+    tile->addKid( leaf );
+
+    return tile;
+}
+
+
+// Load a .obj file and build the fragment list
 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
     fgFRAGMENT fragment;
     Point3D pp;
-    double approx_normal[3], normal[3] /*, scale = 0.0 */;
+    double approx_normal[3] /*, normal[3], scale = 0.0 */;
     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
-    GLint display_list = 0;
+    // GLint display_list = 0;
     int shading;
     bool in_fragment = false, in_faces = false;
     int vncount, vtcount;
@@ -158,13 +275,16 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
     int i;
     ssgSimpleState *state = NULL;
 
-    // printf("loading %s\n", path.c_str() );
+    ssgBranch *tile = new ssgBranch () ;
+    tile -> setName ( (char *)path.c_str() ) ;
 
     // Attempt to open "path.gz" or "path"
     fg_gzifstream in( path );
     if ( ! in.is_open() ) {
        FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
-       return 0;
+       FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
+
+       return fgGenTile( path, t );
     }
 
     shading = current_options.get_shading();
@@ -179,16 +299,19 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
     StopWatch stopwatch;
     stopwatch.start();
 
-    ssgBranch *tile = new ssgBranch () ;
-    tile -> setName ( path.c_str() ) ;
-
     // ignore initial comments and blank lines. (priming the pump)
     // in >> skipcomment;
-    string line;
+    // string line;
 
+    string token;
+    char c;
+
+#ifdef __MWERKS__
+    while ( in.get(c) && c  != '\0' ) {
+       in.putback(c);
+#else
     while ( ! in.eof() ) {
-       string token;
-       char c;
+#endif
 
 #if defined( MACOS )
        in >> ::skipws;
@@ -223,6 +346,13 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                // if first usemtl with shared_done = false, then set
                // shared_done true and build the ssg shared lists
                if ( ! shared_done ) {
+                   // sanity check
+                   if ( (int)nodes.size() != vncount ) {
+                       FG_LOG( FG_TERRAIN, FG_ALERT, 
+                               "Tile has mismatched nodes and normals: " 
+                               << path );
+                       // exit(-1);
+                   }
                    shared_done = true;
 
                    t->vtlist = new sgVec3 [ nodes.size() ];
@@ -246,18 +376,18 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                }
 
                // series of individual triangles
-               if ( in_faces ) {
-                   xglEnd();
-               }
+               // if ( in_faces ) {
+               //     xglEnd();
+               // }
 
                // this also signals the start of a new fragment
                if ( in_fragment ) {
                    // close out the previous structure and start the next
-                   xglEndList();
+                   // xglEndList();
                    // printf("xglEnd(); xglEndList();\n");
 
                    // update fragment
-                   fragment.display_list = display_list;
+                   // fragment.display_list = display_list;
 
                    // push this fragment onto the tile's object list
                    t->fragment_list.push_back(fragment);
@@ -267,8 +397,8 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
 
                // printf("start of fragment (usemtl)\n");
 
-               display_list = xglGenLists(1);
-               xglNewList(display_list, GL_COMPILE);
+               // display_list = xglGenLists(1);
+               // xglNewList(display_list, GL_COMPILE);
                // printf("xglGenLists(); xglNewList();\n");
                in_faces = false;
 
@@ -372,7 +502,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
 
                // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
 
-               xglBegin(GL_TRIANGLE_STRIP);
+               // xglBegin(GL_TRIANGLE_STRIP);
                // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
 
                odd = 1; 
@@ -382,22 +512,22 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    // Shading model is "GL_SMOOTH" so use precalculated
                    // (averaged) normals
                    // MAT3_SCALE_VEC(normal, normals[n1], scale);
-                   xglNormal3dv(normal);
+                   // xglNormal3dv(normal);
                    pp = calc_tex_coords(nodes[n1], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n1].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n1].get_n());         
 
                    // MAT3_SCALE_VEC(normal, normals[n2], scale);
-                   xglNormal3dv(normal);
+                   // xglNormal3dv(normal);
                    pp = calc_tex_coords(nodes[n2], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n2].get_n());                            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n2].get_n());                         
 
                    // MAT3_SCALE_VEC(normal, normals[n3], scale);
-                   xglNormal3dv(normal);
+                   // xglNormal3dv(normal);
                    pp = calc_tex_coords(nodes[n3], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n3].get_n());
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n3].get_n());
                } else {
                    // Shading model is "GL_FLAT" so calculate per face
                    // normals on the fly.
@@ -409,19 +539,19 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                                    nodes[n3], 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());
-                   xglVertex3dv(nodes[n1].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n1].get_n());         
 
                    pp = calc_tex_coords(nodes[n2], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n2].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n2].get_n());         
                    
                    pp = calc_tex_coords(nodes[n3], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n3].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n3].get_n());         
                }
                // printf("some normals, texcoords, and vertices\n");
 
@@ -454,10 +584,10 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                                    approx_normal);
                        // MAT3_SCALE_VEC(normal, approx_normal, scale);
                    }
-                   xglNormal3dv(normal);
+                   // xglNormal3dv(normal);
                    pp = calc_tex_coords(nodes[n4], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n4].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n4].get_n());         
                    
                    odd = 1 - odd;
                    last1 = n3;
@@ -471,11 +601,11 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                fan_vertices.clear();
                fan_tex_coords.clear();
 
-               xglBegin(GL_TRIANGLE_FAN);
+               // xglBegin(GL_TRIANGLE_FAN);
 
                in >> n1;
                fan_vertices.push_back( n1 );
-               xglNormal3dv(normals[n1]);
+               // xglNormal3dv(normals[n1]);
                if ( in.get( c ) && c == '/' ) {
                    in >> tex;
                    fan_tex_coords.push_back( tex );
@@ -485,12 +615,12 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    in.putback( c );
                    pp = calc_tex_coords(nodes[n1], center);
                }
-               xglTexCoord2f(pp.x(), pp.y());
-               xglVertex3dv(nodes[n1].get_n());
+               // xglTexCoord2f(pp.x(), pp.y());
+               // xglVertex3dv(nodes[n1].get_n());
 
                in >> n2;
                fan_vertices.push_back( n2 );
-               xglNormal3dv(normals[n2]);
+               // xglNormal3dv(normals[n2]);
                if ( in.get( c ) && c == '/' ) {
                    in >> tex;
                    fan_tex_coords.push_back( tex );
@@ -500,8 +630,8 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    in.putback( c );
                    pp = calc_tex_coords(nodes[n2], center);
                }
-               xglTexCoord2f(pp.x(), pp.y());
-               xglVertex3dv(nodes[n2].get_n());
+               // xglTexCoord2f(pp.x(), pp.y());
+               // xglVertex3dv(nodes[n2].get_n());
                
                // read all subsequent numbers until next thing isn't a number
                while ( true ) {
@@ -523,7 +653,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    // cout << "  triangle = " 
                    //      << n1 << "," << n2 << "," << n3 
                    //      << endl;
-                   xglNormal3dv(normals[n3]);
+                   // xglNormal3dv(normals[n3]);
                    if ( in.get( c ) && c == '/' ) {
                        in >> tex;
                        fan_tex_coords.push_back( tex );
@@ -533,14 +663,14 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                        in.putback( c );
                        pp = calc_tex_coords(nodes[n3], center);
                    }
-                   xglTexCoord2f(pp.x(), pp.y());
-                   xglVertex3dv(nodes[n3].get_n());
+                   // xglTexCoord2f(pp.x(), pp.y());
+                   // xglVertex3dv(nodes[n3].get_n());
 
                    fragment.add_face(n1, n2, n3);
                    n2 = n3;
                }
 
-               xglEnd();
+               // xglEnd();
 
                // build the ssg entity
                unsigned short *vindex = 
@@ -567,7 +697,7 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                // unoptimized face
 
                if ( !in_faces ) {
-                   xglBegin(GL_TRIANGLES);
+                   // xglBegin(GL_TRIANGLES);
                    // printf("xglBegin(triangles)\n");
                    in_faces = true;
                }
@@ -577,20 +707,20 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                fragment.add_face(n1, n2, n3);
 
                // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
-               xglNormal3dv(normals[n1]);
+               // xglNormal3dv(normals[n1]);
                pp = calc_tex_coords(nodes[n1], center);
-               xglTexCoord2f(pp.lon(), pp.lat());
-               xglVertex3dv(nodes[n1].get_n());
+               // xglTexCoord2f(pp.lon(), pp.lat());
+               // xglVertex3dv(nodes[n1].get_n());
 
-               xglNormal3dv(normals[n2]);
+               // xglNormal3dv(normals[n2]);
                pp = calc_tex_coords(nodes[n2], center);
-               xglTexCoord2f(pp.lon(), pp.lat());
-               xglVertex3dv(nodes[n2].get_n());
+               // xglTexCoord2f(pp.lon(), pp.lat());
+               // xglVertex3dv(nodes[n2].get_n());
                
-               xglNormal3dv(normals[n3]);
+               // xglNormal3dv(normals[n3]);
                pp = calc_tex_coords(nodes[n3], center);
-               xglTexCoord2f(pp.lon(), pp.lat());
-               xglVertex3dv(nodes[n3].get_n());
+               // xglTexCoord2f(pp.lon(), pp.lat());
+               // xglVertex3dv(nodes[n3].get_n());
                // printf("some normals, texcoords, and vertices (tris)\n");
            } else if ( token == "q" ) {
                // continue a triangle strip
@@ -635,11 +765,11 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                    }
                    // MAT3_SCALE_VEC(normal, approx_normal, scale);
                }
-               xglNormal3dv(normal);
+               // xglNormal3dv(normal);
 
                pp = calc_tex_coords(nodes[n1], center);
-               xglTexCoord2f(pp.lon(), pp.lat());
-               xglVertex3dv(nodes[n1].get_n());
+               // xglTexCoord2f(pp.lon(), pp.lat());
+               // xglVertex3dv(nodes[n1].get_n());
                // printf("a normal, texcoord, and vertex (4th)\n");
    
                odd = 1 - odd;
@@ -669,11 +799,11 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
                        }
                        // MAT3_SCALE_VEC(normal, approx_normal, scale);
                    }
-                   xglNormal3dv(normal);
+                   // xglNormal3dv(normal);
                
                    pp = calc_tex_coords(nodes[n2], center);
-                   xglTexCoord2f(pp.lon(), pp.lat());
-                   xglVertex3dv(nodes[n2].get_n());            
+                   // xglTexCoord2f(pp.lon(), pp.lat());
+                   // xglVertex3dv(nodes[n2].get_n());         
                    // printf("a normal, texcoord, and vertex (4th)\n");
 
                    odd = 1 -odd;
@@ -697,12 +827,12 @@ ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
 
     if ( in_fragment ) {
        // close out the previous structure and start the next
-       xglEnd();
-       xglEndList();
+       // xglEnd();
+       // xglEndList();
        // printf("xglEnd(); xglEndList();\n");
        
        // update fragment
-       fragment.display_list = display_list;
+       // fragment.display_list = display_list;
        
        // push this fragment onto the tile's object list
        t->fragment_list.push_back(fragment);