看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指针的更多相关文章

  1. OSG中的智能指针

    在OpenSceneGraph中,智能指针(Smart pointer)的概念指的是一种类的模板,它针对某一特定类型的对象(即Referenced类及其派生类)构建,提供了自己的管理模式,以避免因为用 ...

  2. [osg]OSG使用更新回调来更改模型

    使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新.        回调概览       用户可以使用回调来实现与场景 ...

  3. QT中使用函数指针

    想仿命令行,所以定义了一个类,让一个String 对应一个 function,将两者输入list容器. 类中定义了 QString commandStr; void (MainWindow::*com ...

  4. C++中引用与指针的区别(详细介绍)

    C++中引用与指针的区别(详细介绍) C++中的引用与指针的区别   指向不同类型的指针的区别在于指针类型可以知道编译器解释某个特定地址(指针指向的地址)中的内存内容及大小,而void*指针则只表示一 ...

  5. Delphi中的函数指针判断是否为空

    delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...

  6. js中的this指针(三)

    当一个函数并非一个对象的忏悔时,它会被当作一个函数来调用. 此时,函数中的 this 指针被绑定到了全局对象. 后果:方法不能利用内部函数来帮助工作,由于 this 被绑定了错误的值,将无法共享该方法 ...

  7. 由javascript中的this指针所想到的

    初次结识 this 指针,是在学 <<C++ Primer Plus>>这本书的时候(这本书勉强读了一二遍,之后转学 html+css+js了,不过这是后话). 依稀记得书中举 ...

  8. [BS-21] 关于OC中对象与指针的思考

    关于OC中对象与指针的思考 1. 创建对象: OC中可通过代码Person *p = [[Person alloc] init];创建了一个对象p.该过程中内存情况为: 在当前线程的栈(默认1M)中, ...

  9. PHP中使用数组指针函数操作数组示例

    数组的内部指针是数组内部的组织机制,指向一个数组中的某个元素.默认是指向数组中第一个元素通过移动或改变指针的位置,可以访问数组中的任意元素.对于数组指针的控制PHP提供了以下几个内建函数可以利用. ★ ...

随机推荐

  1. Integer类小细节随笔记录

    先看一段简单的代码: Integer v1 = Integer.valueOf(12); Integer v2 = Integer.valueOf(12); Integer v3 = Integer. ...

  2. 配置git服务

    https://www.cnblogs.com/sumuncle/p/6362697.html

  3. Mysql数据库的简单语法

    Mysql数据库是目前使用最为广泛的数据对,对于小型企业的支持度,比oracle数据库友好很多. mysql数据库的基本语法 1:创建并且使用数据库 找出服务器上当前存在什么数据库: SHOW DAT ...

  4. linux iptables 开启和关闭服务端口号

    需求: 模拟数据库挂掉,服务正常但访问数据库报错,恢复数据库端口后,服务是否能正常访问数据库 步骤:首先,断掉端口号5432,测试服务运行情况:其次,开启端口号5432,测试服务运行情况: 具体操作: ...

  5. usb入门学习

    1.学习资源: usb org.http://www.beyondlogic.org/usbnutshell/usb3.shtml http://wenku.baidu.com/view/028231 ...

  6. Nginx自定义模块编写:根据post参数路由到不同服务器

    Nginx自定义模块编写:根据post参数路由到不同服务器 2014-05-05 15:27 blogread IT技术博客 字号:T | T Nginx可以轻松实现根据不同的url 或者 get参数 ...

  7. 撸一个简单的MVVM例子

    我个人以为mvvm框架里面最重要的一点就是VM这部分,它要与Model层建立联系,将Model层转换成可以被View层识别的数据结构:其次也要同View建立联系,将数据及时更新到View层上,并且响应 ...

  8. about route

    route add default dev  my_iface1 route del default route add default gw 192.168.120.1 route add -net ...

  9. detach()之大坑:detach会引起局部变量失效引起线程对内存的非法访问题。

    detach()之大坑:detach会引起局部变量失效引起线程对内存的非法访问题.一:传递临时对象作为线程参数(1.1)要避免的陷阱(解释一)(1.2)要避免的陷阱(解释一)事实一:只要用临时构造的A ...

  10. JSP/Servlet开发——第七章 Servel基础

    1.Servlet简介: ●Servlet是一个符合特定规范的 JAVA 程序 , 是一个基于JAVA技术的Web组件. ●Servlet允许在服务器端,由Servlet容器所管理,用于处理客户端请求 ...