1. 贴上代码
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
//
// This simple example shows how to do basic rendering and pipeline
// creation using C++.
//
#include "vtkActor.h"//required
#include "vtkCamera.h"// required
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"//required
#include "vtkRenderWindow.h"///required
#include "vtkRenderWindowInteractor.h"//required
#include "vtkRenderer.h"//required
int main()
{
// This creates a polygonal cylinder model with eight circumferential facets.
//
vtkCylinderSource* cylinder = vtkCylinderSource::New();
cylinder->SetResolution(8);
cylinder->SetHeight(11);
cylinder->SetRadius(2);
// 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.
//
vtkPolyDataMapper* cylinderMapper = vtkPolyDataMapper::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());// link the bulk with mapper.
// 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 -22.5 degrees.
vtkActor* cylinderActor = vtkActor::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(1.0000, 1, 0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// Create the graphics structure. The renderer renders into the
// render window. The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
//
vtkRenderer* ren1 = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
// Add the actors to the renderer, set the background and size
//
ren1->AddActor(cylinderActor);
ren1->SetBackground(1, 1, 0.4);
renWin->SetSize(300, 100);//window's backgroud size.
// We'll zoom in a little by accessing the camera and invoking a "Zoom"
// method on it.
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1);
renWin->Render();
// This starts the event loop and as a side effect causes an initial render.
iren->Start();
// Exiting from here, we have to delete all the instances that
// have been created.
//使用智能指针可以省略以下步骤
cylinder->Delete();
cylinderMapper->Delete();
cylinderActor->Delete();
ren1->Delete();
renWin->Delete();
iren->Delete();
return 0;
}
2. 贴上效果图
3.如何理解这段代码
- 首先我们定义一个素人A:圆柱体。她有位置,形状,大小等基本属性,但是注意她只是个素人,所以目前不存在任何化妆等的包装;
vtkCylinderSource* cylinder = vtkCylinderSource::New();
cylinder->SetResolution(8);
cylinder->SetHeight(11);
cylinder->SetRadius(2);
- 为了当上演员,素人A参加了一个比赛来作为进入圈的渠道,此后素人身份“映射为”练习生A;
vtkPolyDataMapper* cylinderMapper = vtkPolyDataMapper::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
- 定义一个演员空缺;
vtkActor* cylinderActor = vtkActor::New();
- 指定我们的练习生A来演;
cylinderActor->SetMapper(cylinderMapper);
- 练习生A成为演员A了,要开始初步包装了,包括颜色,形体等;
cylinderActor->GetProperty()->SetColor(1.0000, 1, 0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
- 为演员要专门新编一个剧,要布置一个舞台,还要找一个影院演该剧,找来观众看演员表演,并与观众互动提高名气。
vtkRenderer* ren1 = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);//舞台添加渲染包装,如打光,舞台背景等
1. 对演员的进一步包装包括在舞台上的打光vtkRenderer
,镜头焦点,舞台背景等(剧肯定是要巡演的,每次演出的舞台大小是不能被包装公司控制的,但是舞台背景包装公司可以自定义,以配合演出效果);
ren1->AddActor(cylinderActor);//添加需要对其进行打光和镜头对准的演员,
ren1->SetBackground(1, 1, 0.4);//设置剧本的背景
2. 为此次演出专门搭建的舞台vtkRenderWindow
,需要定义其大小;
renWin->SetSize(300, 100);//window's backgroud size.定义舞台的大小
3. 影院提供与观众互动机会vtkRenderWindowInteractor
,并添加该剧到播放列表;
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);//定义演哪部剧
// 以下三个是可有可无的画面调整
ren1->ResetCamera();
ren1->GetActiveCamera()->Zoom(1);
renWin->Render();
- 影院开始卖票,宣布启动。
iren->Start();
4. 抛开代码再来总结上述3
- 首先我们定义一个素人A:圆柱体。她有位置,形状,大小等基本属性,但是注意她只是个素人,所以目前不存在任何化妆等的包装;
- 为了当上演员,素人A参加了一个比赛来作为进入圈的渠道,此后素人身份“映射为”练习生A;
- 定义一个演员空缺;
- 指定我们的练习生A来演;
- 练习生A成为演员A了,要开始初步包装了,包括颜色,形体等;
- 为演员要专门新编一个剧,要布置一个舞台,还要找一个影院演剧,找来观众看演员表演,并与观众互动提高名气。
- 对演员的进一步包装包括在舞台上的打光,镜头焦点,舞台背景等(剧肯定是要巡演的,每次演出的舞台大小是不能被包装公司控制的,但是舞台背景包装公司可以自定义,以配合演出效果);
- 为此次演出专门搭建的舞台,需要定义其大小;
- 影院提供与观众互动机会并添加该剧到播放列表;
- 影院开始卖票,宣布启动。