工程环境
visual studio 2019
项目模板
Cmake项目
Example
????????程序功能:vtk动画,利用定时器以及回调函数来使球体围绕z轴旋转的动画。
代码:
RotatingSphere.cpp
// RotatingSphere.cpp: 定义应用程序的入口点。
//
#include "RotatingSphere.h"
#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
class vtkTimerCallback2 : public vtkCommand
{
public:
vtkTimerCallback2() = default;
~vtkTimerCallback2() = default;
int timerId = 0;
static vtkTimerCallback2* New()
{
vtkTimerCallback2* cb = new vtkTimerCallback2;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject* caller, unsigned long eventId,
void* vtkNotUsed(callData))
{
vtkRenderWindowInteractor* iren =
dynamic_cast<vtkRenderWindowInteractor*>(caller);
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
if (TimerCount < 36)
{
actor->RotateZ(5);
iren->GetRenderWindow()->Render();
}
else
{
iren->DestroyTimer();
}
}
private:
int TimerCount = 0;
public:
vtkActor* actor;
};
int main(int, char* [])
{
vtkNew<vtkNamedColors> colors;
// Create a sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(1.0);
sphereSource->SetThetaResolution(15);
sphereSource->SetPhiResolution(15);
sphereSource->Update();
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->RotateX(90);
actor->GetProperty()->SetRepresentationToWireframe();
// Create a renderer, render window, and interactor
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("Seashell").GetData());
// Render and interact
renderWindow->SetWindowName("RotatingSphere");
renderWindow->Render();
// Initialize must be called prior to creating timer events.
renderWindowInteractor->Initialize();
// Sign up to receive TimerEvent
vtkNew<vtkTimerCallback2> cb;
cb->actor = actor;
renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = renderWindowInteractor->CreateRepeatingTimer(100);
cb->timerId = timerId;
// Start the interaction and timer
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
说明
1、使用vtkNamedColors获取对应颜色的rgb。
?2、使用vtkSphereSource创建球体
? ? ?
?3、使用vtkRenderWindowInteractor交互器设置一个定时器。
? ? ? 使用CreateRepeatingTimer创建一个100毫秒调一次的重复定时器
4、使用vtkTimerCallback2回调函数实现动画功能
? ? ? 通过第三步设置的定时器重复触发TimerEvent事件来使球体actor绕z轴旋转5度,达到动画的效果。
GitHub地址
VTKExamples/Animation/RotatingSphere at main · 18810186103/VTKExamples · GitHub
|