#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgGA/TerrainManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osg/Material>
#include <osg/Geode>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osg/Camera>
#include <osg/io_utils>
#include <osg/ShapeDrawable>
#include <osgText/Text>
#include <sstream>
// 处理拾取事件的类,继承自osgGA::GUIEventhandler 界面事件处理
class PickHandler : public osgGA::GUIEventHandler {
public:
//构造函数
PickHandler(osgText::Text* updateText):
_updateText(updateText) {}
//析构函数
~PickHandler() {}
//处理(事件接口、动作接口)
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
//拾取(视图、事件接口)
virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea);
//设置显示内容
void setLabel(const std::string& name)
{
if (_updateText.get())
_updateText->setText(name);
}
protected:
//传递一个文字对象
osg::ref_ptr<osgText::Text> _updateText;
};
//事件处理(事件接口、动作接口)
bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
//dynamic_cast运算符的作用是将&aa转换成osgViewer::View*类型的对象
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (view) pick(view,ea);
return false;
}
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='c')
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
//event也是一个指针,类型是osgGA::GUIEventAdapter
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter(ea);
event->setX((ea.getXmin()+ea.getXmax())*0.5);
event->setY((ea.getYmin()+ea.getYmax())*0.5);
if (view) pick(view,*event);
}
return false;
}
default:
return false;
}
}
//拾取(视图、事件接口)通过view计算交点 ,从事件接口ea中获得 x和y
void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea)
{
//创建一个线段交集检测对象
osgUtil::LineSegmentIntersector::Intersections intersections;
std::string gdlist="";
//得到鼠标的位置 x和y
float x = ea.getX();
float y = ea.getY();
//如果发生交集运算 (即鼠标点中了物体)
if (view->computeIntersections(x,y,intersections))
{
//得到相交交集的交点, hitr是一个迭代器
for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
hitr != intersections.end();
++hitr)
{
std::ostringstream os;//申请一个流
if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty()))
{
// the geodes are identified by name.
os<<"Object \""<<hitr->nodePath.back()->getName()<<"\""<<std::endl;
}
else if (hitr->drawable.valid())
{
os<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl;
}
//将局部坐标顶点和法线输入到os这个流里面。
os<<" local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<" normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;
os<<" world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<" normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;
//交点索引列表vil,交点存在hitr里面
const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil = hitr->indexList;
for(unsigned int i=0;i<vil.size();++i)
{
os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;
}
//gdlist是一个字符串,用来存os中要显示的文字
gdlist += os.str();
}
}
//设置显示内容
setLabel(gdlist);
}
//创建HUD 参数是osgText。HUD也是作为场景中的一个节点存在
osg::Node* createHUD(osgText::Text* updateText)
{
// create the hud. derived from osgHud.cpp (derived:来源于)
// adds a set of quads, each in a separate Geode - which can be picked individually
// eg to be used as a menuing/help system!
// Can pick texts too!
osg::Camera* hudCamera = new osg::Camera;//创建一个相机
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);//设置绝对帧引用
hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);//设置正投影矩阵
hudCamera->setViewMatrix(osg::Matrix::identity());//设置视图矩阵
hudCamera->setRenderOrder(osg::Camera::POST_RENDER);//设置渲染顺序为POST
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);//清除深度缓存
//设置字体,声明一个timesFont,初始化为times.ttf
std::string timesFont("fonts/times.ttf");
//关掉字体的光照、禁用深度缓存 来保证字体一直在最上面
osg::Vec3 position(150.0f,800.0f,0.0f);//设置位置
osg::Vec3 delta(0.0f,-60.0f,0.0f);
{
osg::Geode* geode = new osg::Geode();//也可以写成osg::ref_ptr<osg::Geode> geode = new osg::Geode();-------geode就是一个指针
osg::StateSet* stateset = geode->getOrCreateStateSet();//状态设置
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);//关闭光照
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//关闭深度缓存
geode->setName("simple");//设置这个geode的名字,geode是场景中的叶子节点
hudCamera->addChild(geode);
osgText::Text* text = new osgText::Text;
geode->addDrawable( text );//geode是osg场景中的叶子节点,然后就是让所有drawable的东西成组
text->setFont(timesFont);//设置字体
text->setText("Picking in Head Up Displays is simple!\nHow to do it?\nAm I on the first quard?");//设置内容
text->setPosition(position);//设置字体位置,用到上面申明的position
position += delta;
}
for (int i=0; i<6; i++) {
osg::Vec3 dy(0.0f,-30.0f,0.0f);
osg::Vec3 dx(120.0f,0.0f,0.0f);
osg::Geode* geode = new osg::Geode();
osg::StateSet* stateset = geode->getOrCreateStateSet();
const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"};
osg::Geometry *quad=new osg::Geometry;//申明一个指向几何体的指针
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
std::string name="subOption";
name += " ";
name += std::string(opts[i]);
geode->setName(name);//设置节点的名字
osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad,弄个3元数组指针,数组有4个元素,分别保存矩形的4个顶点
osg::Vec4Array* colors = new osg::Vec4Array;//4元数组指针 ,保存颜色
colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.8-0.1*i, 0.1*i, 0.2*i, 1.0));//通过i的变化来实现块的渐变颜色
quad->setColorArray(colors);
quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);//绑定颜色到方块上