// M23.11.W5.UpdateCallback.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
//osg
#include <osg/Texture2D>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PolygonMode>
#include <osg/PolygonOffset>
#include <osg/OccluderNode>
#include <osg/LineWidth>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgGA/AnimationPathManipulator>
//osgUtil
#include <osgUtil/SmoothingVisitor>
//osglib
#pragma comment(lib,"OpenThreads_d.lib")
#pragma comment(lib,"osg_d.lib")
#pragma comment(lib,"osgDB_d.lib")
#pragma comment(lib,"osgFX_d.lib")
#pragma comment(lib,"osgGA_d.lib")
#pragma comment(lib,"osgUtil_d.lib")
#pragma comment(lib,"osgViewer_d.lib")
#pragma comment(lib,"osgShadow_d.lib")
class DynamicLineCallback: public osg::DrawableUpdateCallback
{
public:
DynamicLineCallback() = default;
/** do customized update code.*/
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable)
{
osg::Geometry* pGeom = dynamic_cast<osg::Geometry*>(drawable);
if (pGeom == nullptr) return;
osg::Vec3Array* pVextices = dynamic_cast<osg::Vec3Array*> (pGeom->getVertexArray());
if (pVextices == nullptr) return;
std::copy(pVextices->begin()+1, pVextices->end(),pVextices->begin());
m_dAndle += 1.0 / 10.0;
osg::Vec3 & LastPt = pVextices->back();
LastPt.set(10.0*cos(m_dAndle), 0, 10.0*sin(m_dAndle));
pVextices->dirty();
}
protected:
virtual ~DynamicLineCallback() = default;
DynamicLineCallback & operator=(const DynamicLineCallback &) = delete;
private:
float m_dAndle{0.0};
};
int main(int argc,char* argv[])
{
osg::ArgumentParser psr(&argc, argv);
osgViewer::Viewer viewer(psr);
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
//create geometry
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(10);
int i = 0;
std::for_each(vertices->begin(), vertices->end(), [&i](osg::Vec3&vertice) {vertice.set(++i,0,0);});
osg::ref_ptr<osg::Geometry> LineGeom = new osg::Geometry();
LineGeom->setVertexArray(vertices);
LineGeom->setUseDisplayList(false);
LineGeom->setUseVertexBufferObjects(true);
LineGeom->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::LINE_STRIP, 0, 10));
LineGeom->setUpdateCallback(new DynamicLineCallback());
// root geode
osg::ref_ptr<osg::Geode> root = new osg::Geode();
root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
root->getOrCreateStateSet()->setAttribute(new osg::LineWidth(2));
root->addDrawable(LineGeom);
osgUtil::SmoothingVisitor smoothvst;
root->accept(smoothvst);
viewer.setSceneData(root.get());
viewer.run();
return 0;
}
运行结果:绕圆周运动的线段