【OSG案例详细分析与讲解】之三:【3D场景优化与分页加载】
2024-01-10 08:41:13
一、【3D场景优化与分页加载】前言
随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
二、【3D场景优化与分页加载】简化场景和优化渲染
class StripStateVisitor : public osg::NodeVisitor
{
public:
StripStateVisitor(bool useStateSets, bool useDisplayLists, bool useVBO):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_useStateSets(useStateSets),
_useDisplayLists(useDisplayLists),
_useVBO(useVBO) {}
bool _useStateSets;
bool _useDisplayLists;
bool _useVBO;
void apply(osg::Node& node)
{
if (!_useStateSets && node.getStateSet()) node.setStateSet(0);
traverse(node);
}
void apply(osg::Drawable& drawable)
{
if (!_useStateSets && drawable.getStateSet())
{
drawable.setStateSet(0);
}
drawable.setUseDisplayList(_useDisplayLists);
drawable.setUseVertexBufferObjects(_useVBO);
}
};
三、【3D场景优化与分页加载】场景纹理图像优化处理
1.代码
代码如下(示例):
class OptimizeImageVisitor : public osg::NodeVisitor
{
public:
OptimizeImageVisitor(osgDB::ImageProcessor* imageProcessor, bool compressImages, bool generateMipmaps):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_imageProcessor(imageProcessor),
_compressImages(compressImages),
_generateMipmaps(generateMipmaps) {}
osg::ref_ptr<osgDB::ImageProcessor> _imageProcessor;
bool _compressImages;
bool _generateMipmaps;
void apply(osg::Node& node)
{
processStateSet(node.getStateSet());
traverse(node);
}
void apply(osg::Geode& node)
{
processStateSet(node.getStateSet());
for(unsigned int i = 0; i<node.getNumDrawables(); ++i)
{
processStateSet(node.getDrawable(i)->getStateSet());
}
traverse(node);
}
void processStateSet(osg::StateSet* stateset)
{
if (!stateset) return;
for(unsigned int ti=0; ti<stateset->getNumTextureAttributeLists(); ++ti)
{
osg::StateAttribute* sa = stateset->getTextureAttribute(ti, osg::StateAttribute::TEXTURE);
osg::Texture* texture = dynamic_cast<osg::Texture*>(sa);
if (texture)
{
for(unsigned int i=0; i<texture->getNumImages(); ++i)
{
proccessImage(texture->getImage(i));
}
}
};
}
void proccessImage(osg::Image* image)
{
if (!image) return;
if (_imageProcessor.valid())
{
OSG_NOTICE<<"Will be using ImageProcessor to process image "<<image->getFileName()<<std::endl;
}
else
{
OSG_NOTICE<<"No ImageProcessor to process image "<<image->getFileName()<<std::endl;
}
OSG_NOTICE<<" compressImage "<<_compressImages<<std::endl;
OSG_NOTICE<<" generateMipmaps "<<_generateMipmaps<<std::endl;
}
};
2.成效
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。
四、【3D场景优化与分页加载】几何节点数组交换
class SwapArrayVisitor : public osg::ArrayVisitor
{
public:
SwapArrayVisitor(osg::Array* array):
_array(array) {}
template <class ARRAY>
void apply_imp(ARRAY& array)
{
if (array.getType()!=_array->getType())
{
OSG_NOTICE<<"Arrays incompatible"<<std::endl;
return;
}
OSG_NOTICE<<"Swapping Array"<<std::endl;
array.swap(*static_cast<ARRAY*>(_array));
}
virtual void apply(osg::ByteArray& ba) { apply_imp(ba); }
virtual void apply(osg::ShortArray& ba) { apply_imp(ba); }
virtual void apply(osg::IntArray& ba) { apply_imp(ba); }
virtual void apply(osg::UByteArray& ba) { apply_imp(ba); }
virtual void apply(osg::UShortArray& ba) { apply_imp(ba); }
virtual void apply(osg::UIntArray& ba) { apply_imp(ba); }
virtual void apply(osg::Vec4ubArray& ba) { apply_imp(ba); }
virtual void apply(osg::FloatArray& ba) { apply_imp(ba); }
virtual void apply(osg::Vec2Array& ba) { apply_imp(ba); }
virtual void apply(osg::Vec3Array& ba) { apply_imp(ba); }
virtual void apply(osg::Vec4Array& ba) { apply_imp(ba); }
osg::Array* _array;
};
五、【3D场景优化与分页加载】分析和修改场景图内存
class MemoryVisitor : public osg::NodeVisitor
{
public:
MemoryVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
void reset()
{
_nodes.clear();
_geometryMap.clear();
_arrayMap.clear();
_primitiveSetMap.clear();
}
void apply(osg::Node& node)
{
_nodes.insert(&node);
traverse(node);
}
void apply(osg::Geode& geode)
{
_nodes.insert(&geode);
for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
{
apply(&geode, geode.getDrawable(i));
}
}
void apply(osg::Geode* geode, osg::Drawable* drawable)
{
if (!drawable) return;
osg::Geometry* geometry = drawable->asGeometry();
if (geometry)
{
_geometryMap[geometry].insert(geode);
apply(geometry, geometry->getVertexArray());
apply(geometry, geometry->getNormalArray());
apply(geometry, geometry->getColorArray());
apply(geometry, geometry->getSecondaryColorArray());
apply(geometry, geometry->getFogCoordArray());
for(unsigned int i=0; i<geometry->getNumTexCoordArrays(); ++i)
{
apply(geometry, geometry->getTexCoordArray(i));
}
for(unsigned int i=0; i<geometry->getNumVertexAttribArrays(); ++i)
{
apply(geometry, geometry->getVertexAttribArray(i));
}
for(unsigned int i=0; i<geometry->getNumPrimitiveSets(); ++i)
{
apply(geometry, geometry->getPrimitiveSet(i));
}
}
}
void apply(osg::Geometry* geometry, osg::Array* array)
{
if (!array) return;
_arrayMap[array].insert(geometry);
}
void apply(osg::Geometry* geometry, osg::PrimitiveSet* primitiveSet)
{
if (!primitiveSet) return;
_primitiveSetMap[primitiveSet].insert(geometry);
}
void report(std::ostream& out)
{
out<<"Nodes "<<_nodes.size()<<std::endl;
out<<"Geometries "<<_geometryMap.size()<<std::endl;
out<<"Arrays "<<_arrayMap.size()<<std::endl;
out<<"PrimitiveSets "<<_primitiveSetMap.size()<<std::endl;
}
void reallocate()
{
OSG_NOTICE<<"Reallocating Arrays"<<std::endl;
typedef std::vector< osg::ref_ptr<osg::Geometry> > GeometryVector;
GeometryVector newGeometries;
for(GeometryMap::iterator itr = _geometryMap.begin();
itr != _geometryMap.end();
++itr)
{
osg::Geometry* geometry = itr->first;
bool useVBO = geometry->getUseVertexBufferObjects();
osg::Geometry* newGeometry = osg::clone(geometry, osg::CopyOp(osg::CopyOp::DEEP_COPY_ALL));
newGeometry->setUseVertexBufferObjects(false);
newGeometry->setUseVertexBufferObjects(useVBO);
newGeometries.push_back(newGeometry);
}
GeometryVector::iterator geom_itr = newGeometries.begin();
for(GeometryMap::iterator itr = _geometryMap.begin();
itr != _geometryMap.end();
++itr, ++geom_itr)
{
osg::Geometry* geometry = itr->first;
Geodes& geodes = itr->second;
for(Geodes::iterator gitr = geodes.begin();
gitr != geodes.end();
++gitr)
{
osg::Geode* geode = const_cast<osg::Geode*>(*gitr);
geode->replaceDrawable(geometry, geom_itr->get());
}
}
}
typedef std::vector< osg::ref_ptr<osg::Geometry> > GeometryVector;
typedef std::pair<osg::Array*, osg::Array*> ArrayPair;
typedef std::vector< ArrayPair > ArrayVector;
typedef std::pair<osg::PrimitiveSet*, osg::PrimitiveSet*> PrimitiveSetPair;
typedef std::vector< PrimitiveSetPair > PrimitiveSetVector;
osg::Array* cloneArray(ArrayVector& arrayVector, osg::Array* array)
{
if (!array) return 0;
osg::Array* newArray = static_cast<osg::Array*>(array->cloneType());
arrayVector.push_back(ArrayPair(array,newArray));
return newArray;
}
osg::PrimitiveSet* clonePrimitiveSet(PrimitiveSetVector& psVector, osg::PrimitiveSet* ps)
{
if (!ps) return 0;
osg::PrimitiveSet* newPS = static_cast<osg::PrimitiveSet*>(ps->cloneType());
psVector.push_back(PrimitiveSetPair(ps,newPS));
return newPS;
}
void reallocate2()
{
OSG_NOTICE<<"Reallocating Arrays"<<std::endl;
ArrayVector arrayVector;
PrimitiveSetVector primitiveSetVector;
GeometryVector newGeometries;
for(GeometryMap::iterator itr = _geometryMap.begin();
itr != _geometryMap.end();
++itr)
{
osg::Geometry* geometry = itr->first;
osg::ref_ptr<osg::Geometry> newGeometry = osg::clone(geometry, osg::CopyOp::SHALLOW_COPY);
newGeometries.push_back(newGeometry.get());
newGeometry->setVertexArray(cloneArray(arrayVector, geometry->getVertexArray()));
newGeometry->setNormalArray(cloneArray(arrayVector, geometry->getNormalArray()));
newGeometry->setColorArray(cloneArray(arrayVector, geometry->getColorArray()));
newGeometry->setSecondaryColorArray(cloneArray(arrayVector, geometry->getSecondaryColorArray()));
newGeometry->setFogCoordArray(cloneArray(arrayVector, geometry->getFogCoordArray()));
for(unsigned int i=0; i<geometry->getNumTexCoordArrays(); ++i)
{
newGeometry->setTexCoordArray(i, cloneArray(arrayVector, geometry->getTexCoordArray(i)));
}
for(unsigned int i=0; i<geometry->getNumVertexAttribArrays(); ++i)
{
newGeometry->setVertexAttribArray(i, cloneArray(arrayVector, geometry->getVertexAttribArray(i)));
}
for(unsigned int i=0; i<geometry->getNumPrimitiveSets(); ++i)
{
newGeometry->setPrimitiveSet(i,clonePrimitiveSet(primitiveSetVector, geometry->getPrimitiveSet(i)));
}
}
GeometryVector::iterator geom_itr = newGeometries.begin();
for(GeometryMap::iterator itr = _geometryMap.begin();
itr != _geometryMap.end();
++itr, ++geom_itr)
{
osg::Geometry* geometry = itr->first;
Geodes& geodes = itr->second;
for(Geodes::iterator gitr = geodes.begin();
gitr != geodes.end();
++gitr)
{
osg::Geode* geode = const_cast<osg::Geode*>(*gitr);
geode->replaceDrawable(geometry, geom_itr->get());
}
}
}
protected:
typedef std::set<osg::Node*> Nodes;
typedef std::set<osg::Geode*> Geodes;
typedef std::set<osg::Geometry*> Geometries;
typedef std::map<osg::Geometry*, Geodes> GeometryMap;
typedef std::map<osg::Array*, Geometries> ArrayMap;
typedef std::map<osg::PrimitiveSet*, Geometries> PrimitiveSetMap;
Nodes _nodes;
GeometryMap _geometryMap;
ArrayMap _arrayMap;
PrimitiveSetMap _primitiveSetMap;
};
六、【3D场景优化与分页加载】分页数据库操作
class DatabasePagingOperation : public osg::Operation, public osgUtil::IncrementalCompileOperation::CompileCompletedCallback
{
public:
DatabasePagingOperation(const std::string& filename,
const std::string& outputFilename,
SceneGraphProcessor* sceneGraphProcessor,
osgUtil::IncrementalCompileOperation* ico):
osg::Referenced(true),
Operation("DatabasePaging Operation", false),
_filename(filename),
_outputFilename(outputFilename),
_modelReadyToMerge(false),
_sceneGraphProcessor(sceneGraphProcessor),
_incrementalCompileOperation(ico)
{
}
virtual void operator () (osg::Object* /*object*/)
{
osg::notify(osg::NOTICE)<<"LoadAndCompileOperation "<<_filename<<std::endl;
_modelReadyToMerge = false;
_loadedModel = osgDB::readRefNodeFile(_filename);
if (_loadedModel.valid())
{
if (_sceneGraphProcessor.valid())
{
_loadedModel = _sceneGraphProcessor->process(_loadedModel.get());
}
}
if (_loadedModel.valid())
{
if (!_outputFilename.empty())
{
OSG_NOTICE<<"Writing out file "<<_outputFilename<<std::endl;
osgDB::writeNodeFile(*_loadedModel, _outputFilename);
}
if (_incrementalCompileOperation.valid())
{
OSG_NOTICE<<"Registering with ICO "<<_outputFilename<<std::endl;
osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet =
new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel.get());
compileSet->_compileCompletedCallback = this;
_incrementalCompileOperation->add(compileSet.get());
}
else
{
_modelReadyToMerge = true;
}
}
osg::notify(osg::NOTICE)<<"done LoadAndCompileOperation "<<_filename<<std::endl;
}
virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* /*compileSet*/)
{
OSG_NOTICE<<"compileCompleted"<<std::endl;
_modelReadyToMerge = true;
return true;
}
std::string _filename;
std::string _outputFilename;
osg::ref_ptr<osg::Node> _loadedModel;
bool _modelReadyToMerge;
osg::ref_ptr<SceneGraphProcessor> _sceneGraphProcessor;
osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
};
七、【3D场景优化与分页加载】总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
文章来源:https://blog.csdn.net/LuckyHanMo/article/details/135493786
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!