zoukankan      html  css  js  c++  java
  • OSG例程(1) 交互(Pick)

    OSG程序设计教程 第五章 交互的Pick实例

    代码
    #include <iostream>
    #include 
    <osgDB/ReadFile>
    #include 
    <osgViewer/Viewer>
    #include 
    <osg/Group>
    #include 
    <osg/Node>
    #include 
    <osgFX/Scribe>
    #include 
    <osgGA/GUIEventHandler>
    #include 
    <osgUtil/LineSegmentIntersector>

    class CPickHandler:public osgGA::GUIEventHandler
    {
    public:
        CPickHandler(osgViewer::Viewer 
    * viewer):mViewer(viewer){};
        
    virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &&aa)
        {
            
    switch(ea.getEventType())
            {
                
    // PUSH, button down
            case osgGA::GUIEventAdapter::PUSH:
                
    // 1,left button push
                if (ea.getButton()==1)
                {
                    Pick(ea.getX(),ea.getY());
                }
                
    return true;
            }
            
    return false;
        }
    protected:
        
    void Pick(float x, float y)
        {
            osgUtil::LineSegmentIntersector::Intersections intersections;
            
    if (mViewer->computeIntersections(x,y,intersections))
            {
                
    for (osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
                    hitr 
    != intersections.end();
                    
    ++ hitr)
                {
                    
    if (! hitr->nodePath.empty() && ! (hitr->nodePath.back()->getName().empty()))
                    {
                        
    const osg::NodePath &np = hitr->nodePath;
                        
    for(int i = np.size()-1;i>=0;--i)
                        {
                            osgFX::Scribe 
    *sc = dynamic_cast<osgFX::Scribe *> (np[i]);
                            
    if(sc!=NULL)
                            {
                                
    if(sc->getNodeMask()!=0)
                                    sc
    ->setNodeMask(0);
                            }
                        }
                    }

                }
            }
        }
        osgViewer::Viewer 
    *mViewer;
    };


    // argc: the number of argv
    // argv: an array of arguments,
    //       args[0] always is the path and name of the program itself.
    int main(int argc, char** argv)
    {

        osg::ref_ptr
    <osgViewer::Viewer> viewer = new osgViewer::Viewer();

        osg::ref_ptr
    <osg::Group>  root = new osg::Group();
        root
    ->addChild(osgDB::readNodeFile("cessna.osg"));
        osg::ref_ptr
    <osg::Node> cow = osgDB::readNodeFile("cow.osg");

        osg::ref_ptr
    <osgFX::Scribe> sc = new osgFX::Scribe();
        sc
    ->addChild(cow.get());
        
        root
    ->addChild(cow.get());
        root
    ->addChild(sc.get());
        
        viewer
    ->setSceneData(root.get());

        viewer
    ->addEventHandler(new CPickHandler(viewer.get()));

        viewer
    ->realize();
        viewer
    ->run();


    }
  • 相关阅读:
    python 学习之集合 三元运算 深浅拷贝
    python 学习之数据类型和for循环
    python 学习之运算符
    python 学习之编码转换和pycharm设置
    python 学习之python数据类型和流程控制
    Django实现下载文件名称为中文的处理
    递归删除postgresql数据库中所有表
    GO编程(打卡)-Task13: 并发编程
    GO编程(打卡)-Task12: 单元测试
    GO编程(打卡)-Task11: 反射机制
  • 原文地址:https://www.cnblogs.com/mumuliang/p/1884025.html
Copyright © 2011-2022 走看看