-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadme
432 lines (399 loc) · 16.2 KB
/
Readme
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
Introduction:
This a simple vtk tutorial for newbies.
This tutorial includes 7 projects which have been tested in VS2017.
Of course, it is assumed that VTK is installed and the library path are
configured correctly. I install X64-version of VTK-8.1.
The first six tutorial is based the official tutorial in
Examples/Tutorial subdirectory, the last tutorial can be found
in the User Manual or Example/GUI subdirectory.
The is Readme pick up important points.
Refers:
https://www2.cs.uic.edu/~jbell/CS526/Tutorial/Sources.html
https://gitlab.kitware.com/vtk/vtk/tree/master/Examples
https://gitlab.kitware.com/vtk/vtk/tree/master/Examples/Tutorial
Tutorial:
+ basic pipeline,
source->mapper->actor->renderer->renderwindow.
+ basic headers,
#include"vtkConeSource.h" //different for different source
#include"vtkPolyDataMapper.h"
#include"vtkActor.h"
#include"vtkRender.h"
#include"vtkRenderWindow.h"
#include"vtkCamera.h"
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkInteractionStyle)
. The last four lines are necessary, alghough these four lines
does not appear in official tutorial.
. The camera provide view window through which we see the
rendered result.
+ minimum codes,
--------------------------------------------------------------------------------
// source
vtkConeSource *cone = vtkConeSource::New();
cone->SetHeight(3.0);
cone->SetRadius(1.0);
cone->SetResolution(10);
//mapper
vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());
//actor
vtkActor *coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);
//render
vtkRenderer *ren1 = vtkRenderer::New();
ren1->AddActor(coneActor);
ren1->SetBackground(0.1, 0.2, 0.4);
//render window
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
renWin->SetSize(300, 300);
//view it from camernt i;
for (i = 0; i < 360; ++i)
{
// render the image
renWin->Render();
// rotate the active camera by
// one degree
ren1->GetActiveCamera()->Azimuth(1);
}
//release resources
cone->Delete();
coneMapper->Delete();
coneActor->Delete();
ren1->Delete();
renWin->Delete();
--------------------------------------------------------------------------------
+ add callback for render
--------------------------------------------------------------------------------
#include<vtkCommands.h"
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New()
{
return new vtkMyCallback;
}
void Execute(vtkObject *caller, unsigned long, void*) override
{
vtkRenderer *renderer = reinterpret_cast<vtkRenderer*>(caller);
cout << renderer->GetActiveCamera()->GetPosition()[0] << " "
<< renderer->GetActiveCamera()->GetPosition()[1] << " "
<< renderer->GetActiveCamera()->GetPosition()[2] << "\n";
}
};
vtkMyCallback *mo1 = vtkMyCallback::New();
ren1->AddObserver(vtkCommand::StartEvent, mo1);
mo1->Delete();
--------------------------------------------------------------------------------
+ two renders with different viewpoints:
--------------------------------------------------------------------------------
vtkRenderer *ren1 = vtkRenderer::New();
ren1->AddActor(coneActor);
ren1->SetBackground(0.1, 0.2, 0.4);
ren1->SetViewport(0.0, 0.0, 0.5, 1.0);
ren1->ResetCamera();
ren1->GetActiveCamera()->Azimuth(180);
vtkRenderer *ren2 = vtkRenderer::New();
ren2->AddActor(coneActor);
ren2->SetBackground(0.2, 0.3, 0.5);
ren2->SetViewport(0.5, 0.0, 1.0, 1.0);
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
renWin->AddRenderer(ren2);
renWin->SetSize(600, 300);
for (i = 0; i < 360; ++i)
{
// render the image
renWin->Render();
// rotate the active camera by
// one degree
ren1->GetActiveCamera()->Azimuth(1);
}
--------------------------------------------------------------------------------
+ two actors with different properties
--------------------------------------------------------------------------------
#include"vtkProperty.h"
vtkActor *coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);
coneActor->GetProperty()->SetColor(0.2, 0.63, 0.79);
coneActor->GetProperty()->SetDiffuse(0.7);
coneActor->GetProperty()->SetSpecular(0.4);
coneActor->GetProperty()->SetSpecularPower(20);
vtkProperty *property = vtkProperty::New();
vtkActor *coneActor2 = vtkActor::New();
coneActor2->SetMapper(coneMapper);
coneActor2->SetProperty(property);
coneActor2->SetPosition(0, 2, 0);
property->SetColor(1.0, 0.3882, 0.2784);
property->SetDiffuse(0.7);
vtkRenderer *ren1 = vtkRenderer::New();
ren1->AddActor(coneActor);
ren1->AddActor(coneActor2);
ren1->SetBackground(0.1, 0.2, 0.4);
property->SetSpecular(0.4);
property->SetSpecularPower(20);
--------------------------------------------------------------------------------
+ interactors instead of limited camera
--------------------------------------------------------------------------------
#include "vtkRenderWindowInteractor.h"
vtkRenderWindowInteractor *iren =
vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
iren->Initialize();
iren->Start();
iren.Delete();
--------------------------------------------------------------------------------
+ interactors with different interaction, more control
--------------------------------------------------------------------------------
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
vtkRenderWindowInteractor *iren =
vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkInteractorStyleTrackballCamera *style =
vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
iren->Delete();
style->Delete();
--------------------------------------------------------------------------------
+ 3D widget
--------------------------------------------------------------------------------
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New()
{
return new vtkMyCallback;
}
void Execute(vtkObject *caller, unsigned long, void*) override
{
vtkTransform *t = vtkTransform::New();
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
widget->GetTransform(t);
widget->GetProp3D()->SetUserTransform(t);
t->Delete();
}
};
vtkBoxWidget *boxWidget = vtkBoxWidget::New();
boxWidget->SetInteractor(iren);
boxWidget->SetPlaceFactor(1.25);
boxWidget->SetProp3D(coneActor);
boxWidget->PlaceWidget();
vtkMyCallback *callback = vtkMyCallback::New();
boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
//
// Normally the user presses the "i" key to bring a 3D widget to life. Here
// we will manually enable it so it appears with the cone.
//
boxWidget->On();
--------------------------------------------------------------------------------
+ embed vtk into Winform created by Win32 API
NOTE: C++ variable has its life period, important of following
. static VTKCone *theVTKApp;
static make cone stays, you can also make a global one.
. pipelines as members to VTKCone object
. start render by,
renWin->Render();
instead of ordinary message loop pumped by VTKInteractor
. create window by APIs
--------------------------------------------------------------------------------
#include<windows.h>
#include"vtk_test.h"
static HANDLE hinst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR /* lpszCmdParam */, int nCmdShow)
{
static char szAppName[] = "Win32Cone";
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(nullptr,IDI_APPLICATION);
wndclass.hCursor = LoadCursor (nullptr, IDC_ARROW);
wndclass.lpszMenuName = nullptr;
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszClassName = szAppName;
RegisterClass (&wndclass);
}
hinst = hInstance;
hwnd = CreateWindow ( szAppName,
"Draw Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
480,
nullptr,
nullptr,
hInstance,
nullptr);
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, nullptr, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND ewin;
static VTKCone *theVTKApp;
switch (message)
{
case WM_CREATE:
{
ewin = CreateWindow("button","Exit",
WS_CHILD | WS_VISIBLE | SS_CENTER,
0,400,400,60,
hwnd,(HMENU)2,
(HINSTANCE)vtkGetWindowLong(hwnd,vtkGWL_HINSTANCE),
nullptr);
theVTKApp = new VTKCone(hwnd);
return 0;
}
case WM_COMMAND:
switch (wParam)
{
case 2:
PostQuitMessage (0);
delete theVTKApp;
theVTKApp = nullptr;
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage (0);
delete theVTKApp;
theVTKApp = nullptr;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
--------------------------------------------------------------------------------
. vtk pipe class
// vtk_test.h
--------------------------------------------------------------------------------
#pragma once
#include<windows.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New()
{
return new vtkMyCallback;
}
void Execute(vtkObject *caller, unsigned long, void*) override
{
vtkTransform *t = vtkTransform::New();
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
widget->GetTransform(t);
widget->GetProp3D()->SetUserTransform(t);
t->Delete();
}
};
class VTKCone
{
private:
vtkConeSource *cone;
vtkPolyDataMapper *coneMapper;
vtkActor *coneActor;
vtkRenderer *ren1;
vtkRenderWindow *renWin;
vtkRenderWindowInteractor *iren;
vtkInteractorStyleTrackballCamera *style;
vtkBoxWidget *boxWidget;
vtkMyCallback *callback;
public:
VTKCone(HWND hwnd);
~VTKCone();
private:
};
--------------------------------------------------------------------------------
.vtk_test class
--------------------------------------------------------------------------------
#include"vtk_test.h"
VTKCone::VTKCone(HWND hwnd)
{
cone = vtkConeSource::New();
cone->SetHeight(3.0);
cone->SetRadius(1.0);
cone->SetResolution(10);
coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());
coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);
ren1 = vtkRenderer::New();
ren1->AddActor(coneActor);
ren1->SetBackground(0.1, 0.2, 0.4);
renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
renWin->SetSize(400, 400);
// ---------------- import --------------
renWin->SetParentId(hwnd);
iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
style = vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
boxWidget = vtkBoxWidget::New();
boxWidget->SetInteractor(iren);
boxWidget->SetPlaceFactor(1.25);
boxWidget->SetProp3D(coneActor);
boxWidget->PlaceWidget();
callback = vtkMyCallback::New();
boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);
boxWidget->On();
renWin->Render();
}
VTKCone::~VTKCone()
{
if(cone)
cone->Delete();
if(coneMapper)
coneMapper->Delete();
if (coneActor)
coneActor->Delete();
if (callback)
callback->Delete();
if (boxWidget)
boxWidget->Delete();
if (ren1)
ren1->Delete();
if (renWin)
renWin->Delete();
if (iren)
iren->Delete();
if (style)
style->Delete();
}
--------------------------------------------------------------------------------