查看: 1195|回复: 1

cull时间太长问题

[复制链接]

该用户从未签到

发表于 2014-4-24 14:27:08 | 显示全部楼层 |阅读模式
在别的帖子里看到过多的节点会使cull时间过长,请教个问题为了加载数据库的shp表,使其可编辑,不得不在遍历数据库(10w条记录)表时就需要10w多个geometry,这样导致cull的太大,请问用这种方式(很多geometry,为的是对单个个体后期可编译)加载数据绘制时怎样控制cull,谢谢~~ 小Q截图-20140424140937.png

#pragma once
#include <database/p3d_soci.h>
#include <postgresql/soci-postgresql.h>
#ifdef NDEBUG

#   pragma comment(lib,"libsoci_postgresql_3_2.lib")
#else
#   pragma comment(lib,"libsoci_postgresql_3_2d.lib")
#endif
#pragma comment(lib,"gdal_i.lib")

#pragma comment(lib,"libpq.lib")
#include <boost/progress.hpp>


#include <vector>
#include <boost/assign.hpp>
using namespace boost::assign;
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Geometry>
#include <osgDB/WriteFile>

struct GeomWkt
{
        std::string geom;
};

namespace soci
{
        template<>
        struct type_conversion<GeomWkt>
        {
                typedef values base_type;
                static void from_base( values const & v, indicator /* ind */, GeomWkt & p )
                {
                        p.geom = v.get<std::string>( "geom" );
                }

                static void to_base( const GeomWkt & p, values & v, indicator & ind )
                {
                        v.set( "geom", p.geom );
                        ind = i_ok;
                }
        };
}
#include <ogrsf_frmts.h>

int main()
{
        osg::ref_ptr<osg::Group> root = new osg::Group;
        {
                boost::progress_timer t;
                soci::session sql( soci::postgresql, "dbname=jiaonan   user=postgres password=p3d" );
                soci::rowset<GeomWkt> rowset = ( sql.prepare << "SELECT  st_astext(geom)  geom FROM jn_resr ;" ); //select distinct x,y,z from temp
                {
                        for ( soci::rowset<GeomWkt>::const_iterator it = rowset.begin(); it != rowset.end(); ++it )
                        {
                                std::string wkt = it->geom;
                                char* pWkt = const_cast<char*>( wkt.c_str() );
                                OGRGeometry *pgeom = NULL;
                                OGRGeometryFactory::createFromWkt( &pWkt, NULL, &pgeom );
                                OGRwkbGeometryType geotype = pgeom->getGeometryType();
                                if( wkbMultiPolygon == geotype )
                                {
                                        OGRMultiPolygon *MultiPolygon = ( OGRMultiPolygon * )pgeom;
                                        for( int polygon_index = 0; polygon_index < MultiPolygon->getNumGeometries(); ++polygon_index )
                                        {
                                                osg::ref_ptr<osg::Geode> geode = new osg::Geode;
                                                osg::ref_ptr< osg::Vec3Array> point_array = new osg::Vec3Array;

                                                OGRPolygon *polygon = dynamic_cast<OGRPolygon *>( MultiPolygon->getGeometryRef( polygon_index ) );
                                                OGRLinearRing *ring = polygon->getExteriorRing();
                                                //std::cout << " 闭合 " << ring->get_IsClosed() << std::endl;
                                                for( int point_index = 0; point_index < ring->getNumPoints(); ++point_index )
                                                {
                                                        OGRPoint pnt;
                                                        ring->getPoint( point_index, &pnt );
                                                        point_array->push_back( osg::Vec3( pnt.getX() , 0.0, pnt.getY() ) );
                                                        //std::cout << " x " << pnt.getX() << " y "  << pnt.getY() << std::endl;
                                                        //if( pnt.getDimension() == 3 )        std::cout << " z " << pnt.getZ() << std::endl;
                                                }
                                                osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;

                                                osg::ref_ptr<osg::Vec3Array> normal = new osg::Vec3Array;
                                                normal->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
                                               
                                                geometry->setNormalArray(normal);
                                                geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);
                                                geometry->setVertexArray( point_array );
                                                geometry->addPrimitiveSet( new osg:rawArrays( osg:rimitiveSet:INE_LOOP , 0, point_array->size() ) );
                                                geode->addDrawable( geometry );
                                                root->addChild( geode );


                                        }
                                }
                        }
                }
        }

        //osgDB::writeNodeFile(*root , "h:\\ewrer.osg");
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
        viewer->addEventHandler( new osgGA::StateSetManipulator( viewer->getCamera()->getOrCreateStateSet() ) );
        viewer->addEventHandler(new osgViewer::StatsHandler);
        viewer->setSceneData( root.get() );
        return viewer->run();
        return 0;
}
还有程序运行时,会出现加载“吃力”白窗(描述不当),十几秒后才会看到场景,这种问题是cull过长导致的吗?如果换成单个节点就没有如上的问题。
小Q截图-20140424142007.png

该用户从未签到

发表于 2014-5-4 23:38:00 | 显示全部楼层
1、考虑合并一些几何体,更少的Geometry意味着更少的显示列表或者VBO,对效率的提升非常明显
2、考虑构建一些节点层次,例如四叉树,或者只是简单地按区域分层都好过在一个层级内平铺
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表