拾取一个对象,拾取的是这个对象所在的actor,所以如果想让不同的对象分开被拾取,一个对象就要创建一个actor。
示例代码功能
1 拾取到一个对象,先保存原有属性,把这个被拾取的对象设置成红色
2 拾取下一个对象,恢复上一个被拾取对象本来的颜色,对被拾取对象执行步骤1
代码
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkMath.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkSphereSource.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
// Handle mouse events
// 拾取每次拾取到的是一个actor
class PropPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static PropPickerInteractorStyle* New();
vtkTypeMacro(PropPickerInteractorStyle, vtkInteractorStyleTrackballCamera);
// 构造函数初始化
PropPickerInteractorStyle()
{
LastPickedActor = NULL;
LastPickedProperty = vtkProperty::New();
}
virtual ~PropPickerInteractorStyle()
{
LastPickedProperty->Delete();
}
// 鼠标按下的时候拾取
virtual void OnLeftButtonDown()
{
// 获取鼠标按下的平面坐标
int* clickPos = this->GetInteractor()->GetEventPosition();
// Pick from this location.
vtkSmartPointer<vtkPropPicker> picker = vtkSmartPointer<vtkPropPicker>::New();
picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());
double* pos = picker->GetPickPosition();
// If we picked something before, reset its property
if (this->LastPickedActor)
{
this->LastPickedActor->GetProperty()->DeepCopy(this->LastPickedProperty);// 获取上次的属性
}
this->LastPickedActor = picker->GetActor(); // 拾取到的新actor
if (this->LastPickedActor)
{
// Save the property of the picked actor so that we can restore it next time
this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());// 记住这次拾取对象的属性,存到成员变量中
// Highlight the picked actor by changing its properties
this->LastPickedActor->GetProperty()->SetColor(1.0, 0.0, 0.0); // 拾取到的对象,设置成红色
this->LastPickedActor->GetProperty()->SetDiffuse(1.0);
this->LastPickedActor->GetProperty()->SetSpecular(0.0);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
private:
vtkActor *LastPickedActor;
vtkProperty *LastPickedProperty;
};
vtkStandardNewMacro(PropPickerInteractorStyle);
int main(int argc, char *argv[])
{
int numberOfSpheres = 10;
if (argc > 1)
{
numberOfSpheres = atoi(argv[1]);
}
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->Render();
// 设置窗口标题
renderWindow->SetWindowName("PropPicker");
renderWindow->AddRenderer(renderer);
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Set the custom type to use for interaction.
vtkSmartPointer<PropPickerInteractorStyle> style = vtkSmartPointer<PropPickerInteractorStyle>::New();
style->SetDefaultRenderer(renderer);
renderWindowInteractor->SetInteractorStyle(style);
// 由于拾取以actor为单位,所以,要把一个对象存到一个actor中
for (int i = 0; i < numberOfSpheres; ++i)
{
// 创建球对象数据
vtkSmartPointer<vtkSphereSource> source = vtkSmartPointer<vtkSphereSource>::New();
//生成随机坐标:x[-5,5] y[-5,5] z[-5,5]
double x, y, z, radius;
x = vtkMath::Random(-5, 5);
y = vtkMath::Random(-5, 5);
z = vtkMath::Random(-5, 5);
// 生成随机半径
radius = vtkMath::Random(.5, 1.0);
source->SetRadius(radius);
source->SetCenter(x, y, z);
source->SetPhiResolution(11);
source->SetThetaResolution(21);
// 创建一个对象需要的 mapper
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(source->GetOutputPort());
// 创建一个对象需要的 actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// 获取随机颜色
double r, g, b;
r = vtkMath::Random(.4, 1.0);
g = vtkMath::Random(.4, 1.0);
b = vtkMath::Random(.4, 1.0);
actor->GetProperty()->SetDiffuseColor(r, g, b);
actor->GetProperty()->SetDiffuse(.8);
actor->GetProperty()->SetSpecular(.5);
actor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
actor->GetProperty()->SetSpecularPower(30.0);
// 将对象添加到renderer
renderer->AddActor(actor);
}
renderer->SetBackground(1.0, 1.0, 1.0);// 白色背景
// Render and interact
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
示例动态图效果