osg解析系列-[osg::Node::setUpdateCallback] - 1.几何体的更新回调

// 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;
}


运行结果:绕圆周运动的线段
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值