VTK学习之光照和相机

目录

一、VTK光照

1、关于vtkLight常用的方法

2、最终效果

二、相机设置 

1、相机设置

2、效果


一、VTK光照

通过设置光照,可以达到不同颜色的目的,参考博客:

VTK修炼之道7_三维场景基本要素:光照_vtk 光照_沈子恒的博客-CSDN博客

1、关于vtkLight常用的方法

SetColor() — 设置光照的颜色(一般是设置rgb)

SetPosition() — 设置光照位置

SetFocalPoint() — 设置光照焦点

SetIntensity() — 设置光照的强度

SetSwitch() / SwitchOn()/ SwitchOff()— 打开或关闭对应的光照。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main()
{
	//设置圆
	vtkSmartPointer sphere =
		vtkSmartPointer::New();
	sphere->SetCenter(0, 0, 0);
	sphere->SetRadius(2.0);
	sphere->SetThetaResolution(15);
	sphere->SetPhiResolution(15);
	sphere->Update();

	//设置mapper
	vtkSmartPointer mapper = vtkSmartPointer::New();
	mapper->SetInputConnection(sphere->GetOutputPort());

	//设置actor
	vtkSmartPointer actor = vtkSmartPointer::New();
	actor->SetMapper(mapper);

	//设置vtkRenderer
	vtkSmartPointer renderer = vtkSmartPointer::New();
	renderer->AddActor(actor);
	renderer->SetBackground(0, 0, 0);

	vtkSmartPointer renWin = vtkSmartPointer::New();
	renWin->AddRenderer(renderer);
	renWin->SetSize(640, 480);
	renWin->Render();
	renWin->SetWindowName("Lights");

	vtkSmartPointer vtkren = vtkSmartPointer::New();
	vtkren->SetRenderWindow(renWin);

	vtkSmartPointer style = vtkSmartPointer::New();
	vtkren->SetInteractorStyle(style);

	//添加光照1(绿光)
	vtkSmartPointer myLight1 = vtkSmartPointer::New();
	myLight1->SetColor(0, 1, 0);
	myLight1->SetPosition(0, 0, 6);
	myLight1->SetFocalPoint(renderer->GetActiveCamera()->GetFocalPoint());
	renderer->AddLight(myLight1);

	//添加光照2(蓝光)
	vtkSmartPointer myLight2 = vtkSmartPointer::New();
	myLight2->SetColor(0, 0, 1);
	myLight2->SetPosition(0, 0, -6);
	myLight2->SetFocalPoint(renderer->GetActiveCamera()->GetFocalPoint());
	renderer->AddLight(myLight2);

	vtkren->Initialize();
	vtkren->Start();

	return 0;
}

2、最终效果

VTK学习之光照和相机_第1张图片

二、相机设置 

VTK学习之光照和相机_第2张图片

1、相机设置

相机位置:vtkCamera::SetPosition()或者GetPosition()
相机焦点:GetFocalPoint或者是SetFocalPoint
投影视角:(View Angle),默认的视角大小为30º,GetViewAngle()或者是SetViewAngle()

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main()
{
	vtkSmartPointer cone = vtkSmartPointer::New();
	cone->SetRadius(0.5);
	cone->SetCenter(0, 0, 0);

	//设置mapper
	vtkSmartPointer mapper = vtkSmartPointer::New();
	mapper->SetInputConnection(cone->GetOutputPort());

	//设置actor
	vtkSmartPointer actor = vtkSmartPointer::New();
	actor->SetMapper(mapper);

	//设置vtkRenderer
	vtkSmartPointer renderer = vtkSmartPointer::New();
	renderer->AddActor(actor);
	renderer->SetBackground(0, 0, 0);

	vtkSmartPointer renWin = vtkSmartPointer::New();
	renWin->AddRenderer(renderer);
	renWin->SetSize(640, 480);
	renWin->Render();
	renWin->SetWindowName("Camera");

	vtkSmartPointer vtkren = vtkSmartPointer::New();
	vtkren->SetRenderWindow(renWin);

	//获取默认相机参数
	vtkSmartPointer activeCamera = renderer->GetActiveCamera();
	double position[3] = { 0.0,0.0,0.0 };
	activeCamera->GetPosition(position);
	cout << "Default position:" << position[0] << "," << position[1] << "," << position[2]<GetFocalPoint(focalPoint);
	cout << "Default Focal Point:" << focalPoint[0] << "," << focalPoint[0] << "," << focalPoint[2] << endl;//0,0,0
	
	double viewAngle = activeCamera->GetViewAngle();
	cout << "viewAngle:"<< viewAngle << endl;

	//加入自己的相机参数
	vtkSmartPointer myCamera = vtkSmartPointer::New();
	position[0] = 0.0;
	position[1] = 0.0;
	position[2] = 1.0;
	myCamera->SetPosition(position);

	focalPoint[0] = 0;
	focalPoint[1] = 0;
	focalPoint[2] = 0;
	myCamera->SetFocalPoint(focalPoint);

	viewAngle = 45.0;
	myCamera->SetViewAngle(viewAngle);
	renderer->SetActiveCamera(myCamera);


	//添加光照1(绿光)
	vtkSmartPointer myLight1 = vtkSmartPointer::New();
	myLight1->SetColor(0, 1, 0);
	myLight1->SetPosition(0, 0, 6);
	myLight1->SetFocalPoint(renderer->GetActiveCamera()->GetFocalPoint());
	renderer->AddLight(myLight1);

	vtkren->Initialize();
	vtkren->Start();

	return  0;
}

将renderer->SetActiveCamera(myCamera);这句屏蔽,可以看到最初效果如下:

VTK学习之光照和相机_第3张图片

2、效果

放开那句之后,加入自定义相机之后的效果,如下:

VTK学习之光照和相机_第4张图片

本文为作者原创,转载,请标明出处!

推荐博客:精灵球Plus介绍_清风徐来Groot的博客-CSDN博客 

百度云盘:链接:https://pan.baidu.com/s/11b634VvKMIsGdahyBLpZ3Q   提取码:6666 

你可能感兴趣的:(vtk,学习)