]> git.mxchange.org Git - flightgear.git/blobdiff - src/Objects/obj.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / Objects / obj.cxx
index 85395e309406d02aee09db09734f780067b3e48a..09fab77b734bb91132e79282a19d74aaf160eddf 100644 (file)
@@ -1,4 +1,4 @@
-// obj.hxx -- routines to handle loading scenery and building the plib
+// obj.cxx -- routines to handle loading scenery and building the plib
 //            scene graph.
 //
 // Written by Curtis Olson, started October 1997.
 #include <simgear/misc/texcoord.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/material/matlib.hxx>
+#include <simgear/scene/tgdb/leaf.hxx>
+#include <simgear/scene/tgdb/pt_lights.hxx>
 
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Time/light.hxx>
-#include <Scenery/tileentry.hxx>
 
-#include "pt_lights.hxx"
+
 #include "obj.hxx"
 
 SG_USING_STD(string);
@@ -85,29 +86,26 @@ runway_lights_pretrav (ssgEntity * e, int mask)
 
 // Generate an ocean tile
 bool fgGenTile( const string& path, SGBucket b,
-                      Point3D *center,
-                      double *bounding_radius,
-                      ssgBranch* geometry )
+                Point3D *center, double *bounding_radius,
+                SGMaterialLib *matlib, ssgBranch* geometry )
 {
-    SGMaterial *newmat;
-
     ssgSimpleState *state = NULL;
 
-    geometry -> setName ( (char *)path.c_str() ) ;
+    geometry->setName( (char *)path.c_str() );
 
     double tex_width = 1000.0;
     // double tex_height;
 
     // find Ocean material in the properties list
-    newmat = material_lib.find( "Ocean" );
-    if ( newmat != NULL ) {
+    SGMaterial *mat = matlib->find( "Ocean" );
+    if ( mat != NULL ) {
         // set the texture width and height values for this
         // material
-        tex_width = newmat->get_xsize();
+        tex_width = mat->get_xsize();
         // tex_height = newmat->get_ysize();
         
         // set ssgState
-        state = newmat->get_state();
+        state = mat->get_state();
     } else {
         SG_LOG( SG_TERRAIN, SG_ALERT, 
                 "Ack! unknown usemtl name = " << "Ocean" 
@@ -233,48 +231,6 @@ static void random_pt_inside_tri( float *res,
 }
 
 
-static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
-                                       double factor ) {
-    int tris = leaf->getNumTriangles();
-    if ( tris > 0 ) {
-        short int n1, n2, n3;
-        float *p1, *p2, *p3;
-        sgVec3 result;
-
-        // generate a repeatable random seed
-        p1 = leaf->getVertex( 0 );
-        unsigned int seed = (unsigned int)(fabs(p1[0]*100));
-        sg_srandom( seed );
-
-        for ( int i = 0; i < tris; ++i ) {
-            leaf->getTriangle( i, &n1, &n2, &n3 );
-            p1 = leaf->getVertex(n1);
-            p2 = leaf->getVertex(n2);
-            p3 = leaf->getVertex(n3);
-            double area = sgTriArea( p1, p2, p3 );
-            double num = area / factor;
-
-            // generate a light point for each unit of area
-            while ( num > 1.0 ) {
-                random_pt_inside_tri( result, p1, p2, p3 );
-                lights->add( result );
-                num -= 1.0;
-            }
-            // for partial units of area, use a zombie door method to
-            // create the proper random chance of a light being created
-            // for this triangle
-            if ( num > 0.0 ) {
-                if ( sg_random() <= num ) {
-                    // a zombie made it through our door
-                    random_pt_inside_tri( result, p1, p2, p3 );
-                    lights->add( result );
-                }
-            }
-        }
-    }
-}
-
-
 /**
  * User data for populating leaves when they come in range.
  */
@@ -282,9 +238,9 @@ class LeafUserData : public ssgBase
 {
 public:
     bool is_filled_in;
-    ssgLeaf * leaf;
-    SGMaterial * mat;
-    ssgBranch * branch;
+    ssgLeaf *leaf;
+    SGMaterial *mat;
+    ssgBranch *branch;
     float sin_lat;
     float cos_lat;
     float sin_lon;
@@ -660,26 +616,19 @@ leaf_out_of_range_callback (ssgEntity * entity, int mask)
 static void
 gen_random_surface_objects (ssgLeaf *leaf,
                             ssgBranch *branch,
-                            Point3D * center,
-                            const string &material_name)
+                            Point3D *center,
+                            SGMaterial *mat )
 {
                                 // If the surface has no triangles, return
                                 // now.
     int num_tris = leaf->getNumTriangles();
     if (num_tris < 1)
-      return;
-
-                                // Get the material for this surface.
-    SGMaterial * mat = material_lib.find(material_name);
-    if (mat == 0) {
-      SG_LOG(SG_INPUT, SG_ALERT, "Unknown material " << material_name);
-      return;
-    }
+        return;
 
                                 // If the material has no randomly-placed
                                 // objects, return now.
     if (mat->get_object_group_count() < 1)
-      return;
+        return;
 
                                 // Calculate the geodetic centre of
                                 // the tile, for aligning automatic
@@ -730,155 +679,11 @@ gen_random_surface_objects (ssgLeaf *leaf,
 // Scenery loaders.
 ////////////////////////////////////////////////////////////////////////
 
-ssgLeaf *gen_leaf( const string& path,
-                   const GLenum ty, const string& material,
-                   const point_list& nodes, const point_list& normals,
-                   const point_list& texcoords,
-                   const int_list& node_index,
-                   const int_list& normal_index,
-                   const int_list& tex_index,
-                   const bool calc_lights, ssgVertexArray *lights )
-{
-    double tex_width = 1000.0, tex_height = 1000.0;
-    ssgSimpleState *state = NULL;
-    float coverage = -1;
-
-    SGMaterial *newmat = material_lib.find( material );
-    if ( newmat == NULL ) {
-        // see if this is an on the fly texture
-        string file = path;
-        string::size_type pos = file.rfind( "/" );
-        file = file.substr( 0, pos );
-        // cout << "current file = " << file << endl;
-        file += "/";
-        file += material;
-        // cout << "current file = " << file << endl;
-        if ( ! material_lib.add_item( file ) ) {
-            SG_LOG( SG_TERRAIN, SG_ALERT, 
-                    "Ack! unknown usemtl name = " << material 
-                    << " in " << path );
-        } else {
-            // locate our newly created material
-            newmat = material_lib.find( material );
-            if ( newmat == NULL ) {
-                SG_LOG( SG_TERRAIN, SG_ALERT, 
-                        "Ack! bad on the fly material create = "
-                        << material << " in " << path );
-            }
-        }
-    }
-
-    if ( newmat != NULL ) {
-        // set the texture width and height values for this
-        // material
-        tex_width = newmat->get_xsize();
-        tex_height = newmat->get_ysize();
-        state = newmat->get_state();
-        coverage = newmat->get_light_coverage();
-        // cout << "(w) = " << tex_width << " (h) = "
-        //      << tex_width << endl;
-    } else {
-        coverage = -1;
-    }
-
-    sgVec2 tmp2;
-    sgVec3 tmp3;
-    sgVec4 tmp4;
-    int i;
-
-    // vertices
-    int size = node_index.size();
-    if ( size < 1 ) {
-        SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! node list size < 1" );
-        exit(-1);
-    }
-    ssgVertexArray *vl = new ssgVertexArray( size );
-    Point3D node;
-    for ( i = 0; i < size; ++i ) {
-        node = nodes[ node_index[i] ];
-        sgSetVec3( tmp3, node[0], node[1], node[2] );
-        vl -> add( tmp3 );
-    }
-
-    // normals
-    Point3D normal;
-    ssgNormalArray *nl = new ssgNormalArray( size );
-    if ( normal_index.size() ) {
-        // object file specifies normal indices (i.e. normal indices
-        // aren't 'implied'
-        for ( i = 0; i < size; ++i ) {
-            normal = normals[ normal_index[i] ];
-            sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
-            nl -> add( tmp3 );
-        }
-    } else {
-        // use implied normal indices.  normal index = vertex index.
-        for ( i = 0; i < size; ++i ) {
-            normal = normals[ node_index[i] ];
-            sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
-            nl -> add( tmp3 );
-        }
-    }
-
-    // colors
-    ssgColourArray *cl = new ssgColourArray( 1 );
-    sgSetVec4( tmp4, 1.0, 1.0, 1.0, 1.0 );
-    cl->add( tmp4 );
-
-    // texture coordinates
-    size = tex_index.size();
-    Point3D texcoord;
-    ssgTexCoordArray *tl = new ssgTexCoordArray( size );
-    if ( size == 1 ) {
-        texcoord = texcoords[ tex_index[0] ];
-        sgSetVec2( tmp2, texcoord[0], texcoord[1] );
-        sgSetVec2( tmp2, texcoord[0], texcoord[1] );
-        if ( tex_width > 0 ) {
-            tmp2[0] *= (1000.0 / tex_width);
-        }
-        if ( tex_height > 0 ) {
-            tmp2[1] *= (1000.0 / tex_height);
-        }
-        tl -> add( tmp2 );
-    } else if ( size > 1 ) {
-        for ( i = 0; i < size; ++i ) {
-            texcoord = texcoords[ tex_index[i] ];
-            sgSetVec2( tmp2, texcoord[0], texcoord[1] );
-            if ( tex_width > 0 ) {
-                tmp2[0] *= (1000.0 / tex_width);
-            }
-            if ( tex_height > 0 ) {
-                tmp2[1] *= (1000.0 / tex_height);
-            }
-            tl -> add( tmp2 );
-        }
-    }
-
-    ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
-
-    // lookup the state record
-
-    leaf->setState( state );
-
-    if ( calc_lights ) {
-        if ( coverage > 0.0 ) {
-            if ( coverage < 10000.0 ) {
-                SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
-                       << coverage << ", pushing up to 10000");
-                coverage = 10000;
-            }
-            gen_random_surface_points(leaf, lights, coverage);
-        }
-    }
-
-    return leaf;
-}
-
-
 // Load an Binary obj file
 bool fgBinObjLoad( const string& path, const bool is_base,
                    Point3D *center,
                    double *bounding_radius,
+                   SGMaterialLib *matlib,
                    ssgBranch* geometry,
                    ssgBranch* rwy_lights,
                    ssgBranch* taxi_lights,
@@ -920,7 +725,7 @@ bool fgBinObjLoad( const string& path, const bool is_base,
             // returns a transform -> lod -> leaf structure
             ssgBranch *branch = gen_directional_lights( nodes, normals,
                                                         pts_v[i], pts_n[i],
-                                                        pt_materials[i],
+                                                        matlib, pt_materials[i],
                                                         up );
             // branches don't honor callbacks as far as I know so I'm
             // commenting this out to avoid a plib runtime warning.
@@ -934,10 +739,10 @@ bool fgBinObjLoad( const string& path, const bool is_base,
         } else {
             material = pt_materials[i];
             tex_index.clear();
-            ssgLeaf *leaf = gen_leaf( path, GL_POINTS, material,
-                                      nodes, normals, texcoords,
-                                      pts_v[i], pts_n[i], tex_index,
-                                      false, ground_lights );
+            ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
+                                        nodes, normals, texcoords,
+                                        pts_v[i], pts_n[i], tex_index,
+                                        false, ground_lights );
             geometry->addKid( leaf );
         }
     }
@@ -961,14 +766,22 @@ bool fgBinObjLoad( const string& path, const bool is_base,
     group_list const& tris_n = obj.get_tris_n();
     group_list const& tris_tc = obj.get_tris_tc();
     for ( i = 0; i < tris_v.size(); ++i ) {
-        ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, tri_materials[i],
-                                  nodes, normals, texcoords,
-                                  tris_v[i], tris_n[i], tris_tc[i],
-                                  is_base, ground_lights );
-
-        if (use_random_objects)
-          gen_random_surface_objects(leaf, random_object_branch,
-                                     center, tri_materials[i]);
+        ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
+                                    tri_materials[i],
+                                    nodes, normals, texcoords,
+                                    tris_v[i], tris_n[i], tris_tc[i],
+                                    is_base, ground_lights );
+
+        if ( use_random_objects ) {
+            SGMaterial *mat = matlib->find( tri_materials[i] );
+            if ( mat == NULL ) {
+                SG_LOG( SG_INPUT, SG_ALERT,
+                        "Unknown material for random surface objects = "
+                        << tri_materials[i] );
+            }
+            gen_random_surface_objects( leaf, random_object_branch,
+                                        center, mat );
+        }
         geometry->addKid( leaf );
     }
 
@@ -978,14 +791,22 @@ bool fgBinObjLoad( const string& path, const bool is_base,
     group_list const& strips_n = obj.get_strips_n();
     group_list const& strips_tc = obj.get_strips_tc();
     for ( i = 0; i < strips_v.size(); ++i ) {
-        ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, strip_materials[i],
-                                  nodes, normals, texcoords,
-                                  strips_v[i], strips_n[i], strips_tc[i],
-                                  is_base, ground_lights );
-
-        if (use_random_objects)
-          gen_random_surface_objects(leaf, random_object_branch,
-                                     center,strip_materials[i]);
+        ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
+                                    matlib, strip_materials[i],
+                                    nodes, normals, texcoords,
+                                    strips_v[i], strips_n[i], strips_tc[i],
+                                    is_base, ground_lights );
+
+        if ( use_random_objects ) {
+            SGMaterial *mat = matlib->find( strip_materials[i] );
+            if ( mat == NULL ) {
+                SG_LOG( SG_INPUT, SG_ALERT,
+                        "Unknown material for random surface objects = "
+                        << strip_materials[i] );
+            }
+            gen_random_surface_objects( leaf, random_object_branch,
+                                        center, mat );
+        }
         geometry->addKid( leaf );
     }
 
@@ -995,13 +816,21 @@ bool fgBinObjLoad( const string& path, const bool is_base,
     group_list const& fans_n = obj.get_fans_n();
     group_list const& fans_tc = obj.get_fans_tc();
     for ( i = 0; i < fans_v.size(); ++i ) {
-        ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN, fan_materials[i],
-                                  nodes, normals, texcoords,
-                                  fans_v[i], fans_n[i], fans_tc[i],
-                                  is_base, ground_lights );
-        if (use_random_objects)
-          gen_random_surface_objects(leaf, random_object_branch,
-                                     center, fan_materials[i]);
+        ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
+                                    matlib, fan_materials[i],
+                                    nodes, normals, texcoords,
+                                    fans_v[i], fans_n[i], fans_tc[i],
+                                    is_base, ground_lights );
+        if ( use_random_objects ) {
+            SGMaterial *mat = matlib->find( fan_materials[i] );
+            if ( mat == NULL ) {
+                SG_LOG( SG_INPUT, SG_ALERT,
+                        "Unknown material for random surface objects = "
+                        << fan_materials[i] );
+            }
+            gen_random_surface_objects( leaf, random_object_branch,
+                                        center, mat );
+        }
         geometry->addKid( leaf );
     }