OSG-OSG中的observer_ptr指针
看array大神的CookBook后一些感想,在代码上添加了一些注释,也对源码做了一些研读,记录下学习的过程。
CookBook中第一个例子就是observer_ptr指针,这个指针和它的名字一样,就是用来观察指针的,可以把它想象成一个观察者,它只观察一个你给他指点的对象,但是不会影响这个对象的创建和销毁,它只是一个旁观者,但是它观察的这个对象要是被销毁了它也会知道并且自己也会被销毁。和osg::ref_ptr不同,osg::ref_ptr是用来管理指针的创建和删除的。如下图:
下面是代码,代码中添加了注释,因为CookBook中的例子很多,所以定义了一个common.h和common.cpp,记录一些通用型的类和函数。common.h:
#pragma once
#ifdef _DEBUG #pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgUtild.lib")
#pragma comment(lib,"osgTextd.lib") #else #pragma comment(lib,"osg.lib");
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgUtil.lib")
#pragma comment(lib,"osgText.lib") #endif #include <osg/Camera>
#include <osg/Geode>
#include <osg/Node>
#include <osg/ShapeDrawable> #include <osgText/Text>
#include <osgUtil/LineSegmentIntersector> #include <osgGA/GUIActionAdapter>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIEventHandler> #include <osgViewer/Viewer> namespace osgCookBook{ extern osg::Camera* createHUDCamera(double left,double right,double bottom,double top); extern osgText::Text* createText(const osg::Vec3 &pos,const std::string &content,float size); class PickHandler : public osgGA::GUIEventHandler
{
public:
/** .h中的成往事 */
virtual void doUserOperation(osgUtil::LineSegmentIntersector::Intersection& )=0;
virtual bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter & aa);
};
}
common.cpp:
#include "common.h"
namespace osgCookBook{
/** 创建HUD相机 */
osg::Camera* createHUDCamera(double left,double right,double bottom,double top){
osg::ref_ptr<osg::Camera> camera=new osg::Camera;
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setAllowEventFocus(false);
camera->setProjectionMatrix(osg::Matrix::ortho2D(left,right,bottom,top));
camera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
return camera.release();
}
osg::ref_ptr<osgText::Font> g_font=osgText::readFontFile("fonts/arial.ttf");
/** 创建文字节点 */
osgText::Text* createText(const osg::Vec3 &pos,const std::string &content,float size){
osg::ref_ptr<osgText::Text> text=new osgText::Text;
text->setDataVariance(osg::Object::DYNAMIC);
text->setFont(g_font.get());
text->setCharacterSize(size);
text->setAxisAlignment(osgText::TextBase::XY_PLANE);
text->setText(content);
return text.release();
}
/** 实现处理器中的handle方法*/
bool PickHandler::handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter & aa){
if (ea.getEventType()!=osgGA::GUIEventAdapter::RELEASE
||ea.getButton()!=osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON
||!(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL))
return false;
osgViewer::View *viewer=dynamic_cast<osgViewer::View *>(&aa);
if (viewer)
{
//定义射线求交类,它的定义方式有3种
//在窗口坐标系osgUtil::Intersector::WINDOW中会创建一个一个起点(x,y,0)到终点(x,y,1)的线段
//在投影坐标系osgUtil::Intersector::PROJECTION中会创建一个一个起点(x,y,-1)到终点(x,y,1)的线段
//在模型视图坐标系osgUtil::Intersector::VIEW或osgUtil::Intersector::MODEL中会创建一个一个起点(x,y,0)到终点(x,y,1)的线段
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector=new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW,ea.getX(),ea.getY());
//创建求交访问器
osgUtil::IntersectionVisitor iv(intersector.get());
viewer->getCamera()->accept(iv);
//判断射线求交是否有交点
if (intersector->containsIntersections())
{
//射线求交的交点是经过深度排序的
//intersector->getIntersections()的返回结果是Intersections
//这是Intersections的定义:typedef std::multiset<Intersection> Intersections
//就是说Intersections是一个multiset,multiset是一个有序的容器,里面的元素都是经过排序的
//它排序的标准是里面元素的<操作符的定义,在这里就是Intersection中<操作符的定义
//而在Intersection类中有如下操作符重载: bool operator < (const Intersection& rhs) const { return ratio < rhs.ratio; }
//也就是说ratio是反应交点与射线起点距离的一个成员变量,array也说了ratio是交点与线段起点的距离和线段总长度的比例
osgUtil::LineSegmentIntersector::Intersection &result=*(intersector->getIntersections().begin());
doUserOperation(result);
}
}
return false;
}
}
然后就是observer_ptr指针的例子:observe_ptr.cpp
#include "../common/common.cpp" /**
* 事件处理器类
继承自PickHandler,只需要重写用户自定义方法doUserOperation即可
*/
class RemoveShapeHandle : public osgCookBook::PickHandler
{ virtual void doUserOperation(osgUtil::LineSegmentIntersector::Intersection& result){ //射线求交得到的结果
if (result.nodePath.size()>0)
{
//得到该路径中最后一个节点
osg::Geode* geode=dynamic_cast<osg::Geode *>(result.nodePath.back());
if (geode)
geode->removeDrawable(result.drawable);
}
} int test;
}; /**
* 节点回调类,每一帧都会执行
*/
class ObserveShapeCallback : public osg::NodeCallback{
public: virtual void operator()(osg::Node * node,osg::NodeVisitor *nv){
std::string content;
if(_drawable1.valid()) content+="Drawable 1 ;";
if(_drawable2.valid()) content+="Drawable 2 ;";
if(_text.valid()) _text->setText(content);
} osg::ref_ptr<osgText::Text> _text;
osg::observer_ptr<osg::Drawable> _drawable1; //这里如果用ref_ptr那么当这个Drawable被移除后,文字依然存在
osg::observer_ptr<osg::Drawable> _drawable2;
}; int main()
{
osgText::Text * text=osgCookBook::createText(osg::Vec3(70.0,70.0,0.0f),"",10.0f);
osg::ref_ptr<osg::Geode> textGeode=new osg::Geode;
textGeode->addDrawable(text); osg::ref_ptr<osg::Camera> hudCamera=osgCookBook::createHUDCamera(0,800,0,600);
hudCamera->addChild(textGeode); osg::ref_ptr<osg::Geode> geode=new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(-2.0f,0.0,0.0),1.0)));
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(2.0,0.0,0.0),1.0))); osg::ref_ptr<osg::Group> root=new osg::Group;
root->addChild(hudCamera);
root->addChild(geode); osg::ref_ptr<ObserveShapeCallback> ob=new ObserveShapeCallback;
ob->_text=text;
ob->_drawable1=geode->getDrawable(0);
ob->_drawable2=geode->getDrawable(1);
root->addUpdateCallback(ob); osgViewer::Viewer viewer;
viewer.addEventHandler(new RemoveShapeHandle);
viewer.setSceneData(root);
return viewer.run(); }
OSG-OSG中的observer_ptr指针的更多相关文章
- OSG中的智能指针
在OpenSceneGraph中,智能指针(Smart pointer)的概念指的是一种类的模板,它针对某一特定类型的对象(即Referenced类及其派生类)构建,提供了自己的管理模式,以避免因为用 ...
- [osg]OSG使用更新回调来更改模型
使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新. 回调概览 用户可以使用回调来实现与场景 ...
- QT中使用函数指针
想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...
- C++中引用与指针的区别(详细介绍)
C++中引用与指针的区别(详细介绍) C++中的引用与指针的区别 指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一 ...
- Delphi中的函数指针判断是否为空
delphi函数指针 只有@@p才代表了函数指针本身的地址 assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...
- js中的this指针(三)
当一个函数并非一个对象的忏悔时,它会被当作一个函数来调用. 此时,函数中的 this 指针被绑定到了全局对象. 后果:方法不能利用内部函数来帮助工作,由于 this 被绑定了错误的值,将无法共享该方法 ...
- 由javascript中的this指针所想到的
初次结识 this 指针,是在学 <<C++ Primer Plus>>这本书的时候(这本书勉强读了一二遍,之后转学 html+css+js了,不过这是后话). 依稀记得书中举 ...
- [BS-21] 关于OC中对象与指针的思考
关于OC中对象与指针的思考 1. 创建对象: OC中可通过代码Person *p = [[Person alloc] init];创建了一个对象p.该过程中内存情况为: 在当前线程的栈(默认1M)中, ...
- PHP中使用数组指针函数操作数组示例
数组的内部指针是数组内部的组织机制,指向一个数组中的某个元素.默认是指向数组中第一个元素通过移动或改变指针的位置,可以访问数组中的任意元素.对于数组指针的控制PHP提供了以下几个内建函数可以利用. ★ ...
随机推荐
- HDU 6386 Age of Moyu 【BFS + 优先队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...
- MyBatis(1)优点&介绍&工程
本次全部学习内容:MyBatisLearning 一:jabc的相关说明: jdbc编程步骤: 加载数据库驱动 创建并获取数据库链接 创建jdbc statement对象 设置sql语句 设置sql语 ...
- VS调试_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));崩溃原因及解决方法
今天下午对面的老大调试遇到这个问题,大家一起讨论好久才解决这个问题 crt源代码都是可以看到的,为了了解清楚原因,十分有必要查看源码,源码一般在你的VS安装路径下VC\crt\src下. 点击重试,定 ...
- HTML CSS STYLE USER Font Awesome Definition Content Attribute
在css样式表中写样式: 其中content使用图标类对应的字符编码如下
- HDU 2092 (将表达式变成一元二次方程形式)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2092 整数解 Time Limit: 1000/1000 MS (Java/Others) Me ...
- AtomicStampedReference解决ABA问题
在运用CAS做Lock-Free操作中有一个经典的ABA问题: 线程1准备用CAS将变量的值由A替换为B,在此之前,线程2将变量的值由A替换为C,又由C替换为A,然后线程1执行CAS时发现变量的值 ...
- NodeJs仿阿帕奇实现浏览某一路径文件目录效果
网页效果 这里实现的效果是将我的电脑下的某一路径文件展现在网页中 html网页代码: <h1>仿阿帕奇网页 </h1> <table> <tr> < ...
- 在tornado中使用异步mysql操作
在使用tornado框架进行开发的过程中,发现tornado的mysql数据库操作并不是一步的,造成了所有用户行为的堵塞.tornado本身是一个异步的框架,要求所有的操作都应该是异步的,但是数据库这 ...
- TinyMCE插件:Filemanager [4.x-6.x] 图片自动添加水印
上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)有一个move_uploaded_file() ...
- R语言爬虫:穿越表单
使用rvest包实现实现穿越表单以及页面的跳转 formurl <- "http://open.xmu.edu.cn/oauth2/authorize?client_id=1010&a ...