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();


    }
  • 相关阅读:
    C#:基于WMI查询USB设备信息 及 Android设备厂商VID列表
    C#中 @ 的3种用途
    有关于 使用 命名管道 进行网络 进程间通信 的资料收集
    MySql LAST_INSERT_ID 【插入多条数据时】
    两个“不合理继承 ”的判定标识
    MYSQL 函数 字符串到整数
    Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程
    重构,拥有多个构造函数(重载)的类
    vue二级联动select
    gulp.dest用法详解
  • 原文地址:https://www.cnblogs.com/mumuliang/p/1884025.html
Copyright © 2011-2022 走看看