VTK + MFC Single Document

其实很弱的,VTK自带的源代码里就有这个文档,CHM格式的。

vtkSIDIView.h
//Additional or changed code is indicated in bold:

// Include the required header files for the vtk classes we are using

#include
<vtkRenderer.h>

#include
<vtkWin32OpenGLRenderWindow.h>

#include
<vtkWin32RenderWindowInteractor.h>



#include
<vtkSphereSource.h>

#include
<vtkConeSource.h>

#include
<vtkGlyph3D.h>

#include
<vtkElevationFilter.h>

#include
<vtkPolyDataMapper.h>

#include
<vtkActor.h>

#include
<vtkCubeAxesActor2D.h>



class CVtkSDIView : public CView

{

protected: // create from serialization only

CVtkSDIView();

DECLARE_DYNCREATE(CVtkSDIView)



// Attributes

public:

CVtkSDIDoc
* GetDocument();



// Operations

public:



// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CVtkSDIView)

public:

virtualvoid OnDraw(CDC* pDC); // overridden to draw this view

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

protected:

virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

virtualvoid OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

virtualvoid OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL



// Implementation

private:

void Pipeline ( void );

virtual~CVtkSDIView();



vtkWin32OpenGLRenderWindow
*renWin;

vtkRenderer
*ren;

vtkWin32RenderWindowInteractor
*iren;



vtkSphereSource
*sphere;

vtkPolyDataMapper
*sphereMapper;

vtkElevationFilter
*sphereElevation;

vtkActor
*sphereActor;

vtkConeSource
*cone;

vtkGlyph3D
*glyph;

vtkPolyDataMapper
*spikeMapper;

vtkActor
*spikeActor;

vtkCubeAxesActor2D
*sphereAxis;





#ifdef _DEBUG

virtualvoid AssertValid() const;

virtualvoid Dump(CDumpContext& dc) const;

#endif



protected:



// Generated message map functions

protected:

//{{AFX_MSG(CVtkSDIView)

afx_msg
void OnSize(UINT nType, int cx, int cy);

afx_msg BOOL OnEraseBkgnd(CDC
* pDC);

afx_msg
int OnCreate(LPCREATESTRUCT lpCreateStruct);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};
vtkSIDIView.cpp
//Additional or changed code is indicated in bold:

CVtkSDIView::CVtkSDIView()

{

// Create the the renderer, window and interactor objects.

this->ren = vtkRenderer::New();

this->renWin = vtkWin32OpenGLRenderWindow::New();

this->iren = vtkWin32RenderWindowInteractor::New();



// Create the the objects used to form the visualisation.

this->sphere = vtkSphereSource::New();

this->sphereElevation = vtkElevationFilter::New();

this->sphereMapper = vtkPolyDataMapper::New();

this->sphereActor = vtkActor::New();

this->cone = vtkConeSource::New();

this->glyph = vtkGlyph3D::New();

this->spikeMapper = vtkPolyDataMapper::New();

this->spikeActor = vtkActor::New();

this->sphereAxis = vtkCubeAxesActor2D::New();



}



CVtkSDIView::
~CVtkSDIView()

{

// Delete the the renderer, window and interactor objects.

this->ren->Delete();

this->iren->Delete();

this->renWin->Delete();



// Delete the the objects used to form the visualisation.

this->sphere->Delete();

this->sphereElevation->Delete();

this->sphereMapper->Delete();

this->sphereActor->Delete();

this->cone->Delete();

this->glyph->Delete();

this->spikeMapper->Delete();

this->spikeActor->Delete();

this->sphereAxis->Delete();

}



void CVtkSDIView::OnDraw(CDC* pDC)

{

CVtkMDIDoc
* pDoc = GetDocument();

ASSERT_VALID(pDoc);



if ( !this->iren->GetInitialized() )

{

CRect rect;



this->GetClientRect(&rect);

this->iren->Initialize();

this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);



this->ren->ResetCamera();



}



// Invoke the pipeline

Pipeline();



if ( pDC->IsPrinting() )

{

this->BeginWaitCursor();



// Obtain the size of the printer page in pixels.

int cxPage = pDC->GetDeviceCaps(HORZRES);

int cyPage = pDC->GetDeviceCaps(VERTRES);



// Get the size of the window in pixels.

int*size =this->renWin->GetSize();

int cxWindow = size[0];

int cyWindow = size[1];

float fx =float(cxPage) /float(cxWindow);

float fy =float(cyPage) /float(cyWindow);

float scale = min(fx,fy);

int x =int(scale *float(cxWindow));

int y =int(scale *float(cyWindow));

this->renWin->SetupMemoryRendering(cxWindow, cyWindow, pDC->GetSafeHdc());

this->renWin->Render();

HDC memDC
=this->renWin->GetMemoryDC();

StretchBlt(pDC
->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow,cyWindow,SRCCOPY);

this->renWin->ResumeScreenRendering();



this->EndWaitCursor();



}

else

{

this->renWin->Render();

}

}



void CVtkSDIView::OnSize(UINT nType, int cx, int cy)

{

CView::OnSize(nType, cx, cy);



CRect rect;

this->GetClientRect(&rect);

this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);



}



BOOL CVtkSDIView::OnEraseBkgnd(CDC
* pDC)

{

return TRUE;

}



int CVtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CView::OnCreate(lpCreateStruct) ==-1)

return-1;



this->renWin->AddRenderer(this->ren);

// setup the parent window

this->renWin->SetParentId(this->m_hWnd);

this->iren->SetRenderWindow(this->renWin);



return0;

}



LRESULT CVtkSDIView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

//case WM_PAINT:

case WM_LBUTTONDOWN:

case WM_LBUTTONUP:

case WM_MBUTTONDOWN:

case WM_MBUTTONUP:

case WM_RBUTTONDOWN:

case WM_RBUTTONUP:

case WM_MOUSEMOVE:

case WM_CHAR:

case WM_TIMER:

if (this->iren->GetInitialized())

{

return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam, this->iren);

}

break;

}



return CView::WindowProc(message, wParam, lParam);

}





void CVtkSDIView::Pipeline()

{

// Construct the sphere

this->sphere->SetRadius(1);

this->sphere->SetThetaResolution(18);

this->sphere->SetPhiResolution(18);

this->sphere->LatLongTessellationOn();

// Generate elevations

this->sphereElevation->SetInput(this->sphere->GetOutput());

this->sphereElevation->SetLowPoint(0,0,-1);

this->sphereElevation->SetHighPoint(0,0,1);

this->sphereElevation->SetScalarRange(-1,1);

// Link the mapper

this->sphereMapper->SetInput(this->sphereElevation->GetPolyDataOutput());

this->sphereMapper->SetColorModeToMapScalars();

this->sphereMapper->SetScalarRange(-1,1);

// Link the actor

this->sphereActor->SetMapper(this->sphereMapper);

// Add it to the renderer

this->ren->AddActor(this->sphereActor);



// Construct the cone

this->cone->SetResolution(8);



// Construct the glyphs on the spherical surface

this->glyph->SetInput(this->sphere->GetOutput());

this->glyph->SetSource(this->cone->GetOutput());

this->glyph->SetVectorModeToUseNormal();

this->glyph->SetScaleModeToScaleByVector();

this->glyph->SetScaleFactor(0.1);

// Link the mapper to the glyph

this->spikeMapper->SetInput(this->glyph->GetOutput());

// Link the actor

this->spikeActor->SetMapper(this->spikeMapper);

// Add it to the renderer

this->ren->AddActor(this->spikeActor);



// Add in the cube axis actor

this->sphereAxis->SetInput(this->sphereElevation->GetOutput());

this->sphereAxis->SetCamera(this->ren->GetActiveCamera());

// Add it to the renderer

this->ren->AddActor(this->sphereAxis);

}

原文地址:https://www.cnblogs.com/unsigned/p/1746727.html