先挂参考链接 https://blog.csdn.net/zgjorson/article/details/9132559

博主大体上是参照上面博客的,另外加了一些自己实现了功能,博客里面贴的有代码。

和参考博客有点儿不一样,上面博客使用的vtk5.2,博主使用的是vtk7(比起原博客注释了两行代码),另外博主添加了相机视角设置,OpenGL绘图也有区别。

直接贴代码好了,我们先从vtkProp继承一个类,然后自己在这个类中实现自己的OpenGL绘图

头文件(里面好多Include是可以不要的,如果你没有用的话,删掉是么问题的)

#pragma once
#include <vtkProp.h>
#include <vtkCamera.h>
#include <vtkType.h>
#include <vtkVersionMacros.h>
#include <vtkVersion.h>
#include <vtkViewport.h>
#include <vtkIndent.h>
#include <vtkObjectFactory.h>

#include <vtkResliceCursor.h>  
#include <vtkResliceCursorWidget.h>  

#include <vtkPlane.h>  
#include <vtkPlaneSource.h>  
#include <vtkPlaneWidget.h>  

#include <vtkImagePlaneWidget.h>  
#include <vtkResliceCursorThickLineRepresentation.h>  
#include <vtkResliceCursor.h>  

#include <vtkCommand.h>  
#include <vtkViewport.h>  
#include <vtkViewDependentErrorMetric.h>  

#include <vtkSmartPointer.h>  

#include <vtkRenderer.h>  
#include <vtkRendererSource.h>  
#include <vtkRenderingOpenGLModule.h>  
#include <vtkRenderWindow.h>  
#include <vtkWin32OpenGLRenderWindow.h>  
#include <vtkWin32RenderWindowInteractor.h> 

#include <vtkPolyVertex.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>

#include <vtkLookupTable.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>

class vtkMydProp :
	public vtkProp
{
public:
	vtkMydProp();
	~vtkMydProp();

	//vtkTypeRevisionMacro(vtkMydProp, vtkProp);
	void PrintSelf(ostream& os, vtkIndent indent);
	static vtkMydProp* New();
	int RenderVolumetricGeometry(vtkViewport *);
	//vtkCloud * pCloud;
	vtkCamera * RtCamera;

private:
	vtkMydProp(const vtkMydProp&);  // Not implemented.     
	void operator=(const vtkMydProp&);  // Not implemented.
};

类实现cpp文件

#include "stdafx.h"
#include "vtkMydProp.h"


vtkStandardNewMacro(vtkMydProp);

vtkMydProp::vtkMydProp()
{
	RtCamera = vtkCamera::New();
	//RtCamera->SetViewUp(0, 0, -1); // 视角位置
	RtCamera->SetPosition(0, 0, -10); // 观察位置
	//RtCamera->SetFocalPoint(0, 0, -10); // 设置焦点
	//RtCamera->ComputeViewPlaneNormal(); // 自动
}


vtkMydProp::~vtkMydProp()
{
}

void vtkMydProp::PrintSelf(ostream& os, vtkIndent indent){
	
}

int vtkMydProp::RenderVolumetricGeometry(vtkViewport *){
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
	glBegin(GL_QUADS); {
		// Top Side
		glVertex3f(1.0f, 1.0f, 1.0f);
		glVertex3f(1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		// Bottom Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(1.0f, -1.0f, -1.0f);
		glVertex3f(1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		// Front Side
		glVertex3f(1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(1.0f, -1.0f, 1.0f);
		// Back Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(1.0f, 1.0f, -1.0f);
		glVertex3f(1.0f, -1.0f, -1.0f);
		// Left Side
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		// Right Side
		glVertex3f(1.0f, 1.0f, 1.0f);
		glVertex3f(1.0f, -1.0f, 1.0f);
		glVertex3f(1.0f, -1.0f, -1.0f);
		glVertex3f(1.0f, 1.0f, -1.0f);
	} glEnd();
	
	return 1;
}

添加好上面的类后,我们就可以在vtk绘图窗口调用了

	vtkMydProp *mydprop;
	mydprop = vtkMydProp::New();

	m_Renderer->RemoveViewProp(mydprop);
	m_Renderer->AddViewProp(mydprop);
	m_Renderer->SetBackground(0, 0, 0);
	m_Renderer->SetActiveCamera(mydprop->RtCamera);
	m_RenderWindow->Render();

	return;

这样就可以了

 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注