在OSG中旋转时图像消失 [英] Image is disappearing with rotation in OSG

查看:235
本文介绍了在OSG中旋转时图像消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的OSG程序,它使一个x,y轴和随机点在这个轴内。目的是,这将导致制作用于激光扫描数据的3D观察器。 (我知道它以前做过,但我们需要它是超轻的重量)。这是代码:

I have a simple OSG program which makes an x, y axis and randomises points inside of this axis. The intention is that this will lead on to making a 3d viewer for laser scan data. (I know its been done before but we need it to be super light weight). Here is the code:

#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查看器中旋转图像时,整个图像经常消失。有时它可以通过旋转回到你开始的地方带回来,但更多的时候它会永远消失。

This code works, it will generate a 10 unit long x/y axis and begin to generate random points inside that axis. However the issue is that when I rotate the image in the OSG viewer the whole image often disappears. Sometimes it can be brought back by rotating back to where you started but more often it will disappear forever.

有人知道为什么会发生这种情况吗?

Has anyone got an idea about why this is happening?

推荐答案

我想出来了,在这种情况下的原因是点是更新太快。这导致观察者在渲染图像时极度困难,因为点快速进入。

I figured it out, the reason in this case is that the points are updating too fast. This is causing the visualizer extreme difficulty with rendering the image as the points are coming in to fast.

这篇关于在OSG中旋转时图像消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆