|
代码没有报出任何的问题。在win10系统下和win7系统运行之后,就崩了严重的直接死机,只有在一台win8上运行没有问题,困扰我一星期了。。。谢谢各位大神!!!
#include "../Common/Common.h"
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Geometry>
#include <osgUtil/IntersectionVisitor>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib, "../Debug/Common.lib")
#else
#pragma comment(lib, "../Release/Common.lib")
#endif
osg::ref_ptr<osg::Geode> CreateLine(const osg::Vec3 &start, const osg::Vec3 &end)
{
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
osg::ref_ptr<osg::Geometry> gy = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
gnode->addDrawable(gy);
gy->setVertexArray(coords);
coords->push_back(start);
coords->push_back(end);
gy->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:INE_LOOP, 0, 2));
return gnode;
}
osg::ref_ptr<osg::Geode> CreateSphere(const osg::Vec3d ¢er)
{
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
gnode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(center, 0.5)));
return gnode;
}
osg::ref_ptr<osg::Geode> CreateBox()
{
osg::ref_ptr<osg::Geode> gnode = new osg::Geode;
gnode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), 10.0, 10.0, 10.0)));
//gnode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), 0.1, 0.1, 20)));
return gnode;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Group> group = new osg::Group;
osg::Vec3 start = osg::Vec3(-10, 7, 15);
osg::Vec3 end = osg::Vec3(5, -4, -12);
//std::cout<<"a";
//
osgUtil::LineSegmentIntersector::Intersections intersections;
osg::ref_ptr<osgUtil::LineSegmentIntersector> ls = new osgUtil::LineSegmentIntersector(start, end);
osg::ref_ptr<osgUtil::IntersectionVisitor> iv = new osgUtil::IntersectionVisitor(ls);
//std::cout<<"a";
//
//osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("glider.osg");
//std::cout<<"a";
osg::ref_ptr<osg::Geode> node =CreateBox();
group->addChild(node);
//std::cout<<"a";
node->accept(*iv.get());
//std::cout<<"a";
group->addChild(CreateLine(start,end));
//std::cout<<"a";
//如果有碰撞,则输出所有的交点
if(ls->containsIntersections()) //问题似乎处在这里
{
intersections = ls->getIntersections();
for(osgUtil::LineSegmentIntersector::Intersections::iterator iter = intersections.begin(); iter != intersections.end(); iter++)
{
std::cout<< iter->getWorldIntersectPoint().x() << " " << iter->getWorldIntersectPoint().y() << " "<<iter->getWorldIntersectPoint().z()<<std::endl;
group->addChild(CreateSphere(iter->getWorldIntersectPoint()));
}
}
// else
// {
// std::cout<< ls->containsIntersections() << sendl;
// }
viewer->setSceneData(group);
return viewer->run();
} |
|