1. 前言
在一个项目中,需要用鼠标和键盘完成对模型旋转操作和移动操作。
实现中,对鼠标的操作虚函数进行了重写,完成左右键点击,对模型正负方向旋转。
2.代码
#include "InitInclude.h"
#include "CreateSphere.h"
#include "thr.h"
#include "2D.h"
#include "2DplatformTexture.h"
#include "3DGammaKnife.h"
#include "2D(nei_qianghou).h"
#include "MouseInteractorHighLightActor.h"
#include "MouseInter.h"
#include "duobianxing.h"
#include "duobianxingzhuose(shiyongqianghoudiban).h"
#include "MouseInteractorStyle6 .h"
#include "testAdd.h"
#include <vtkImageImport.h>
#include <vtkImageActor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkInteractorStyleUser.h>
#include <vtkObjectFactory.h>
#include "my3DCameraStyle.h"
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include "MouseInteractorStyle2.h"
#include "MouseIntr922.h"
#include <vtkInteractorStyleTrackballActor.h>//另一种交互方式,操纵演员
#include <vtkInteractorStyleTrackballCamera.h>
// Handle mouse events
class MouseInteractorHighLightActor222 : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorHighLightActor222* New();
vtkTypeMacro(MouseInteractorHighLightActor222, vtkInteractorStyleTrackballCamera);
MouseInteractorHighLightActor222()
{
m_RotateZStep = 0.0;
LastPickedActor = NULL;
LastPickedProperty = vtkProperty::New();
}
virtual ~MouseInteractorHighLightActor222()
{
LastPickedProperty->Delete();
}
//左键按下执行以下操作
virtual void OnLeftButtonDown()
{
this->LastPickedActor = tActor;
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
m_RotateZStep += 10.05;
this->LastPickedActor->RotateZ(m_RotateZStep);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
//右键按下执行以下操作
virtual void OnRightButtonDown()
{
this->LastPickedActor = tActor;
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
m_RotateZStep += 10.05;
this->LastPickedActor->RotateZ(m_RotateZStep);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
virtual void OnChar()
{
this->LastPickedActor = tActor;
switch (this->Interactor->GetKeyCode())
{
case VK_SPACE:
{
//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
m_RotateZStep += 10.05;
this->LastPickedActor->RotateZ(m_RotateZStep);
}
cout << "sssssss38up " << endl;
break;
}
case 26:
{
cout << "wertyuio " << endl;
break;
}
}
//vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
vtkActor *tActor;
double m_RotateZStep;
private:
vtkActor *LastPickedActor;
vtkProperty *LastPickedProperty;
};
vtkStandardNewMacro(MouseInteractorHighLightActor222);
int main(int, char *[])
{ // This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
vtkNew<vtkCylinderSource> cylinder;
cylinder->SetResolution(8);
// The mapper is responsible for pushing the geometry into the graphics
// library. It may also do color mapping, if scalars or other attributes are
// defined.
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
{
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->RotateX(0);
cylinderActor->RotateY(-0);
cylinderActor->RotateZ(-0.0);
cylinderActor->SetOrigin(0, 0, 0);
}
//vtkSmartPointer<vtkActor> cylinderActor1 = vtkSmartPointer<vtkActor>::New();
//{
// cylinderActor1->SetMapper(cylinderMapper);
// double xx = 20;
// cylinderActor1->RotateX(xx);
// cylinderActor1->RotateY(-120.0);
// cylinderActor1->RotateZ(-40.0);
// cylinderActor1->SetOrigin(1, 1, 1);
//}
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
//vtkSmartPointer<MouseInteractorStyle2> StyleActor = vtkSmartPointer<MouseInteractorStyle2>::New();
vtkSmartPointer<MouseInteractorHighLightActor222> style = vtkSmartPointer<MouseInteractorHighLightActor222>::New();
style->SetDefaultRenderer(renderer);
style->tActor = cylinderActor;
renderWindowInteractor->SetInteractorStyle(style);
//style->SetActor(cylinderActor);
//renderWindowInteractor->SetInteractorStyle(style);
//StyleActor->SetDefaultRenderer(renderer);
//StyleActor->ActorA = cylinderActor;
//StyleActor->ActorB = cylinderActor1;
//renderWindowInteractor->SetInteractorStyle(StyleActor);
// The renderer generates the image
// which is then displayed on the render window.
// It can be thought of as a scene to which the actor is added
renderer->AddActor(cylinderActor);
//renderer->AddActor(cylinderActor1);
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(900, 900);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindow->Render();
renderWindowInteractor->Start();
}
3.效果
https://www.bilibili.com/video/BV1Wq4y1o7KN/