osg--提高效率

本文介绍了如何使用OpenThreads进行多线程管理和渲染,包括线程的启动、状态检查以及多线程渲染的设置。同时,探讨了OpenSceneGraph(OSG)中的剔除技术,如背面剔除、视锥剔除和遮挡剔除,以及纹理共享的实现。此外,还提到了osgUtil::Optimizer优化工具和动态加载四叉树技术,如osg::HeightField和osg::PagedLOD的应用。
摘要由CSDN通过智能技术生成

多线程

  • OpenThreads::Thread
    • 虚函数
      • cancel()
      • run()
  • OpenThreads::Mutex
  • OpenThreads::Barrier
  • OpenThreads::Condition

线程管理

  • GetNumberOfProcessors() 处理器数目获取
  • SetProcessorAffinityOfCurrentThread() 当前线程使用处理器设置
  • CurrentThread()
  • YieldCurrentThread() 让出处理器给其它线程
  • microSleep() 当前线程休息毫秒
  • isRunning() 线程运行状态
  • startThread()开启线程

多线程渲染

注意:update过程不参与多线程渲染

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • setThreadingModel() 设置渲染模式
    • AutomaticSelection
    • SingleThreaded
    • ThreadPerContext
    • ThreadPerCamera

示例–多线程数据接收

#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <iostream>
#include <io.h>
class DataReceiverThread : public OpenThreads::Thread
{
public:
	static DataReceiverThread* instance()
	{
		static DataReceiverThread s_thread;
		return &s_thread;
	}


	virtual int cancel();
	virtual void run();

	void addToContent(int ch);
	bool getContent(std::string& str);


protected:
	OpenThreads::Mutex _mutex;
	std::string _content;
	bool _done;
	bool _dirty;
};
int DataReceiverThread::cancel()
{
	_done = true;
	while (isRunning()) 
		YieldCurrentThread();
	return 0;
}
void DataReceiverThread::run()
{
	_done = false;
	_dirty = true;
	do
	{
		YieldCurrentThread();
		char ch = 0;
		std::cin.get(ch);
		switch (ch)
		{
		case 0: 
			break;  // We don't want '\0' to be added
		case 9: 
			_done = true; 
			break;  // ASCII code of Tab = 9
		default: 
			addToContent(ch); 
			break;
		}
	} while (!_done);
}
void DataReceiverThread::addToContent(int ch)
{
	OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
	_content += ch;
	_dirty = true;
}
bool DataReceiverThread::getContent(std::string& str)
{
	OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
	if (_dirty)
	{
		str += _content;
		_dirty = false;
		return true;
	}
	return false;
}
class UpdateTextCallback : public osg::Drawable::UpdateCallback
{
public:
	virtual void update(osg::NodeVisitor* nv,osg::Drawable* drawable)
	{
		osgText::Text* text = static_cast<osgText::Text*>(drawable);
		if (text)
		{
			std::string str("# ");
			if (DataReceiverThread::instance()->getContent(str))
				text->setText(str);
		}
	}
};
int main(int argc, char** argv)
{
	osg::ref_ptr<osgText::Text> text = new osgText::Text;
	text->setFont("fonts/arial.ttf");
	text->setAxisAlignment(osgText::TextBase::SCREEN);
	text->setDataVariance(osg::Object::DYNAMIC);
	text->setInitialBound(osg::BoundingBox(osg::Vec3(), osg::Vec3(400.0f, 20.0f, 20.0f)));
	text->setUpdateCallback(new UpdateTextCallback);

	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	geode->addDrawable(text.get());
	ge
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值