Ejemplo n.º 1
0
        protected void applyOverlayTexturing(osg.Geometry geom, Feature input, FilterEnv env)
        {
            GeoExtent tex_extent;

            if (getRasterOverlayScript() != null)
            {
                // if there's a raster script for this filter, we're applying textures per-feature:
                tex_extent = new GeoExtent(
                    input.getExtent().getSouthwest().getAbsolute(),
                    input.getExtent().getNortheast().getAbsolute());
            }
            else
            {
                // otherwise prepare the geometry for an overlay texture covering the entire working extent:
                tex_extent = env.getExtent();
            }

            float width  = (float)tex_extent.getWidth();
            float height = (float)tex_extent.getHeight();

            // now visit the verts and calculate texture coordinates for each one.
            osg.Vec3Array verts = (osg.Vec3Array)(geom.getVertexArray());
            if (verts != null)
            {
                // if we are dealing with geocentric data, we will need to xform back to a real
                // projection in order to determine texture coords:
                GeoExtent tex_extent_geo;
                if (env.getInputSRS().isGeocentric())
                {
                    tex_extent_geo = new GeoExtent(
                        tex_extent.getSRS().getGeographicSRS().transform(tex_extent.getSouthwest()),
                        tex_extent.getSRS().getGeographicSRS().transform(tex_extent.getNortheast()));
                }

                osg.Vec2Array texcoords = new osg.Vec2Array(verts.size());
                for (int j = 0; j < verts.size(); j++)
                {
                    // xform back to raw SRS w.o. ref frame:
                    GeoPoint vert = new GeoPoint(verts[j], env.getInputSRS());
                    GeoPoint vert_map = vert.getAbsolute();
                    float    tu, tv;
                    if (env.getInputSRS().isGeocentric())
                    {
                        tex_extent_geo.getSRS().transformInPlace(vert_map);
                        tu = (vert_map.X - tex_extent_geo.getXMin()) / width;
                        tv = (vert_map.Y - tex_extent_geo.getYMin()) / height;
                    }
                    else
                    {
                        tu = (vert_map.X - tex_extent.getXMin()) / width;
                        tv = (vert_map.Y - tex_extent.getYMin()) / height;
                    }
                    (*texcoords)[j].set(tu, tv);
                }
                geom.setTexCoordArray(0, texcoords);
            }

            // if we are applying the raster per-feature, do so now.
            // TODO: deprecate? will we ever use this versus the BuildNodesFilter overlay? maybe
            if (getRasterOverlayScript() != null)
            {
                ScriptResult r = env.getScriptEngine().run(getRasterOverlayScript(), input, env);
                if (r.isValid())
                {
                    RasterResource raster = env.getSession().getResources().getRaster(r.asString());
                    if (raster != null)
                    {
                        Image            image = null;
                        std.stringstream builder;
                        builder << "rtex_" << input.getOID() << ".jpg"; //TODO: dds with DXT1 compression

                        osg.StateSet raster_ss = new osg.StateSet();
                        if (raster.applyToStateSet(raster_ss.get(), tex_extent, getRasterOverlayMaxSize(), out image))
                        {
                            image.setFileName(builder.str());
                            geom.setStateSet(raster_ss.get());

                            // add this as a skin resource so the compiler can properly localize and deploy it.
                            env.getResourceCache().addSkin(raster_ss.get());
                        }
                    }
                }
                else
                {
                    env.getReport().error(r.asString());
                }
            }
        }
Ejemplo n.º 2
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);
        }