1.VTK默认透视投影(近大远小);
1.调用vtkCamera的ParallelProjectionOn函数开启
2.通过vtkCamera的SetParallelScale缩放
3.通过vtkCamera的SetClippingRange设置前后裁剪平面
2.正交投影(平行投影,远近一样):
//开启正交投影模式
renderer->GetActiveCamera()->ParallelProjectionOn();
renderer->GetActiveCamera()->SetParallelScale(10);
renderer->GetActiveCamera()->SetClippingRange(-100, 100);
正交投影样例:
Code:
#pragma once
//#include "vtk_include.h"
#include <vtkLODActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTextActor.h>
#include <algorithm>
#include <array>
#include <vtkPlaneSource.h>
#include<stdio.h>
#include<iostream>
using namespace std;
void main()
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> renderer;
//平面
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->ResetCamera();
//开启正交投影模式
renderer->GetActiveCamera()->ParallelProjectionOn();
renderer->GetActiveCamera()->SetParallelScale(10);
renderer->GetActiveCamera()->SetClippingRange(-1000, 1000);
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("RenderWindowNoUIFile");
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renderWindow);
renderWindow->Render();
iren->Initialize();
iren->Start();
}