forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkWin32OpenGLRenderWindow.cxx
1757 lines (1577 loc) · 47.5 KB
/
vtkWin32OpenGLRenderWindow.cxx
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWin32OpenGLRenderWindow.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkIdList.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLError.h"
#include "vtkRendererCollection.h"
#include "vtkTypeTraits.h"
#include "vtkWin32RenderWindowInteractor.h"
#include <cmath>
#include <sstream>
#include "vtkOpenGL.h"
#include "vtkOpenGLError.h"
#include "vtkgl.h"
// Mouse wheel support
// In an ideal world we would just have to include <zmouse.h>, but it is not
// always available with all compilers/headers
#ifndef WM_MOUSEWHEEL
# define WM_MOUSEWHEEL 0x020A
#endif //WM_MOUSEWHEEL
vtkStandardNewMacro(vtkWin32OpenGLRenderWindow);
#define VTK_MAX_LIGHTS 8
vtkWin32OpenGLRenderWindow::vtkWin32OpenGLRenderWindow()
{
this->ApplicationInstance = NULL;
this->Palette = NULL;
this->ContextId = 0;
this->WindowId = 0;
this->ParentId = 0;
this->NextWindowId = 0;
this->DeviceContext = (HDC)0;
this->MFChandledWindow = FALSE;
this->StereoType = VTK_STEREO_CRYSTAL_EYES;
this->CursorHidden = 0;
this->Capabilities = 0;
this->ScreenDeviceContext = (HDC)0;
this->MemoryHdc = (HDC)0;
this->CreatingOffScreenWindow = 0;
this->WindowIdReferenceCount = 0;
this->ScreenMapped = this->Mapped;
this->ScreenWindowSize[0] = this->Size[0];
this->ScreenWindowSize[1] = this->Size[1];
this->ScreenDeviceContext = this->DeviceContext;
this->ScreenDoubleBuffer = this->DoubleBuffer;
this->ScreenContextId = this->ContextId;
}
vtkWin32OpenGLRenderWindow::~vtkWin32OpenGLRenderWindow()
{
this->Finalize();
vtkRenderer *ren;
vtkCollectionSimpleIterator rit;
this->Renderers->InitTraversal(rit);
while ((ren = this->Renderers->GetNextRenderer(rit)))
{
ren->SetRenderWindow(NULL);
}
delete[] this->Capabilities;
}
void vtkWin32OpenGLRenderWindow::Clean()
{
GLuint id;
/* finish OpenGL rendering */
if (this->OwnContext && this->ContextId)
{
this->MakeCurrent();
vtkOpenGLClearErrorMacro();
/* first delete all the old lights */
for (short cur_light = GL_LIGHT0; cur_light < GL_LIGHT0+VTK_MAX_LIGHTS; cur_light++)
{
glDisable((GLenum)cur_light);
}
/* now delete all textures */
glDisable(GL_TEXTURE_2D);
for (int i = 1; i < this->TextureResourceIds->GetNumberOfIds(); i++)
{
id = (GLuint)this->TextureResourceIds->GetId(i);
#ifdef GL_VERSION_1_1
if (glIsTexture(id))
{
glDeleteTextures(1, &id);
}
#else
if (glIsList(id))
{
glDeleteLists(id,1);
}
#endif
}
vtkOpenGLCheckErrorMacro("failed in Clean");
this->CleanUpRenderers();
// Note: wglMakeCurrent(NULL,NULL) is valid according to the documentation
// and works with nVidia and ATI but not with Intel. Passing an existing
// device context works in any case.
// see VTK Bug 7119.
if (wglMakeCurrent(this->DeviceContext,NULL)!=TRUE)
{
vtkErrorMacro("wglMakeCurrent failed in Clean(), error: " << GetLastError());
}
if (wglDeleteContext(this->ContextId) != TRUE)
{
vtkErrorMacro("wglDeleteContext failed in Clean(), error: " << GetLastError());
}
}
this->ContextId = NULL;
if (this->Palette)
{
SelectPalette(this->DeviceContext, this->OldPalette, FALSE); // SVA delete the old palette
DeleteObject(this->Palette);
this->Palette = NULL;
}
}
void vtkWin32OpenGLRenderWindow::CleanUpRenderers()
{
// tell each of the renderers that this render window/graphics context
// is being removed (the RendererCollection is removed by vtkRenderWindow's
// destructor)
vtkRenderer *ren;
vtkCollectionSimpleIterator rsit;
for (this->Renderers->InitTraversal(rsit);
(ren = this->Renderers->GetNextRenderer(rsit));)
{
ren->SetRenderWindow(NULL);
ren->SetRenderWindow(this);
}
}
LRESULT APIENTRY vtkWin32OpenGLRenderWindow::WndProc(HWND hWnd, UINT message,
WPARAM wParam,
LPARAM lParam)
{
LRESULT res;
vtkWin32OpenGLRenderWindow *me =
(vtkWin32OpenGLRenderWindow *)vtkGetWindowLong(hWnd, sizeof(vtkLONG));
if (me && me->GetReferenceCount()>0)
{
me->Register(me);
res = me->MessageProc(hWnd, message, wParam, lParam);
me->UnRegister(me);
}
else
{
res = DefWindowProc(hWnd, message, wParam, lParam);
}
return res;
}
void vtkWin32OpenGLRenderWindow::SetWindowName(const char * _arg)
{
vtkWindow::SetWindowName(_arg);
if (this->WindowId)
{
#ifdef UNICODE
wchar_t *wname = new wchar_t [mbstowcs(NULL, this->WindowName, 32000)+1];
mbstowcs(wname, this->WindowName, 32000);
SetWindowText(this->WindowId, wname);
delete [] wname;
#else
SetWindowText(this->WindowId, this->WindowName);
#endif
}
}
int vtkWin32OpenGLRenderWindow::GetEventPending()
{
MSG msg;
if (PeekMessage(&msg, this->WindowId, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE))
{
if (msg.message == WM_MOUSEMOVE)
{
PeekMessage(&msg, this->WindowId, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE);
}
if ((msg.message == WM_LBUTTONDOWN) ||
(msg.message == WM_RBUTTONDOWN) ||
(msg.message == WM_MBUTTONDOWN) ||
(msg.message == WM_MOUSEWHEEL))
{
return 1;
}
}
return 0;
}
// ----------------------------------------------------------------------------
bool vtkWin32OpenGLRenderWindow::InitializeFromCurrentContext()
{
HGLRC currentContext = wglGetCurrentContext();
if (currentContext != NULL)
{
this->SetWindowId(WindowFromDC(wglGetCurrentDC()));
this->SetDeviceContext(wglGetCurrentDC());
this->SetContextId(currentContext);
this->OpenGLInit();
this->OwnContext = 0;
return true;
}
return false;
}
// ----------------------------------------------------------------------------
void vtkWin32OpenGLRenderWindow::MakeCurrent()
{
// Try to avoid doing anything (for performance).
HGLRC current = wglGetCurrentContext();
if (this->ContextId != current)
{
if (this->IsPicking && current)
{
vtkErrorMacro("Attempting to call MakeCurrent for a different window"
" than the one doing the picking, this can causes crashes"
" and/or bad pick results");
}
else
{
if (wglMakeCurrent(this->DeviceContext, this->ContextId) != TRUE)
{
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
if (lpMsgBuf)
{
#ifdef UNICODE
wchar_t *wmsg = new wchar_t [mbstowcs(NULL, (const char*)lpMsgBuf, 32000)+1];
wchar_t *wtemp = new wchar_t [mbstowcs(NULL, "wglMakeCurrent failed in MakeCurrent(), error: ", 32000)+1];
mbstowcs(wmsg, (const char*)lpMsgBuf, 32000);
mbstowcs(wtemp, "wglMakeCurrent failed in MakeCurrent(), error: ", 32000);
vtkErrorMacro(<< wcscat(wtemp, wmsg));
delete [] wmsg;
delete [] wtemp;
#else
vtkErrorMacro("wglMakeCurrent failed in MakeCurrent(), error: "
<< (LPCTSTR)lpMsgBuf);
#endif
::LocalFree(lpMsgBuf);
}
}
}
}
}
// ----------------------------------------------------------------------------
// Description:
// Tells if this window is the current OpenGL context for the calling thread.
bool vtkWin32OpenGLRenderWindow::IsCurrent()
{
return this->ContextId != 0 && this->ContextId == wglGetCurrentContext();
}
// ----------------------------------------------------------------------------
void AdjustWindowRectForBorders(HWND hwnd, DWORD style, const int x, const int y,
const int width, const int height, RECT &r)
{
if (!style && hwnd)
{
style = GetWindowLong(hwnd, GWL_STYLE);
}
r.left = x;
r.top = y;
r.right = r.left + width;
r.bottom = r.top + height;
BOOL result = AdjustWindowRect(&r, style, FALSE);
if (!result)
{
vtkGenericWarningMacro("AdjustWindowRect failed, error: "
<< GetLastError());
}
}
// ----------------------------------------------------------------------------
void vtkWin32OpenGLRenderWindow::SetSize(int x, int y)
{
static int resizing = 0;
if ((this->Size[0] != x) || (this->Size[1] != y))
{
this->Superclass::SetSize(x, y);
if (this->Interactor)
{
this->Interactor->SetSize(x, y);
}
if (this->OffScreenRendering)
{
if (!this->CreatingOffScreenWindow)
{
if (!resizing)
{
resizing = 1;
this->CleanUpOffScreenRendering();
this->CreateOffScreenWindow(x,y);
resizing = 0;
}
}
}
else if (this->Mapped)
{
if (!resizing)
{
resizing = 1;
if (this->ParentId)
{
SetWindowExtEx(this->DeviceContext, x, y, NULL);
SetViewportExtEx(this->DeviceContext, x, y, NULL);
SetWindowPos(this->WindowId, HWND_TOP, 0, 0,
x, y, SWP_NOMOVE | SWP_NOZORDER);
}
else
{
RECT r;
AdjustWindowRectForBorders(this->WindowId, 0, 0, 0, x, y, r);
SetWindowPos(this->WindowId, HWND_TOP, 0, 0,
r.right - r.left,
r.bottom - r.top,
SWP_NOMOVE | SWP_NOZORDER);
}
resizing = 0;
}
}
}
}
void vtkWin32OpenGLRenderWindow::SetPosition(int x, int y)
{
static int resizing = 0;
if ((this->Position[0] != x) || (this->Position[1] != y))
{
this->Modified();
this->Position[0] = x;
this->Position[1] = y;
if (this->Mapped)
{
if (!resizing)
{
resizing = 1;
SetWindowPos(this->WindowId, HWND_TOP, x, y,
0, 0, SWP_NOSIZE | SWP_NOZORDER);
resizing = 0;
}
}
}
}
// End the rendering process and display the image.
void vtkWin32OpenGLRenderWindow::Frame(void)
{
this->MakeCurrent();
if (!this->AbortRender && this->DoubleBuffer && this->SwapBuffers)
{
// If this check is not enforced, we crash in offscreen rendering
if (this->DeviceContext)
{
// use global scope to get Win32 API SwapBuffers and not be
// confused with this->SwapBuffers
::SwapBuffers(this->DeviceContext);
vtkDebugMacro(<< " SwapBuffers\n");
}
}
else
{
glFlush();
}
}
int vtkWin32OpenGLRenderWindow::SupportsOpenGL()
{
MakeCurrent();
if (!this->DeviceContext)
{
return 0;
}
int pixelFormat = GetPixelFormat(this->DeviceContext);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(this->DeviceContext, pixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
return (pfd.dwFlags & PFD_SUPPORT_OPENGL) ? 1 : 0;
}
int vtkWin32OpenGLRenderWindow::IsDirect()
{
MakeCurrent();
if (!this->DeviceContext)
{
return 0;
}
int pixelFormat = GetPixelFormat(this->DeviceContext);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(this->DeviceContext, pixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
return (pfd.dwFlags & PFD_GENERIC_FORMAT) ? 0 : 1;
}
const char* vtkWin32OpenGLRenderWindow::ReportCapabilities()
{
MakeCurrent();
if (!this->DeviceContext)
{
return "no device context";
}
int pixelFormat = GetPixelFormat(this->DeviceContext);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(this->DeviceContext, pixelFormat,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
const char *glVendor = (const char*)glGetString(GL_VENDOR);
const char *glRenderer = (const char*)glGetString(GL_RENDERER);
const char *glVersion = (const char*)glGetString(GL_VERSION);
const char *glExtensions = (const char*)glGetString(GL_EXTENSIONS);
std::ostringstream strm;
strm << "OpenGL vendor string: " << glVendor << endl;
strm << "OpenGL renderer string: " << glRenderer << endl;
strm << "OpenGL version string: " << glVersion << endl;
strm << "OpenGL extensions: " << glExtensions << endl;
strm << "PixelFormat Descriptor:" << endl;
strm << "depth: " << static_cast<int>(pfd.cDepthBits) << endl;
if (pfd.cColorBits <= 8)
{
strm << "class: PseudoColor" << endl;
}
else
{
strm << "class: TrueColor" << endl;
}
strm << "buffer size: " << static_cast<int>(pfd.cColorBits) << endl;
strm << "level: " << static_cast<int>(pfd.bReserved) << endl;
if (pfd.iPixelType == PFD_TYPE_RGBA)
{
strm << "renderType: rgba" << endl;
}
else
{
strm <<"renderType: ci" << endl;
}
if (pfd.dwFlags & PFD_DOUBLEBUFFER) {
strm << "double buffer: True" << endl;
} else {
strm << "double buffer: False" << endl;
}
if (pfd.dwFlags & PFD_STEREO) {
strm << "stereo: True" << endl;
} else {
strm << "stereo: False" << endl;
}
if (pfd.dwFlags & PFD_GENERIC_FORMAT) {
strm << "hardware acceleration: False" << endl;
} else {
strm << "hardware acceleration: True" << endl;
}
strm << "rgba: redSize=" << static_cast<int>(pfd.cRedBits) <<
" greenSize=" << static_cast<int>(pfd.cGreenBits) <<
"blueSize=" << static_cast<int>(pfd.cBlueBits) <<
"alphaSize=" << static_cast<int>(pfd.cAlphaBits) << endl;
strm << "aux buffers: " << static_cast<int>(pfd.cAuxBuffers)<< endl;
strm << "depth size: " << static_cast<int>(pfd.cDepthBits) << endl;
strm << "stencil size: " << static_cast<int>(pfd.cStencilBits) << endl;
strm << "accum: redSize=" << static_cast<int>(pfd.cAccumRedBits) <<
" greenSize=" << static_cast<int>(pfd.cAccumGreenBits) <<
"blueSize=" << static_cast<int>(pfd.cAccumBlueBits) <<
"alphaSize=" << static_cast<int>(pfd.cAccumAlphaBits) << endl;
delete[] this->Capabilities;
size_t len = strm.str().length() + 1;
this->Capabilities = new char[len];
strncpy(this->Capabilities, strm.str().c_str(), len);
return this->Capabilities;
}
typedef bool (APIENTRY *wglChoosePixelFormatARBType)(HDC, const int*, const float*, unsigned int, int*, unsigned int*);
bool WGLisExtensionSupported(const char *extension)
{
const size_t extlen = strlen(extension);
const char *supported = NULL;
// Try To Use wglGetExtensionStringARB On Current DC, If Possible
PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
if (wglGetExtString)
{
supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
}
// If That Failed, Try Standard Opengl Extensions String
if (supported == NULL)
{
supported = (char*)glGetString(GL_EXTENSIONS);
}
// If That Failed Too, Must Be No Extensions Supported
if (supported == NULL)
{
return false;
}
// Begin Examination At Start Of String, Increment By 1 On False Match
for (const char* p = supported; ; p++)
{
// Advance p Up To The Next Possible Match
p = strstr(p, extension);
if (p == NULL)
{
return false; // No Match
}
// Make Sure That Match Is At The Start Of The String Or That
// The Previous Char Is A Space, Or Else We Could Accidentally
// Match "wglFunkywglExtension" With "wglExtension"
// Also, Make Sure That The Following Character Is Space Or NULL
// Or Else "wglExtensionTwo" Might Match "wglExtension"
if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' '))
{
return true; // Match
}
}
}
void vtkWin32OpenGLRenderWindow::SetupPixelFormat(HDC hDC, DWORD dwFlags,
int debug, int bpp,
int zbpp)
{
// Create a dummy window, needed for calling wglGetProcAddress.
#ifdef UNICODE
HWND tempId = CreateWindow(L"vtkOpenGL", 0, 0, 0, 0, 1, 1, 0, 0, this->ApplicationInstance, 0);
#else
HWND tempId = CreateWindow("vtkOpenGL", 0, 0, 0, 0, 1, 1, 0, 0, this->ApplicationInstance, 0);
#endif
HDC tempDC = GetDC(tempId);
PIXELFORMATDESCRIPTOR tempPfd;
memset(&tempPfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
tempPfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
tempPfd.nVersion = 1;
tempPfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
tempPfd.iPixelType = PFD_TYPE_RGBA;
int tempPixelFormat = ChoosePixelFormat(tempDC, &tempPfd);
SetPixelFormat(tempDC, tempPixelFormat, &tempPfd);
HGLRC tempContext = wglCreateContext(tempDC);
wglMakeCurrent(tempDC, tempContext);
// First we try to use the newer wglChoosePixelFormatARB which enables
// features like multisamples.
int pixelFormat = 0;
wglChoosePixelFormatARBType wglChoosePixelFormatARB =
reinterpret_cast<wglChoosePixelFormatARBType>(wglGetProcAddress("wglChoosePixelFormatARB"));
if ((dwFlags & PFD_DRAW_TO_WINDOW) && wglChoosePixelFormatARB)
{
int attrib[] = {
vtkwgl::ACCELERATION_ARB, vtkwgl::FULL_ACCELERATION_ARB,
vtkwgl::SUPPORT_OPENGL_ARB, TRUE,
vtkwgl::DRAW_TO_WINDOW_ARB, TRUE,
vtkwgl::DOUBLE_BUFFER_ARB, TRUE,
vtkwgl::COLOR_BITS_ARB, bpp/4*3,
vtkwgl::DEPTH_BITS_ARB, zbpp/4*3,
vtkwgl::PIXEL_TYPE_ARB, vtkwgl::TYPE_RGBA_ARB,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned int n = 14;
if (this->AlphaBitPlanes)
{
attrib[n] = vtkwgl::ALPHA_BITS_ARB;
attrib[n+1] = bpp/4;
n += 2;
}
if (this->StencilCapable)
{
attrib[n] = vtkwgl::STENCIL_BITS_ARB;
attrib[n+1] = 8;
n += 2;
}
if (dwFlags & PFD_STEREO)
{
attrib[n] = vtkwgl::STEREO_ARB;
attrib[n+1] = TRUE;
n += 2;
}
unsigned int multiSampleAttributeIndex = 0;
if (this->MultiSamples > 1 && WGLisExtensionSupported("WGL_ARB_multisample"))
{
attrib[n] = vtkwgl::SAMPLE_BUFFERS_ARB;
attrib[n+1] = 1;
attrib[n+2] = vtkwgl::SAMPLES_ARB;
attrib[n+3] = this->MultiSamples;
multiSampleAttributeIndex = n+3;
n += 4;
}
unsigned int numFormats;
if (!wglChoosePixelFormatARB(hDC, attrib, 0, 1, &pixelFormat, &numFormats))
{
// If the requested number of multisamples does not work, try
// scaling down the number of multisamples a few times.
if (multiSampleAttributeIndex)
{
attrib[multiSampleAttributeIndex] /= 2;
if (!wglChoosePixelFormatARB(hDC, attrib, 0, 1, &pixelFormat, &numFormats))
{
attrib[multiSampleAttributeIndex] /= 2;
wglChoosePixelFormatARB(hDC, attrib, 0, 1, &pixelFormat, &numFormats);
}
}
}
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(hDC, pixelFormat, sizeof(pfd), &pfd);
if (!SetPixelFormat(hDC, pixelFormat, &pfd))
{
pixelFormat = 0;
}
}
// Delete the dummy window
wglMakeCurrent(tempDC, 0);
wglDeleteContext(tempContext);
ReleaseDC(tempId, tempDC);
::DestroyWindow(tempId); // windows api
// If we got a valid pixel format in the process, we are done.
// Otherwise, we use the old approach of using ChoosePixelFormat.
if (pixelFormat)
{
return;
}
BYTE bpp_byte = static_cast<BYTE>(bpp);
BYTE zbpp_byte = static_cast<BYTE>(zbpp);
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), /* size */
1, /* version */
dwFlags , /* support double-buffering */
PFD_TYPE_RGBA, /* color type */
bpp_byte, /* preferred color depth */
0, 0, 0, 0, 0, 0, /* color bits (ignored) */
static_cast<BYTE>(this->AlphaBitPlanes ? bpp/4 : 0), /* no alpha buffer */
0, /* alpha bits (ignored) */
0, /* no accumulation buffer */
0, 0, 0, 0, /* accum bits (ignored) */
zbpp_byte, /* depth buffer */
static_cast<BYTE>(this->StencilCapable), /* stencil buffer */
0, /* no auxiliary buffers */
PFD_MAIN_PLANE, /* main layer */
0, /* reserved */
0, 0, 0, /* no layer, visible, damage masks */
};
// Only try to set pixel format if we do not currently have one
int currentPixelFormat = GetPixelFormat(hDC);
// if there is a current pixel format, then make sure it
// supports OpenGL
if (currentPixelFormat != 0)
{
DescribePixelFormat(hDC, currentPixelFormat, sizeof(pfd), &pfd);
if (!(pfd.dwFlags & PFD_SUPPORT_OPENGL))
{
#ifdef UNICODE
MessageBox(WindowFromDC(hDC),
L"Invalid pixel format, no OpenGL support",
L"Error",
MB_ICONERROR | MB_OK);
#else
MessageBox(WindowFromDC(hDC),
"Invalid pixel format, no OpenGL support",
"Error",
MB_ICONERROR | MB_OK);
#endif
if (this->HasObserver(vtkCommand::ExitEvent))
{
this->InvokeEvent(vtkCommand::ExitEvent, NULL);
return;
}
else
{
exit(1);
}
}
}
else
{
// hDC has no current PixelFormat, so
pixelFormat = ChoosePixelFormat(hDC, &pfd);
if (pixelFormat == 0)
{
#ifdef UNICODE
MessageBox(WindowFromDC(hDC), L"ChoosePixelFormat failed.", L"Error",
MB_ICONERROR | MB_OK);
#else
MessageBox(WindowFromDC(hDC), "ChoosePixelFormat failed.", "Error",
MB_ICONERROR | MB_OK);
#endif
if (this->HasObserver(vtkCommand::ExitEvent))
{
this->InvokeEvent(vtkCommand::ExitEvent, NULL);
return;
}
else
{
exit(1);
}
}
DescribePixelFormat(hDC, pixelFormat, sizeof(pfd), &pfd);
if (SetPixelFormat(hDC, pixelFormat, &pfd) != TRUE)
{
// int err = GetLastError();
#ifdef UNICODE
MessageBox(WindowFromDC(hDC), L"SetPixelFormat failed.", L"Error",
MB_ICONERROR | MB_OK);
#else
MessageBox(WindowFromDC(hDC), "SetPixelFormat failed.", "Error",
MB_ICONERROR | MB_OK);
#endif
if (this->HasObserver(vtkCommand::ExitEvent))
{
this->InvokeEvent(vtkCommand::ExitEvent, NULL);
return;
}
else
{
exit(1);
}
}
}
if (debug && (dwFlags & PFD_STEREO) && !(pfd.dwFlags & PFD_STEREO))
{
vtkGenericWarningMacro("No Stereo Available!");
this->StereoCapableWindow = 0;
}
}
void vtkWin32OpenGLRenderWindow::SetupPalette(HDC hDC)
{
int pixelFormat = GetPixelFormat(hDC);
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
int paletteSize;
if (pfd.dwFlags & PFD_NEED_PALETTE)
{
paletteSize = 1 << pfd.cColorBits;
}
else
{
return;
}
LOGPALETTE* pPal = (LOGPALETTE*)
malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
pPal->palVersion = 0x300;
pPal->palNumEntries = paletteSize;
// build a simple RGB color palette
int redMask = (1 << pfd.cRedBits) - 1;
int greenMask = (1 << pfd.cGreenBits) - 1;
int blueMask = (1 << pfd.cBlueBits) - 1;
for (int i = 0; i < paletteSize; ++i)
{
pPal->palPalEntry[i].peRed =
(((i >> pfd.cRedShift) & redMask) * 255) / redMask;
pPal->palPalEntry[i].peGreen =
(((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
pPal->palPalEntry[i].peBlue =
(((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
pPal->palPalEntry[i].peFlags = 0;
}
this->Palette = CreatePalette(pPal);
free(pPal);
if (this->Palette)
{
this->OldPalette = SelectPalette(hDC, this->Palette, FALSE);
RealizePalette(hDC);
}
}
LRESULT vtkWin32OpenGLRenderWindow::MessageProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
// nothing to be done here, opengl is initialized after the call to
// create now
return 0;
}
case WM_DESTROY:
this->Clean();
if (this->DeviceContext)
{
ReleaseDC(this->WindowId, this->DeviceContext);
this->DeviceContext = NULL;
this->WindowId = NULL;
}
return 0;
case WM_SIZE:
// track window size changes
if (this->ContextId)
{
this->SetSize((int)LOWORD(lParam),(int)HIWORD(lParam));
return 0;
}
case WM_PALETTECHANGED:
// realize palette if this is *not* the current window
if (this->ContextId && this->Palette && (HWND) wParam != hWnd)
{
SelectPalette(this->DeviceContext, this->OldPalette, FALSE);
UnrealizeObject(this->Palette);
this->OldPalette = SelectPalette(this->DeviceContext,
this->Palette, FALSE);
RealizePalette(this->DeviceContext);
this->Render();
}
break;
case WM_QUERYNEWPALETTE:
// realize palette if this is the current window
if (this->ContextId && this->Palette)
{
SelectPalette(this->DeviceContext, this->OldPalette, FALSE);
UnrealizeObject(this->Palette);
this->OldPalette = SelectPalette(this->DeviceContext,
this->Palette, FALSE);
RealizePalette(this->DeviceContext);
this->Render();
return TRUE;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
if (this->ContextId)
{
this->Render();
}
EndPaint(hWnd, &ps);
return 0;
}
break;
case WM_ERASEBKGND:
return TRUE;
case WM_SETCURSOR:
if (HTCLIENT == LOWORD(lParam))
{
this->SetCurrentCursor(this->GetCurrentCursor());
return TRUE;
}
break;
default:
this->InvokeEvent(vtkCommand::RenderWindowMessageEvent, &message);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void vtkWin32OpenGLRenderWindow::InitializeApplication()
{
// get the application instance if we don't have one already
if (!this->ApplicationInstance)
{
// if we have a parent window get the app instance from it
if (this->ParentId)
{
this->ApplicationInstance =
(HINSTANCE)vtkGetWindowLong(this->ParentId, vtkGWL_HINSTANCE);
}
else
{
this->ApplicationInstance = GetModuleHandle(NULL);
}
}
}
void vtkWin32OpenGLRenderWindow::CreateAWindow()
{
WNDCLASS wndClass;
// has the class been registered ?
#ifdef UNICODE
if (!GetClassInfo(this->ApplicationInstance, L"vtkOpenGL", &wndClass))
#else
if (!GetClassInfo(this->ApplicationInstance, "vtkOpenGL", &wndClass))
#endif
{
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
wndClass.lpfnWndProc = vtkWin32OpenGLRenderWindow::WndProc;
wndClass.cbClsExtra = 0;
wndClass.hInstance = this->ApplicationInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndClass.lpszMenuName = NULL;
#ifdef UNICODE
wndClass.lpszClassName = L"vtkOpenGL";
#else
wndClass.lpszClassName = "vtkOpenGL";
#endif
// vtk doesn't use the first extra vtkLONG's worth of bytes,
// but app writers may want them, so we provide them. VTK
// does use the second vtkLONG's worth of bytes of extra space.
wndClass.cbWndExtra = 2 * sizeof(vtkLONG);
RegisterClass(&wndClass);
}
if (this->WindowIdReferenceCount == 0)
{
if (!this->WindowId)
{
this->DeviceContext = 0;
// Check if the user has changed the window name
// ("Visualization toolkit - OpenGL" is vtkOpenGLRenderWindow's default)
if (!strcmp(this->GetWindowName(), "Visualization Toolkit - OpenGL"))
{
static int count = 1;
std::stringstream windowName;
windowName << "Visualization Toolkit - Win32OpenGL #" << count++;
this->SetWindowName(windowName.str().c_str());
}
#ifdef UNICODE
wchar_t *wname = new wchar_t [mbstowcs(NULL, this->WindowName, 32000)+1];
mbstowcs(wname, this->WindowName, 32000);
#endif
int x = ((this->Position[0] >= 0) ? this->Position[0] : 5);
int y = ((this->Position[1] >= 0) ? this->Position[1] : 5);
int height = ((this->Size[1] > 0) ? this->Size[1] : 300);
int width = ((this->Size[0] > 0) ? this->Size[0] : 300);
/* create window */
if (this->ParentId)
{
#ifdef UNICODE
this->WindowId = CreateWindow(
L"vtkOpenGL", wname,
WS_CHILD | WS_CLIPCHILDREN /*| WS_CLIPSIBLINGS*/,
x, y, width, height,
this->ParentId, NULL, this->ApplicationInstance, NULL);
#else
this->WindowId = CreateWindow(
"vtkOpenGL", this->WindowName,
WS_CHILD | WS_CLIPCHILDREN /*| WS_CLIPSIBLINGS*/,