左:
右:
前:
后:
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCameraActor.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
void ViewDirection(vtkRenderer* renderer,
double lookX, double lookY, double lookZ,
double upX, double upY, double upZ)
{
renderer->GetActiveCamera()->SetPosition(lookX, lookY, lookZ); // 相机位置
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0); // 焦点位置
renderer->GetActiveCamera()->SetViewUp(upX, upY, upZ); // 相机朝上方向
renderer->ResetCamera();
renderer->Render();
}
// 左视图
void ViewPositiveX(vtkRenderer* renderer)
{
ViewDirection(renderer, -1, 0, 0, 0, 1, 0);
}
// 右视图
void ViewNegativeX(vtkRenderer* renderer)
{
ViewDirection(renderer, 1, 0, 0, 0, 1, 0);
}
// 前视图
void ViewPositiveY(vtkRenderer* renderer)
{
ViewDirection(renderer, 0, 0, 1, 0, 1, 0);
}
// 后视图
void ViewNegativeY(vtkRenderer* renderer)
{
ViewDirection(renderer, 0, 0, -1, 0, 1, 0);
}
// 上视图
void ViewPositiveZ(vtkRenderer* renderer)
{
ViewDirection(renderer, 0, 1, 0, 0, 0, -1);
}
// 下视图
void ViewNegativeZ(vtkRenderer* renderer)
{
ViewDirection(renderer, 0, -1, 0, 0, 0, 1);
}
int main(int, char* [])
{
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); // 读取STL
reader->SetFileName("C:\\Users\\wangjun\\3D Objects\\F22.STL");
reader->Update();
vtkSmartPointer <vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer <vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground(0.1, 0.2, 0.4);
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);
renWin->SetSize(300, 300);
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);
//ViewPositiveX(renderer); // 左
//ViewNegativeX(renderer); // 右
//ViewPositiveY(renderer); // 前
//ViewNegativeY(renderer); // 后
//ViewPositiveZ(renderer); // 上
//ViewNegativeZ(renderer); // 下
renWin->SetSize(600, 600);
renWin->Render();
iren->Start();
renderer->ResetCamera();
return 0;
}