Beispiel #1
0
 virtual void apply(osg.Node node)
 {
     osg.StateSet ss = node.getStateSet();
     if (ss)
     {
         rewrite(ss);
     }
     osg.NodeVisitor.apply(node);
 }
Beispiel #2
0
        /*** Statics ********************************************************/

        static bool getTerrainData(Terrain terrain,
                                   out osg.Node out_terrain_node,
                                   out SpatialReference out_terrain_srs,
                                   out GeoExtent out_terrain_extent)
        {
            if (terrain != null)
            {
                if (!string.IsNullOrEmpty(terrain.getURI()))
                {
                    out_terrain_node = osgDB.readNodeFile(terrain.getAbsoluteURI());
                }

                // first check for an explicity defined SRS:
                out_terrain_srs = terrain.getExplicitSRS();
                if (out_terrain_srs != null && out_terrain_srs.isGeographic())
                {
                    // and make it geocentric if necessary..
                    out_terrain_srs = Registry.SRSFactory().createGeocentricSRS(out_terrain_srs.get());
                }

                if (out_terrain_node != null)
                {
                    // if the SRS wasn't explicit, try to read it from the scene graph:
                    if (out_terrain_srs == null)
                    {
                        out_terrain_srs = Registry.SRSFactory().createSRSfromTerrain(out_terrain_node.get());
                    }

                    //osgGIS.notice()
                    //    << "Loaded TERRAIN from \"" << terrain.getAbsoluteURI() << "\", SRS = "
                    //    << (out_terrain_srs != null? out_terrain_srs.getName() : "unknown")
                    //    << std.endl;
                }

                else if (!string.IsNullOrEmpty(terrain.getURI()))
                {
                    //osgGIS.warn()
                    //    << "Unable to load data for terrain \""
                    //    << terrain.getName() << "\"."
                    //    << std.endl;

                    return(false);
                }
            }

            out_terrain_extent = new GeoExtent(-180, -90, 180, 90,
                                               Registry.instance().getSRSFactory().createWGS84());

            return(true);
        }
Beispiel #3
0
        /**
         * Sets the reference terrain against which to compile.
         */
        //public  void setTerrain(
        //    osg.Node               terrain,
        //      SpatialReference  terrain_srs =NULL,
        //      GeoExtent         terrain_extent =GeoExtent.infinite() )
        public void setTerrain(osg.Node _terrain,
                               SpatialReference _terrain_srs,
                               GeoExtent _terrain_extent)
        {
            terrain_node   = _terrain;
            terrain_srs    = (SpatialReference)_terrain_srs;
            terrain_extent = _terrain_extent;

            if (terrain_srs == null)
            {
                terrain_srs = MogreGis.Registry.SRSFactory().createSRSfromTerrain(terrain_node.get());
            }

            //if ( !terrain_srs.valid() )
            //    osgGIS.warn() << "[MapLayerCompiler] WARNING: cannot determine SRS of terrain!" << std.endl;
        }
Beispiel #4
0
        public virtual void runSynchronousPostProcess(Report report)
        {
            if (need_to_compile)
            {
                if (!getResult().isOK())
                {
                    // osgGIS.notice() << getName() << " failed to compile: " << getResult().getMessage() << std.endl;
                    return;
                }

                if (output_status == CellCompiler.OutputStatus.OUTPUT_EMPTY) //!getResultNode() || !has_drawables )
                {
                    // osgGIS.info() << getName() << " resulted in no geometry" << std.endl;
                    result_node = null;
                    return;
                }

                if (packager.valid())
                {
                    // TODO: we should probably combine the following two calls into one:

                    // update any texture/model refs in preparation for packaging:
                    packager.rewriteResourceReferences(getResultNode());

                    // copy resources to their final destination
                    packager.packageResources(env.getResourceCache(), report);

                    // write the node data itself
                    osg.Node node_to_package = getResultNode();

                    if (!packager.packageNode(node_to_package.get(), abs_output_uri)) //, env.getCellExtent(), min_range, max_range ) )
                    {
                        //osgGIS.warn() << getName() << " failed to package node to output location" << std.endl;
                        result = FilterGraphResult.error("Cell built OK, but failed to deploy to disk/archive");
                    }
                }
            }
        }
        /**
         * Sets the reference terrain against which to compile.
         */
        //public  void setTerrain(
        //    osg.Node               terrain,
        //      SpatialReference  terrain_srs =NULL,
        //      GeoExtent         terrain_extent =GeoExtent.infinite() )
        public void setTerrain(osg.Node _terrain,
            SpatialReference _terrain_srs,
            GeoExtent _terrain_extent)
        {
            terrain_node = _terrain;
            terrain_srs = (SpatialReference)_terrain_srs;
            terrain_extent = _terrain_extent;

            if (terrain_srs == null)
                terrain_srs = MogreGis.Registry.SRSFactory().createSRSfromTerrain(terrain_node.get());

            //if ( !terrain_srs.valid() )
            //    osgGIS.warn() << "[MapLayerCompiler] WARNING: cannot determine SRS of terrain!" << std.endl;
        }
Beispiel #6
0
        void LayerCompiler.localizeResources(string output_folder)
        {
            // collect the resources marked as used.
            ResourceList resources_to_localize = getSession().getResourcesUsed(true);


            osgDB.ReaderWriter.Options local_options = new osgDB.ReaderWriter.Options();

            foreach (Resource i in resources_to_localize)
            {
                Resource resource = i;

                // if we are localizing resources, attempt to copy each one to the output folder:
                if (getLocalizeResources())
                {
                    if (resource is SkinResource)
                    {
                        SkinResource skin = (SkinResource)resource;

                        /******/
                        Image image = null; //skin.getImage();
                        if (image != null)
                        {
                            string filename = osgDB.getSimpleFileName(image.getFileName());

                            Image output_image = image.get();
                            if (getCompressTextures())
                            {
                                output_image = ImageUtils.convertRGBAtoDDS(image.get());
                                filename     = osgDB.getNameLessExtension(filename) + ".dds";
                                output_image.setFileName(filename);
                            }

                            if (getArchive() && !getArchive().fileExists(filename))
                            {
                                osgDB.ReaderWriter.WriteResult r = getArchive().writeImage(*(output_image.get()), filename, local_options.get());
                                if (r.error())
                                {
                                    //TODO osgGIS.notify( osg.WARN ) << "  Failure to copy image " << filename << " into the archive" << std.endl;
                                }
                            }
                            else
                            {
                                if (osgDB.fileExists(output_folder))
                                {
                                    if (!osgDB.writeImageFile(*(output_image.get()), PathUtils.combinePaths(output_folder, filename), local_options.get()))
                                    {
                                        //TODO  osgGIS.notify( osg.WARN ) << "  FAILED to copy image " << filename << " into the folder " << output_folder << std.endl;
                                    }
                                }
                                else
                                {
                                    //TODO osgGIS.notify( osg.WARN ) << "  FAILD to localize image " << filename << ", folder " << output_folder << " not found" << std.endl;
                                }
                            }
                        }
                    }

                    else if (resource is ModelResource)
                    {
                        ModelResource model = (ModelResource)resource;

                        osg.Node node = osgDB.readNodeFile(model.getAbsoluteURI());
                        if (node.valid())
                        {
                            string filename = osgDB.getSimpleFileName(model.getAbsoluteURI());
                            if (getArchive() != null)
                            {
                                osgDB.ReaderWriter.WriteResult r = getArchive().writeNode(*(node.get()), filename, local_options.get());
                                if (r.error())
                                {
                                    //TODO osgGIS.notify( osg.WARN ) << "  Failure to copy model " << filename << " into the archive" << std.endl;
                                }
                            }
                            else
                            {
                                if (osgDB.fileExists(output_folder))
                                {
                                    if (!osgDB.writeNodeFile(*(node.get()), osgDB.concatPaths(output_folder, filename), local_options.get()))
                                    {
                                        //TODO osgGIS.notify( osg.WARN ) << "  FAILED to copy model " << filename << " into the folder " << output_folder << std.endl;
                                    }
                                }
                                else
                                {
                                    //TODO osgGIS.notify( osg.WARN ) << "  FAILD to localize model " << filename << ", folder " << output_folder << " not found" << std.endl;
                                }
                            }
                        }
                    }
                }

                // now remove any single-use (i.e. non-shared) resources (whether we are localizing them or not)
                if (resource.isSingleUse())
                {
                    getSession().getResources().removeResource(resource);
                }
            }
        }
Beispiel #7
0
        protected void setCenterAndRadius(osg.Node node, GeoExtent cell_extent, SmartReadCallback reader)
        {
            SpatialReference srs = map_layer.getOutputSRS(getSession(), getTerrainSRS());
            // first get the output srs centroid:
            GeoPoint centroid = srs.transform(cell_extent.getCentroid());
            GeoPoint sw       = srs.transform(cell_extent.getSouthwest());

            double radius = map_layer.getEncodeCellRadius() ?
                            (centroid - sw).length() :
                            -1.0;

            if (terrain_node.valid() && terrain_srs != null)
            {
                GeoPoint clamped;
                for (int t = 0; t < 5; t++)
                {
                    clamped = GeomUtils.clampToTerrain(centroid, terrain_node.get(), terrain_srs, reader);
                    if (!clamped.isValid())
                    {
                        // if the clamp failed, it's due to the geocentric intersection bug in which the isect
                        // fails when coplanar with a tile boundary/skirt. Fudge the centroid and try again.
                        double fudge = 0.001 * ((double)(1 + (Random.Rand() % 10)));
                        centroid.X += fudge;
                        centroid.Y -= fudge;
                        centroid.Z += fudge * fudge;
                    }
                    else
                    {
                        break;
                    }
                }

                if (!clamped.isValid())
                {
                    SpatialReference geo    = srs.getGeographicSRS();
                    GeoPoint         latlon = geo.transform(centroid);
                    //osgGIS.warn() << "*** UNABLE TO CLAMP CENTROID: ***" << latlon.toString() << std.endl;
                }
                else
                {
                    centroid = clamped;
                }
            }
            else
            {
                //osgGIS.warn() << "*** Failed to clamp Center/Radius for cell" << std.endl;
            }

            if (node is osg.LOD)
            {
                osg.LOD plod = (osg.LOD)node;
                plod.setCenter(centroid);
                plod.setRadius(radius);
            }
            else if (node is osg.ProxyNode)
            {
                osg.ProxyNode proxy = (osg.ProxyNode)node;
                proxy.setCenter(centroid);
                proxy.setRadius(radius);
            }
        }
Beispiel #8
0
 public bool packageNode(osg.Node node, string abs_uri)
 {
     throw new NotImplementedException();
 }
Beispiel #9
0
 public void rewriteResourceReferences(osg.Node node)
 {
     throw new NotImplementedException();
 }
Beispiel #10
0
        protected virtual AttributedNodeList process(AttributedNodeList input, FilterEnv env)
        {
            Node result;

            if (input.Count > 1)
            {
                result = new osg.Group();
                for (AttributedNodeList.iterator i = input.begin(); i != input.end(); i++)
                {
                    osg.Node node = i.get().getNode();
                    if (node != null)
                    {
                        if (getEmbedAttributes())
                        {
                            embedAttributes(node, i.get().getAttributes());
                        }

                        result.asGroup().addChild(node);
                    }
                }
            }
            else if (input.Count == 1)
            {
                result = input[0].getNode();

                if (getEmbedAttributes())
                {
                    embedAttributes(result.get(), input[0].getAttributes());
                }
            }
            else
            {
                return(new AttributedNodeList());
            }

            // if there are no drawables or external refs, toss it.
            if (!GeomUtils.hasDrawables(result.get()))
            {
                return(AttributedNodeList());
            }

            // NEXT create a XFORM if there's a localization matrix in the SRS. This will
            // prevent jittering due to loss of precision.
            SpatialReference input_srs = env.getInputSRS();

            if (env.getExtent().getArea() > 0 && !input_srs.getReferenceFrame().isIdentity())
            {
                Vector3D            centroid     = new Vector3D(0, 0, 0);
                osg.Matrixd         irf          = input_srs.getInverseReferenceFrame();
                osg.Vec3d           centroid_abs = centroid * irf;
                osg.MatrixTransform xform        = new osg.MatrixTransform(irf);

                xform.addChild(result);
                result = xform;

                if (getApplyClusterCulling() && input_srs.isGeocentric())
                {
                    Vector3D normal = centroid_abs;
                    normal.normalize();

                    //osg.BoundingSphere bs = result.computeBound(); // force it
                    // radius = distance from centroid inside which to disable CC altogether:
                    //float radius = bs.radius();
                    //osg.Vec3d control_point = bs.center();

                    Vector3D control_point = centroid_abs;
                    GeoPoint env_cen       = input_srs.transform(env.getCellExtent().getCentroid());
                    GeoPoint env_sw        = input_srs.transform(env.getCellExtent().getSouthwest());
                    float    radius        = (env_cen - env_sw).length();

                    // dot product: 0 = orthogonal to normal, -1 = equal to normal
                    float deviation = -radius / input_srs.getEllipsoid().getSemiMinorAxis();

                    osg.ClusterCullingCallback ccc = new osg.ClusterCullingCallback();
                    ccc.set(control_point, normal, deviation, radius);

                    osg.Group cull_group = new osg.Group();
                    cull_group.setCullCallback(ccc);
                    cull_group.addChild(xform);
                    result = cull_group;


                    //osgGIS.notify(osg.NOTICE) << "CCC: radius = " << radius << ", deviation = " << deviation << std.endl;


                    //if ( getDrawClusterCullingNormals() == true )
                    //{
                    //    //DRAW CLUSTER-CULLING NORMALS
                    //    osg.Geode* geode = new osg.Geode();
                    //    osg.Geometry* g = new osg.Geometry();
                    //    osg.Vec3Array* v = new osg.Vec3Array(2);
                    //    (*v)[0] = control_point; (*v)[1] = control_point + (normal*radius);
                    //    g.setVertexArray( v );
                    //    osg.Vec4Array* c = new osg.Vec4Array(1);
                    //    (*c)[0] = osg.Vec4f( 0,1,0,1 );
                    //    g.setColorArray( c );
                    //    g.setColorBinding( osg.Geometry.BIND_OVERALL );
                    //    g.addPrimitiveSet( new osg.DrawArrays( osg.PrimitiveSet.LINES, 0, 2 ) );
                    //    geode.addDrawable( g );
                    //    cull_group.addChild( geode );
                    //}
                }
            }

            if (getCullBackfaces())
            {
                result.getOrCreateStateSet().setAttributeAndModes(new osg.CullFace(), osg.StateAttribute.ON);
            }

            if (getDisableLighting())
            {
                result.getOrCreateStateSet().setMode(GL_LIGHTING, osg.StateAttribute.OFF);
            }

            if (getLineWidth() > 0.0f)
            {
                result.getOrCreateStateSet().setAttribute(new osg.LineWidth(line_width), osg.StateAttribute.ON);
            }

            if (getPointSize() > 0.0f)
            {
                osg.Point point = new osg.Point();
                point.setSize(point_size);
                result.getOrCreateStateSet().setAttribute(point, osg.StateAttribute.ON);
            }

            if (getAlphaBlending())
            {
                osg.BlendFunc blend_func = new osg.BlendFunc();
                //blend_func.setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
                result.getOrCreateStateSet().setAttributeAndModes(blend_func, osg.StateAttribute.ON);
                result.getOrCreateStateSet().setRenderingHint(osg.StateSet.TRANSPARENT_BIN);
            }

            if (getRasterOverlayScript())
            {
                ScriptResult r = env.getScriptEngine().run(getRasterOverlayScript(), env);
                if (r.isValid())
                {
                    RasterResource *raster = env.getSession().getResources().getRaster(r.asString());
                    if (raster)
                    {
                        osg.Image *image = NULL;

                        std.stringstream builder;

                        string cell_id = env.getProperties().getValue("compiler.cell_id", "");
                        if (cell_id.length() > 0)
                        {
                            builder << "r" << cell_id << ".jpg";
                        }
                        else
                        {
                            double x = env.getExtent().getCentroid().x();
                            double y = env.getExtent().getCentroid().y();
                            builder << std.setprecision(10) << "r" << x << "x" << y << ".jpg";
                        }

                        if (raster.applyToStateSet(result.getOrCreateStateSet(), env.getExtent(), getRasterOverlayMaxSize(), &image))
                        {
                            // Add this as a skin resource so the compiler can properly localize and deploy it.
                            image.setFileName(builder.str());

                            env.getResourceCache().addSkin(result.getOrCreateStateSet());
                        }
                    }
                }
                else
                {
                    env.getReport().error(r.asString());
                }
            }

            if (getOptimize())
            {
                //osgGIS.notice() << "[BuildNodes] Optimizing..." << std.endl;

                osgUtil.Optimizer opt;
                int opt_mask =
                    osgUtil.Optimizer.DEFAULT_OPTIMIZATIONS |
                    osgUtil.Optimizer.MERGE_GEODES |
                    osgUtil.Optimizer.TRISTRIP_GEOMETRY |
                    osgUtil.Optimizer.SPATIALIZE_GROUPS;

                // disable texture atlases, since they mess with our shared skin resources and
                // don't work correctly during multi-threaded building
                opt_mask &= ~osgUtil.Optimizer.TEXTURE_ATLAS_BUILDER;

                // I've seen this crash the app when dealing with certain ProxyNodes.
                // TODO: investigate this later.
                opt_mask &= ~osgUtil.Optimizer.REMOVE_REDUNDANT_NODES;

                // integrate the optimizer hints:
                opt_mask |= env.getOptimizerHints().getIncludedOptions();
                opt_mask &= ~(env.getOptimizerHints().getExcludedOptions());

                opt.optimize(result.get(), opt_mask);

                GeometryCleaner cleaner;
                cleaner.clean(result.get());
            }

            AttributedNodeList output;

            output.push_back(new AttributedNode(result.get()));

            return(output);
        }