#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. find 查找文件的命令

    find顾名思义就是查找,Linux下find命令提供相当多的查找条件,可以在众多文件或目录下查找你想要的任何文件或目录. 语法: find filename 我当前目录下有aaa.txt和bbb.t ...

  2. DataTable通过Select进行过滤

    DataTable方法测试 //测试DataTable的select DataTable dt = new DataTable(); //a.OrderType, //a.[Status] dt.Co ...

  3. 'Cloud Native': What It Means, Why It Matters

    When HP announced July 28 that it was acquiring ActiveState's PaaS business, senior vice president B ...

  4. python实现查找算法

    搜索是在一个项目集合中找到一个特定项目的算法过程.搜索通常的答案是真的或假的,因为该项目是否存在. 搜索的几种常见方法:顺序查找.二分法查找.二叉树查找.哈希查找 线性查找线性查找就是从头找到尾,直到 ...

  5. Hive和HBase的区别 转载:https://www.cnblogs.com/guoruibing/articles/9894521.html

    1.Hive和HBase的区别 1)hive是sql语言,通过数据库的方式来操作hdfs文件系统,为了简化编程,底层计算方式为mapreduce. 2)hive是面向行存储的数据库. 3)Hive本身 ...

  6. Django --- 路由层(urls)

    目录 1.orm表关系如何建立 2.django请求生命周期流程图 3.urls.py路由层 4.路由匹配 5.无名分组 6.有名分组 7.反向解析 8.路由分发 9.名称空间 10.伪静态 11.虚 ...

  7. 随便写一个c++类

    为了让代码更贴合实际项目需要,我们分别用xxx.h文件,xxx.cpp文件来包含类的定义,类的声明和类的调用部分,实验平台vs2010 mycoach.h文件 #pragma once #includ ...

  8. AirTest与模拟器连接(二)

    如果我们手边没有可用的Android真机,又想进行Android应用自动化测试,这时候就要使用AirtestIDE的Android模拟器自动化测试功能了. AirtestIDE所支持的模拟器包括 An ...

  9. Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API

    Spark机器学习MLlib系列1(for python)--数据类型,向量,分布式矩阵,API 关键词:Local vector,Labeled point,Local matrix,Distrib ...

  10. 题解 [51nod1358] 浮波那契

    题解 [51nod1358] 浮波那契 题面 解析 首先根据经验应该能一眼矩阵快速幂加速.... 因为给了你递推式,并且\(O(n)\)求显然不可能. 所以考虑怎么构造矩阵. 首先要处理的是小数的问题 ...