C++ (VtkSDIView.h):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | // VtkSDIView.h : interface of the CVtkSDIView class // #pragma once // 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() const ; // Operations public : // Overrides public : virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected : virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); // Implementation public : 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 virtual void AssertValid() const ; virtual void Dump(CDumpContext& dc) const ; #endif protected : // Generated message map functions protected : afx_msg void OnFilePrintPreview(); afx_msg void OnRButtonUp( UINT nFlags, CPoint point); afx_msg void OnContextMenu(CWnd* pWnd, CPoint point); DECLARE_MESSAGE_MAP() public : afx_msg void OnSize( UINT nType, int cx, int cy); afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); virtual LRESULT WindowProc( UINT message, WPARAM wParam, LPARAM lParam); }; #ifndef _DEBUG // debug version in VtkSDIView.cpp inline CVtkSDIDoc* CVtkSDIView::GetDocument() const { return reinterpret_cast <CVtkSDIDoc*>(m_pDocument); } #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | // VtkSDIView.cpp : implementation of the CVtkSDIView class // #include "stdafx.h" // SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail // and search filter handlers and allows sharing of document code with that project. #ifndef SHARED_HANDLERS #include "VtkSDI.h" #endif #include "VtkSDIDoc.h" #include "VtkSDIView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CVtkSDIView IMPLEMENT_DYNCREATE(CVtkSDIView, CView) BEGIN_MESSAGE_MAP(CVtkSDIView, CView) // Standard printing commands ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CVtkSDIView::OnFilePrintPreview) ON_WM_CONTEXTMENU() ON_WM_RBUTTONUP() ON_WM_SIZE() ON_WM_ERASEBKGND() ON_WM_CREATE() END_MESSAGE_MAP() // CVtkSDIView construction/destruction 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(); } BOOL CVtkSDIView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } // CVtkSDIView drawing void CVtkSDIView::OnDraw(CDC* pDC) { CVtkSDIDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return ; 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(); } } // CVtkSDIView printing void CVtkSDIView::OnFilePrintPreview() { #ifndef SHARED_HANDLERS AFXPrintPreview( this ); #endif } BOOL CVtkSDIView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CVtkSDIView::OnBeginPrinting(CDC* /*pDC*/ , CPrintInfo* /*pInfo*/ ) { // TODO: add extra initialization before printing } void CVtkSDIView::OnEndPrinting(CDC* /*pDC*/ , CPrintInfo* /*pInfo*/ ) { // TODO: add cleanup after printing } void CVtkSDIView::OnRButtonUp( UINT /* nFlags */ , CPoint point) { ClientToScreen(&point); OnContextMenu( this , point); } void CVtkSDIView::OnContextMenu(CWnd* /* pWnd */ , CPoint point) { #ifndef SHARED_HANDLERS theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this , TRUE); #endif } // CVtkSDIView diagnostics #ifdef _DEBUG void CVtkSDIView::AssertValid() const { CView::AssertValid(); } void CVtkSDIView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CVtkSDIDoc* CVtkSDIView::GetDocument() const // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVtkSDIDoc))); return (CVtkSDIDoc*)m_pDocument; } #endif //_DEBUG // CVtkSDIView message handlers 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); return 0; } 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); } |
說明:
MFC使用VTK 5.10.0繪製3D模型範例。