#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32 #include <osg/Group>
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osg/Node> #include <osg/Geometry>
#include <osg/Image>
#include <osg/ShapeDrawable>
#include <osg/Texture2D> #include <osg/MatrixTransform>
#include <osg/AnimationPath> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgGA/DriveManipulator>
#include <osgGA/GUIEventHandler>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIActionAdapter> #include <osgGA/AnimationPathManipulator> #include <osgUtil/LineSegmentIntersector> #include <iostream>
using namespace std; class PickHandler :public osgGA::GUIEventHandler
{
public:
PickHandler(osgViewer::Viewer *viewerParam)
{
viewer1 = viewerParam;
controls = new osg::Vec3Array;
} osg::AnimationPath* createPath()
{
osg::ref_ptr<osg::AnimationPath> animationPath = new osg::AnimationPath;
animationPath->setLoopMode(osg::AnimationPath::LOOP); float time = 0.0;
float angle = 0.0;
float degrees = osg::inDegrees(90.0); if (controls.valid())
{
osg::Vec3Array::iterator iter1 = controls->begin();
for (;;)
{
osg::Vec3 position1(*iter1);
iter1++;
if (iter1 != controls->end())
{
if (iter1->x() > position1.x())
{
angle = 1.57 - atan((iter1->y() - position1.y()) / (iter1->x() - position1.x()));
if (angle<)
{
angle = angle + 1.57;
}
}
else
{
angle = -1.57 - atan((iter1->y() - position1.y()) / (iter1->x() - position1.x()));
if (angle>)
{
angle = -(1.57 - angle);
}
} osg::Quat rotation1(osg::Quat(degrees, osg::Vec3(1.0, 0.0, 0.0))*osg::Quat(-angle, osg::Vec3(0.0, 0.0, 1.0)));
animationPath->insert(time, osg::AnimationPath::ControlPoint(position1, rotation1));
time += calculateDistance(position1, *iter1);
}
else
{
break;
}
}
} return animationPath.release();
} float calculateDistance(osg::Vec3 vecStart,osg::Vec3 vecEnd)
{
float speed = 0.4;
float dis1 = sqrt((vecStart.x() - vecEnd.x())*(vecStart.x() - vecEnd.x()) + (vecStart.y() - vecEnd.y())*(vecStart.y() - vecEnd.y()));
return dis1*speed;
} osg::Geode* createBox(osg::Vec3 centers)
{
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
gnode->addDrawable(new osg::ShapeDrawable(new osg::Box(centers, 7.0, 7.0, 7.0)));
return gnode.release();
} bool handle(const osgGA::GUIEventAdapter& gea, osgGA::GUIActionAdapter& gaa)
{
switch (gea.getEventType())
{
case osgGA::GUIEventAdapter::DOUBLECLICK:
if (viewer1)
{
float x=0.0, y=0.0;
x = gea.getX();
y = gea.getY();
//申请一个存放交叉点的集合
osgUtil::LineSegmentIntersector::Intersections inters;
// bool computeIntersections(float x,float y, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff);
if (viewer1->computeIntersections(x,y,inters))
{
osgUtil::LineSegmentIntersector::Intersections::iterator iter1 = inters.begin();
std::cout <<"x:"<< iter1->getWorldIntersectPoint().x()<<" y:"<<iter1->getWorldIntersectPoint().y()<< std::endl;
//controls->push_back(iter1->getWorldIntersectPoint());
controls->push_back(osg::Vec3(iter1->getWorldIntersectPoint().x(), iter1->getWorldIntersectPoint().y(), ));
viewer1->getSceneData()->asGroup()->addChild(createBox(iter1->getWorldIntersectPoint()));
//osg::ref_ptr<osg::Node> node2 = viewer1->getSceneData();
//osg::ref_ptr<osg::Group> group2 = new osg::Group;
//group2->addChild(node2);
//group2->addChild(createBox(iter1->getWorldIntersectPoint()));
//viewer1->setSceneData(group2); }
}
break; case osgGA::GUIEventAdapter::KEYDOWN:
//F:70 0:96
if (gea.getKey()== 0x20)
{
if (viewer1)
{
osgGA::AnimationPathManipulator* animationPathManipulator1 = new osgGA::AnimationPathManipulator;
animationPathManipulator1->setAnimationPath(createPath());
viewer1->setCameraManipulator(animationPathManipulator1);
}
} break; default:
break;
} return false;
} private:
osgViewer::Viewer *viewer1;
osg::ref_ptr<osg::Vec3Array> controls;
}; int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer;
osg::ref_ptr<osg::Group> group1 = new osg::Group;
osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:\\参考手册\\BIM\\osg\\build20190628.osgb"); group1->addChild(node1.get());
viewer1->setSceneData(group1.get());
viewer1->addEventHandler(new PickHandler(viewer1)); viewer1->setUpViewInWindow(, ,, , ); return viewer1->run();
}

osg 场景漫游的更多相关文章

  1. OSG实现场景漫游(转载)

    OSG实现场景漫游 下面的代码将可以实现场景模型的导入,然后在里面任意行走,于此同时还实现了碰撞检测. 源代码下载地址: /* * File : Travel.cpp * Description : ...

  2. 【Unity入门】场景编辑与场景漫游快捷键

    版权声明:本文为博主原创文章,转载请注明出处. 打开Unity主窗口,选择顶部菜单栏的“GameObject”->“3D Object”->“Plane”在游戏场景里面添加一个面板对象.然 ...

  3. Unity3d场景漫游---iTween实现

    接触U3D以来,我做过的场景漫游实现方式一般有以下几种: Unity3d中的Animation组件,通过设置摄像机的关键点实现场景漫游 第一人称或第三人称控制器 编写摄像机控制脚本 iTween iT ...

  4. Unity3d 简单的小球沿贝塞尔曲线运动(适合场景漫游使用)

        简单的小球沿贝塞尔曲线运动,适合场景漫游使用 贝塞尔曲线:(贝塞尔曲线的基本想法部分摘自http://blog.csdn.net/u010019717/article/details/4768 ...

  5. 基于SketchUp和Unity3D的虚拟场景漫游和场景互动

    这是上学期的一次课程作业,难度不高但是也一并记录下来,偷懒地拿课程报告改改发上来. 课程要求:使用sketchUp建模,在Unity3D中实现场景漫游和场景互动. 知识点:建模.官方第一人称控制器.网 ...

  6. [Unity3D]巧妙利用父级子级实现Camera场景平面漫游

    本文系作者原创,转载请注明出处 入门级的笔者想了一上午才搞懂那个欧拉角的Camera旋转..=.= 在调试场景的时候,每次都本能的按下W想前进,但是这是不可能的(呵呵) 于是便心血来潮想顺便添加个Ke ...

  7. OSG中相机参数的更改

    #pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...

  8. 【学习笔记】OSG中相机参数的更改

    #pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...

  9. osgViewer:: Viewer::advance() osg多线程与智能指针

    void ViewerBase::frame(double simulationTime) { if (_done) return; // OSG_NOTICE<<std::endl< ...

随机推荐

  1. iptables详解(2)表中规则管理(增删改查)

    我们定义了四张表:raw表.mangle表.nat表.filter表,不同的表有不同的功能 filter表用来过滤,允许哪些ip.端口访问,禁止哪些ip.端口访问,表中会有很多链 ①禁止ip地址访问我 ...

  2. 2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划

    2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划 [Problem Description] ​ 有向无环图中,有个机器人从\(1\)号节点出发,每天等概率的走到下 ...

  3. FRDM-KL43开发板驱动段式液晶SLCD的实现方法

    LCD的驱动不像LED那样,加上电压(LED实际上是电流驱动)就可以长期显示的. LCD驱动必须使用交流电压驱动才能保持稳定的显示,如果在LCD上加上稳定的直流电压, 不但不能正常显示,时间久了还会损 ...

  4. java 从上至下打印二叉树

    从上往下打印二叉树题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行一个整数n(1<=n<=1000, ...

  5. 实验十一 团队作业7:团队项目设计完善&编码1

    博文简要信息表: 项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh/ 本次实验链接地址 https://www.cnblogs.com/nwnu-daizh/ ...

  6. python 单引号与双引号的转义

    import simplejson a = """{"a":"\\""}""" b = & ...

  7. ES6学习记录(一)

    Class类 Class的静态方法 类相当于实例的原型,所有在类中定义的方法,都会被实例继承.如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态 ...

  8. HashMap与HashTable的理解与区别

    Hashtable是java一开始发布时就提供的键值映射的数据结构,而HashMap产生于JDK1.2.虽然Hashtable比HashMap出现的早一些,但是现在Hashtable基本上已经被弃用了 ...

  9. 51NOD 1452 - 加括号

    DP预处理每个区间的值,再枚举括号位置就好了 #include <bits/stdc++.h> using namespace std; typedef long long ll; con ...

  10. MyBatis的关联查询

    关联映射的一对多 //查询经理角色 以及 该角色下对应的员工集合 public SmbmsRole getRoleAndUser(Integer id); <resultMap id=" ...