osg系列文章目录
前言
osg3.6.5 osgEarth3.2中,生成ive格式文件,使用osgViewer.exe命令打开ive文件时,却无法正常显示。
代码如下:
#include <Windows.h>
#include <iostream>
#include <string>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgEarth/MapNode>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarth/ImageLayer>
#include <osgEarth/EarthManipulator>
#include <osgEarth/OGRFeatureSource>
#include <osgEarth/FeatureModelLayer>
#include <osgEarth/FeatureImageLayer>
#include <osgEarth/ECEF>
#include <osgEarth/GeoData>
#include <osgEarth/Viewpoint>
#include <osgEarth/TerrainOptions>
#include <osgGA/SphericalManipulator>
#include <osgEarth/Symbol>
#include <osgEarth/Feature>
#include <osgEarth/FeatureModelSource>
#include <osgUtil/Tessellator>
#include <osg/LineWidth>
#include <osg/Depth>
#include <osg/PolygonMode>
using namespace std;
void LoadShape()
{
srand(static_cast<unsigned int>(time(nullptr)));
//// Load the base map (globe)
//osg::ref_ptr<osg::Node> globe = osgDB::readNodeFile("../vs2022_64bit_3rdParty_osg365_oe32/runtime/test/earthFile/china-simple.earth");
//if (!globe)
//{
// std::cerr << "Failed to load earth file." << std::endl;
// return;
//}
/*osg::ref_ptr<osg::Group> gp = new osg::Group;
gp->addChild(globe);
osg::ref_ptr<osgEarth::MapNode> mapNode = osgEarth::MapNode::get(globe);
if (!mapNode)
{
std::cerr << "Failed to get MapNode." << std::endl;
return;
}
osg::ref_ptr<osg::Geode> gNode = new osg::Geode;
gp->addChild(gNode);
osg::ref_ptr<osgEarth::Map> map = mapNode->getMap();*/
// Load the Shapefile
osg::ref_ptr<osgEarth::OGRFeatureSource> features = new osgEarth::OGRFeatureSource;
features->setURL("F:/osg/yangShiXing/019.Earth/builder/data/shpFile/world.shp"); // china.shp world.shp
if (!features->open())
{
std::cerr << "Failed to load shapefile: " << features->getStatus().message() << std::endl;
return;
}
else
{
std::cout << "Shapefile loaded successfully." << std::endl;
}
// Initialize the query object
osgEarth::Query query;
//query.expression() = "1=1"; // A simple query that matches all features
// Log the query expression if it exists
if (query.expression().isSet())
{
std::cout << "Query expression: " << query.expression().value() << std::endl;
}
// Create a feature cursor with the query
osg::ref_ptr<osgEarth::FeatureCursor> cursor = features->createFeatureCursor(query, nullptr);
if (!cursor)
{
std::cerr << "Failed to create feature cursor." << std::endl;
return;
}
else
{
std::cout << "Feature cursor created successfully." << std::endl;
}
const osgEarth::SpatialReference* srs = features->getFeatureProfile()->getSRS();
const osgEarth::SpatialReference* mapSRS = features->getFeatureProfile()->getSRS();
//osg::ref_ptr<osg::Geode> gNode = new osg::Geode;
int colorIndex = 0;
osg::Vec4 color1 = osg::Vec4(1.0, 0.0, 0.0, 0.2);
osg::Vec4 color2 = osg::Vec4(1.0, 1.0, 0.0, 0.2);
osg::Vec4 color3 = osg::Vec4(1.0, 0.0, 1.0, 0.2);
osg::Vec4 color4 = osg::Vec4(0.0, 1.0, 0.0, 0.2);
osg::Vec4 color5 = osg::Vec4(0.0, 0.0, 1.0, 0.2);
osg::Vec4 color6 = osg::Vec4(1.0, 1.0, 1.0, 0.2);
osg::Vec4 color7 = osg::Vec4(0.5, 0.2, 0.1, 0.2);
osg::ref_ptr<osg::Group> gp = new osg::Group;
while (cursor