动画路径回调

时间:2021-08-28
本文章向大家介绍动画路径回调,主要包括动画路径回调使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <Windows.h>
#include <osg/Node>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Switch>
#include <osg/MatrixTransform>
#include <osg/NodeCallback>
#include <osgFX/Scribe>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>

osg::AnimationPathCallback* createAnimationPathCallback(float radius, float time)
{
	osg::ref_ptr<osg::AnimationPath> path = new osg::AnimationPath;
	path->setLoopMode(osg::AnimationPath::LOOP);

	int sample = 32;
	float delta_yaw = 2.0f * osg::PI / ((float)sample - 1.0f);
	float delta_time = time / (float)sample;
	for (int i = 0; i < sample; ++i)
	{
		float yaw = delta_yaw * (float)i;
		osg::Vec3 pos(cos(yaw)*radius, sin(yaw)*radius, 0.0f);
		osg::Quat rot(yaw-osg::PI_2, osg::Z_AXIS);
		path->insert(delta_time * (float)i, osg::AnimationPath::ControlPoint(pos, rot));
	}

	osg::ref_ptr<osg::AnimationPathCallback> apc = new osg::AnimationPathCallback;
	apc->setAnimationPath(path.get());
	return apc.release();
}

osg::Geode* createAnimationPathNode(float radius)
{
	osg::ref_ptr<osg::Geode> geode = new osg::Geode();
	geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
	osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();

	//设置顶点
	osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array();
	int sample = 32;
	float delta_yaw = 2.0f* osg::PI / (sample - 1.0f);
	for (int i = 0; i < sample; ++i)
	{
		float yaw = delta_yaw*i;
		osg::Vec3 pos(cos(yaw)*radius, sin(yaw)*radius, 0.0);
		vertex->push_back(pos);
	}
	geometry->setVertexArray(vertex);

	//设置颜色
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
	for (int i = 0; i < sample; ++i)
	{
		colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0 / (double)sample*(double)i));
	}
	geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);

	//图元装配
	geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, vertex->size()));

	geode->addDrawable(geometry);

	return geode.release();
}

int main()
{
	osgViewer::Viewer viewer;

	osg::ref_ptr<osg::Group> group = new osg::Group();
	float radius = 10.0;
	float time = 6.0;
	osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();
	mt->addChild(osgDB::readNodeFile("glider.osg"));
	mt->setUpdateCallback(createAnimationPathCallback(radius, time));
	group->addChild(mt);
	group->addChild(createAnimationPathNode(radius));

	viewer.setSceneData(group);
	viewer.addEventHandler(new osgViewer::StatsHandler());
	viewer.setUpViewInWindow(100, 100, 500, 400);

	return viewer.run();
}

原文地址:https://www.cnblogs.com/shiguoliang/p/15201153.html