|
在别的帖子里看到过多的节点会使cull时间过长,请教个问题为了加载数据库的shp表,使其可编辑,不得不在遍历数据库(10w条记录)表时就需要10w多个geometry,这样导致cull的太大,请问用这种方式(很多geometry,为的是对单个个体后期可编译)加载数据绘制时怎样控制cull,谢谢~~
#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过长导致的吗?如果换成单个节点就没有如上的问题。
|
|