osg 场景漫游
#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 场景漫游的更多相关文章
- OSG实现场景漫游(转载)
OSG实现场景漫游 下面的代码将可以实现场景模型的导入,然后在里面任意行走,于此同时还实现了碰撞检测. 源代码下载地址: /* * File : Travel.cpp * Description : ...
- 【Unity入门】场景编辑与场景漫游快捷键
版权声明:本文为博主原创文章,转载请注明出处. 打开Unity主窗口,选择顶部菜单栏的“GameObject”->“3D Object”->“Plane”在游戏场景里面添加一个面板对象.然 ...
- Unity3d场景漫游---iTween实现
接触U3D以来,我做过的场景漫游实现方式一般有以下几种: Unity3d中的Animation组件,通过设置摄像机的关键点实现场景漫游 第一人称或第三人称控制器 编写摄像机控制脚本 iTween iT ...
- Unity3d 简单的小球沿贝塞尔曲线运动(适合场景漫游使用)
简单的小球沿贝塞尔曲线运动,适合场景漫游使用 贝塞尔曲线:(贝塞尔曲线的基本想法部分摘自http://blog.csdn.net/u010019717/article/details/4768 ...
- 基于SketchUp和Unity3D的虚拟场景漫游和场景互动
这是上学期的一次课程作业,难度不高但是也一并记录下来,偷懒地拿课程报告改改发上来. 课程要求:使用sketchUp建模,在Unity3D中实现场景漫游和场景互动. 知识点:建模.官方第一人称控制器.网 ...
- [Unity3D]巧妙利用父级子级实现Camera场景平面漫游
本文系作者原创,转载请注明出处 入门级的笔者想了一上午才搞懂那个欧拉角的Camera旋转..=.= 在调试场景的时候,每次都本能的按下W想前进,但是这是不可能的(呵呵) 于是便心血来潮想顺便添加个Ke ...
- OSG中相机参数的更改
#pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...
- 【学习笔记】OSG中相机参数的更改
#pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...
- osgViewer:: Viewer::advance() osg多线程与智能指针
void ViewerBase::frame(double simulationTime) { if (_done) return; // OSG_NOTICE<<std::endl< ...
随机推荐
- 使用C#应用程序与Arduino开发板进行通信
在本文中,我们将一个Arduino Pro Mini开发板连接到PC,并且使用C# Windows应用程序与其进行通信. 将硬件连接到PC是非常有利的,不仅是因为你可以发送命令以及监控状态,还可以实时 ...
- 云计算(8)--MapReduce如何处理fault
一些常见的故障 NM周期性的给RM发送heartbeats,如果RM发现server fails,则它会让所有与这个server有关的AM知道,让受影响的job的AM采取一些action,重新分配它的 ...
- Java8新特性--CompletableFuture
并发与并行 Java 5并发库主要关注于异步任务的处理,它采用了这样一种模式,producer线程创建任务并且利用阻塞队列将其传递给任务的consumer.这种模型在Java 7和8中进一步发展,并且 ...
- vue init webpack-simple
vue init webpack-simple .. 将我们的项目更加方便,更有助于开发者快速开发. vue init webpack-simple的项目默认打包后之后一个html和一个js文件,而 ...
- Python3连接MySQL数据库实战
Python3连接MySQL数据库实战 第三方库 :pymysql 数据库连接 def connect(): try: #建立数据库连接,从左至右参数依次为 # ip地址 我用的是云端数据库 如果为本 ...
- 引入其他服务器的JS文件,同时也引入本地JS文件 报错时:
控制台报错: Uncaught ReferenceError: define is not defined at core.js:5
- MySQL表结构,表空间,段,区,页,MVCC ,undo 事务槽
索引组织表(IOT表):为什么引入索引组织表,好处在那里,组织结构特点是什么,如何创建,创建IOT的限制LIMIT. IOT是以索引的方式存储的表,表的记录存储在索引中,索引即是数据,索引的KEY为P ...
- Vue.config.productionTip = false 是什麽意思
阻止启动生产消息,常用作指令. 阻止启动生产消息 這又是什麽意思? 看下效果 Vue.config.productionTip = false Vue.config.productionTip = t ...
- 【csp模拟赛九】--dfs3
这道题贪心错误:直接dfs就行,枚举新开一个还是往之前的里面塞 贪心代码(80): #include<cstdio> #include<algorithm> #include& ...
- 简述with原理
with open('x') as f: for line in f: print(line.replace('a', 'b')) 不管在处理文件过程中是否发生异常,都能保证 with 语句执行完毕后 ...