openscenegraph get new vector values after rotate

378 Views Asked by At

How to get the new points after rotating a geometry?

osg::ref_ptr<osg::Vec3Array> points = new osg::Vec3Array; 
osg::ref_ptr<osg::Geometry> geometry( new osg::Geometry);

points->push_back(osg::Vec3(0.0,0.0,0.0));
points->push_back(osg::Vec3(10.0,10.0,0.0));

geometry ->setVertexArray(points.get());
geometry ->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0, points->size()));

cout<<points[0][1]; //returns the right values 10 10 0

MatrixTransform* transform = new osg::MatrixTransform;
const double angle = 0.8;
const Vec3d axis(0, 0, 1);
transform->setMatrix(Matrix::rotate(angle, axis)); 
transform->addChild(geometry);

I tried this:

Vec3Array *_line= dynamic_cast< osg::Vec3Array*>(transform->getChild(0)->asGeometry()->getVertexArray());
cout<<_line->back(); // it returns equal 10 10 0

But it returns the same coords before rotating, where are the new point values?

1

There are 1 best solutions below

1
rickyviking On

What you see is the expected behavior: the MatrixTransform is applied at each frame to the underlying geometry, resulting in the "visualized" geometry to be moved.
If you actually want to modify the geometry vertices, you need to do so explicitly:

const double angle = 0.8;
const Vec3d axis(0, 0, 1);
osg::Matrix mx = Matrix::rotate(angle, axis));

osg::Vec3 originalVx(10.0, 10.0, 0.0);
osg::Vec3 modifiedVx = originalVx * mx;


Vec3Array *_line= dynamic_cast< osg::Vec3Array*>(transform->getChild(0)->asGeometry()->getVertexArray());
*_line[1] = modifiedVx;

If your update takes place at runtime, you'll need to dirty() the VertexBuffer and/or invalidate the DisplayList if you use them.