VTK 正交投影 透视投影

VTK默认透视投影(近大远小),如果想改成正交投影(平行投影,远近一样):

1.调用vtkCamera的ParallelProjectionOn函数开启

2.通过vtkCamera的SetParallelScale缩放

3.通过vtkCamera的SetClippingRange设置前后裁剪平面

关键代码如下:

//开启正交投影模式
renderer->GetActiveCamera()->ParallelProjectionOn();
renderer->GetActiveCamera()->SetParallelScale(10);
renderer->GetActiveCamera()->SetClippingRange(-1000, 1000);

全部代码:

QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());

//平面
vtkNew<vtkPlaneSource> plane; 
plane->SetCenter(0, 0, 0);//设置平面中心点坐标
plane->SetOrigin(-10, -10, 0);//设置平面起点坐标,一般为平面左下角
plane->SetPoint1(10, -10, 0);//设置平面第一条轴的长度
plane->SetPoint2(-10, 10, 0);//设置平面第二条轴的长度
plane->SetNormal(0, 0, 1);//设置平面的法向量
plane->SetXResolution(10);//x方向分割数
plane->SetYResolution(10);//y方向分割数
vtkNew<vtkPolyDataMapper> planeMapper;
planeMapper->SetInputConnection(plane->GetOutputPort());
vtkNew<vtkActor> planeActor;
planeActor->SetMapper(planeMapper);
planeActor->GetProperty()->EdgeVisibilityOn(); //显示线框
renderer->AddActor(planeActor);

//
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetRadius(5);
sphereSource->SetCenter(5, 0, 0);
sphereSource->Update();
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetColor(colors->GetColor4d("Green").GetData());
sphereActor->GetProperty()->EdgeVisibilityOn(); //显示线框
renderer->AddActor(sphereActor);


//开启正交投影模式
renderer->GetActiveCamera()->ParallelProjectionOn();
renderer->GetActiveCamera()->SetParallelScale(10);
renderer->GetActiveCamera()->SetClippingRange(-1000, 1000);

renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("RenderWindowNoUIFile");

ui.vtk_main->setRenderWindow(renderWindow);

 

posted @ 2023-05-24 16:56  上位机  阅读(827)  评论(0编辑  收藏  举报