我们使用 继承 vtkActor
,如果只是简单继承,发现,并不能正常显示,原因是它需要重写几个方法。
下面看一下 vtkActor 这个类几个方法:
//支持标准的渲染方法
virtual int RenderOpaqueGeometry(vtkViewport *viewport);//不透明的;
virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport);//透明的几何体;
virtual void Render(vtkRenderer *, vtkMapper *) {};//这使得这个actor将会被渲染,将会渲染actor's property, texture map and then mapper。property如果没有声明,则会自动创建一个。这个方法的运行将会导致流水线的更新
void ReleaseGraphicsResources(vtkWindow *);//释放由这个actor消耗的图形资源,参数window用来决定那些图形资源将要被释放
样例,我写了一个为了实现通过二个点来画一个 圆柱体的Actor
#ifndef hozCylinderActor_h
#define hozCylinderActor_h
#include "vtkActor.h"
#include "vtkCoordinate.h"
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
class hozCylinderActor: public vtkActor
{
public:
vtkTypeMacro(hozCylinderActor, vtkActor);
//void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Instantiate object.
*/
static hozCylinderActor* New();
public:
void SetPointStart(double x[3]) { this->SetPointStart(x[0], x[1], x[2]); }
void SetPointStart(double x, double y, double z);
double* GetPointStart();
void SetPointEnd(double x[3]) { this->SetPointEnd(x[0], x[1], x[2]); }
void SetPointEnd(double x, double y, double z);
double* GetPointEnd();
vtkSetClampMacro(Radius, double, 0.0, VTK_DOUBLE_MAX);
vtkGetMacro(Radius, double);
void Build();
//@{
/**
* Draw the
*/
virtual void Render(vtkRenderer *ren);
virtual void ReleaseGraphicsResources(vtkWindow *window);
int RenderOpaqueGeometry(vtkViewport* viewport) override;
int RenderTranslucentPolygonalGeometry(vtkViewport* viewport) override;
protected:
hozCylinderActor();
~hozCylinderActor();
protected:
vtkCoordinate* PointStartCoordinate;
vtkCoordinate* PointEndCoordinate;
double Radius; //minimum radius of tube
vtkSmartPointer<vtkPolyDataMapper> mMapper;
vtkActor* Device;
};
#endif
#include "hozCylinderActor.h"
#include <vtkRenderer.h>
#include <vtkLineSource.h>
#include <vtkTubeFilter.h>
#include <vtkTriangleFilter.h>
#include <vtkProperty.h>
#include <vtkTexture.h>
hozCylinderActor::hozCylinderActor()
{
this->PointStartCoordinate = vtkCoordinate::New();
this->PointStartCoordinate->SetCoordinateSystemToWorld();
this->PointStartCoordinate->SetValue(0.0, 0.0, 0.0);
this->PointEndCoordinate = vtkCoordinate::New();
this->PointEndCoordinate->SetCoordinateSystemToWorld();
this->PointEndCoordinate->SetValue(0.75, 0.0, 0.0);
mMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
this->Device = vtkActor::New();
}
hozCylinderActor::~hozCylinderActor()
{
this->Device->Delete();
}
void hozCylinderActor::SetPointStart(double x, double y, double z)
{
this->PointStartCoordinate->SetValue(x, y, z);
}
//---------------------------------------------------------------------------
void hozCylinderActor::SetPointEnd(double x, double y, double z)
{
this->PointEndCoordinate->SetValue(x, y, z);
}
//---------------------------------------------------------------------------
double* hozCylinderActor::GetPointStart()
{
return this->PointStartCoordinate->GetValue();
}
//---------------------------------------------------------------------------
double* hozCylinderActor::GetPointEnd()
{
return this->PointEndCoordinate->GetValue();
}
void hozCylinderActor::Build()
{
vtkSmartPointer<vtkLineSource> lineSource =
vtkSmartPointer<vtkLineSource>::New();
lineSource->SetPoint1(PointStartCoordinate->GetValue());
lineSource->SetPoint2(PointEndCoordinate->GetValue());
vtkSmartPointer<vtkTubeFilter> tubeFilter = vtkSmartPointer<vtkTubeFilter>::New();
tubeFilter->SetInputConnection(lineSource->GetOutputPort());
tubeFilter->SetRadius(Radius);
tubeFilter->SetNumberOfSides(50);
tubeFilter->CappingOn();
//Create a mapper and actor for the cylinder
mMapper->SetInputConnection(tubeFilter->GetOutputPort());
this->SetMapper(mMapper);
}
void hozCylinderActor::ReleaseGraphicsResources(vtkWindow *window) {
this->Superclass::ReleaseGraphicsResources(window);
}
int hozCylinderActor::RenderOpaqueGeometry(vtkViewport* viewport)
{
if (!this->Mapper) {
return 0;
}
if (!this->Property) {
this->GetProperty();
}
if (this->GetIsOpaque()) {
vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
this->Render(ren);
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Build the translucent poly actors and render.
//-----------------------------------------------------------------------------
int hozCylinderActor::RenderTranslucentPolygonalGeometry(vtkViewport* viewport)
{
if (!this->Mapper) {
return 0;
}
if (!this->Property) {
this->GetProperty();
}
if (!this->GetIsOpaque()) {
vtkRenderer *ren = static_cast<vtkRenderer *>(viewport);
this->Render(ren);
return 1;
}
return 0;
}
void hozCylinderActor::Render(vtkRenderer *ren) {
this->Property->Render(this, ren);
this->Device->SetProperty(this->Property);
this->Property->Render(this, ren);
if (this->BackfaceProperty) {
this->BackfaceProperty->BackfaceRender(this, ren);
this->Device->SetBackfaceProperty(this->BackfaceProperty);
}
if (this->Texture) {
this->Texture->Render(ren);
}
this->ComputeMatrix();
this->Device->SetUserMatrix(this->Matrix);
this->Device->Render(ren, this->Mapper);
}
vtkStandardNewMacro(hozCylinderActor);