2012-04-16 122 views
1

我有一個簡單OSG程序,這使得一個x,y軸和randomises該軸的內部的點。目的是,這將導致爲激光掃描數據製作3D瀏覽器。 (我知道它以前做過,但我們需要它是超輕量級的)。下面是代碼:圖像與旋轉消失在OSG

#include <osg/Node> 
#include <osg/Group> 
#include <osg/Geode> 
#include <osg/Geometry> 
#include <osg/Texture2D> 
#include <osgDB/ReadFile> 
#include <osgViewer/Viewer> 
#include <osg/PositionAttitudeTransform> 
#include <osgGA/TrackballManipulator> 
#include <time.h> 
#include <cstdlib> 

void addAxis(osg::ref_ptr<osg::Group> root) { 


//ADD Y-Axis 
osg::ref_ptr<osg::Geode> lineGeode = new osg::Geode(); 
osg::ref_ptr<osg::Geometry> yAxis = new osg::Geometry(), xAxis = new osg::Geometry(); 

lineGeode->addDrawable(yAxis); 
lineGeode->addDrawable(xAxis); 
root->addChild(lineGeode); 


osg::ref_ptr<osg::Vec3Array> lineVertices = new osg::Vec3Array; 
lineVertices->push_back(osg::Vec3(0, 0, 0)); 
lineVertices->push_back(osg::Vec3(0, 10, 0)); 


yAxis->setVertexArray(lineVertices); 

osg::ref_ptr<osg::DrawElementsUInt> lineBase = 
    new osg::DrawElementsUInt(osg::PrimitiveSet::LINES, 0); 
lineBase->push_back(1); 
lineBase->push_back(0); 
yAxis->addPrimitiveSet(lineBase); 


osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array; 
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); //index 0 red 
yAxis->setColorArray(colors); 
yAxis->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); 



//ADD X Axis 
lineVertices = new osg::Vec3Array; 
lineVertices->push_back(osg::Vec3(0, 0, 0)); 
lineVertices->push_back(osg::Vec3(10, 0, 0)); 


xAxis->setVertexArray(lineVertices); 

lineBase = 
    new osg::DrawElementsUInt(osg::PrimitiveSet::LINES, 0); 
lineBase->push_back(1); 
lineBase->push_back(0); 
xAxis->addPrimitiveSet(lineBase); 


colors = new osg::Vec4Array; 
colors->push_back(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f)); //index 0 red 
xAxis->setColorArray(colors); 
xAxis->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); 

} 

void addPoint(osg::ref_ptr<osg::Group> root, std::vector<double> pointIn) { 
osg::ref_ptr<osg::Geode> pointGeode = new osg::Geode(); 
osg::ref_ptr<osg::Geometry> pointGeometry = new osg::Geometry(); 



pointGeode->addDrawable(pointGeometry); 
root->addChild(pointGeode); 

osg::ref_ptr<osg::Vec3Array> point = new osg::Vec3Array; 
point->push_back(osg::Vec3(pointIn[0], pointIn[1], pointIn[2])); 

pointGeometry->setVertexArray(point); 

osg::ref_ptr<osg::DrawElementsUInt> points = 
    new osg::DrawElementsUInt(osg::PrimitiveSet::POINTS, 0); 

points->push_back(0); 
points->push_back(2); 
points->push_back(1); 
pointGeometry->addPrimitiveSet(points); 


osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array; 
colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); 

pointGeometry->setColorArray(colors); 
    pointGeometry->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); 
} 


int main() 
{ 
osgViewer::Viewer viewer; 
osg::ref_ptr<osg::Group> root = new osg::Group(); 

addAxis(root); 


std::vector<double> point; 
for (int i = 0; i < 3; i++) 
    point.push_back(0); 
srand(time(NULL)); 

root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); 
viewer.setSceneData(root); 

viewer.setCameraManipulator(new osgGA::TrackballManipulator()); 
viewer.realize(); 
int count = 0; 
while(!viewer.done()) 
{ 
    if (count == 200) { 
    point[0] = (double)rand()*10.0/(double)INT_MAX; 
    point[1] = (double)rand()*10.0/(double)INT_MAX; 
    count = 0; 
    } 
    count++; 
    addPoint(root, point); 

    viewer.frame(); 



} 

return 0; 
} 

此代碼的工作,就會產生一個10單元長的x/y軸並開始產生該軸的內部的隨機點。然而,問題是,當我在OSG觀察者旋轉圖像的整體形象往往消失。有時候,它可以通過轉回到你開始的地方,但更多的時候它就會永遠消失帶回。

有沒有人有關於爲什麼會這樣的想法?

+0

我對OSG並不熟悉,但在該片段中的任何位置都看不到任何旋轉代碼。你能指出你添加了什麼導致它出錯的原因嗎?你確定你正在圍繞正確的軸旋轉嗎? – Tim 2012-04-16 04:58:06

+0

所以這段代碼打開了一個使用鼠標旋轉圖像的visuliser。當我在每幀之間添加隨機點時,這是錯誤開始發生的時間。 – 2012-04-16 22:55:11

回答

1

我想通了,在這種情況下,原因是點更新太快。這導致視覺渲染器極其困難,隨着點快速進入,渲染圖像。