]>
Commit | Line | Data |
---|---|---|
19cf4f80 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: glcanvas.cpp | |
3 | // Purpose: wxGLCanvas, for using OpenGL with wxWindows under MS Windows | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
0122b7e3 | 9 | // Licence: wxWindows licence |
19cf4f80 RD |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "glcanvas.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #if defined(__BORLANDC__) | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #include <wx/setup.h> | |
23 | ||
24 | ||
8a721b35 | 25 | #undef wxUSE_GLCANVAS |
19cf4f80 RD |
26 | #define wxUSE_GLCANVAS 1 |
27 | #if wxUSE_GLCANVAS | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include <wx/frame.h> | |
31 | #endif | |
32 | ||
33 | #include <wx/msw/private.h> | |
34 | #include <wx/settings.h> | |
35 | #include <wx/log.h> | |
36 | ||
37 | #include "myglcanvas.h" | |
38 | ||
0122b7e3 RD |
39 | static const wxChar *wxGLCanvasClassName = wxT("wxGLCanvasClass"); |
40 | static const wxChar *wxGLCanvasClassNameNoRedraw = wxT("wxGLCanvasClassNR"); | |
19cf4f80 RD |
41 | |
42 | LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, | |
43 | WPARAM wParam, LPARAM lParam); | |
44 | ||
45 | /* | |
46 | * GLContext implementation | |
47 | */ | |
48 | ||
49 | wxGLContext::wxGLContext(bool isRGB, wxGLCanvas *win, const wxPalette& palette) | |
50 | { | |
51 | m_window = win; | |
52 | ||
53 | m_hDC = win->GetHDC(); | |
54 | ||
55 | m_glContext = wglCreateContext((HDC) m_hDC); | |
56 | wxCHECK_RET( m_glContext, wxT("Couldn't create OpenGl context") ); | |
57 | ||
58 | wglMakeCurrent((HDC) m_hDC, m_glContext); | |
59 | } | |
60 | ||
61 | wxGLContext::wxGLContext( | |
62 | bool isRGB, wxGLCanvas *win, | |
63 | const wxPalette& palette, | |
64 | const wxGLContext *other /* for sharing display lists */ | |
65 | ) | |
66 | { | |
67 | m_window = win; | |
68 | ||
69 | m_hDC = win->GetHDC(); | |
70 | ||
71 | m_glContext = wglCreateContext((HDC) m_hDC); | |
72 | wxCHECK_RET( m_glContext, wxT("Couldn't create OpenGl context") ); | |
73 | ||
74 | if( other != 0 ) | |
75 | wglShareLists( other->m_glContext, m_glContext ); | |
76 | ||
77 | wglMakeCurrent((HDC) m_hDC, m_glContext); | |
78 | } | |
79 | ||
80 | wxGLContext::~wxGLContext() | |
81 | { | |
82 | if (m_glContext) | |
83 | { | |
84 | wglMakeCurrent(NULL, NULL); | |
85 | wglDeleteContext(m_glContext); | |
86 | } | |
87 | } | |
88 | ||
89 | void wxGLContext::SwapBuffers() | |
90 | { | |
91 | if (m_glContext) | |
92 | { | |
93 | wglMakeCurrent((HDC) m_hDC, m_glContext); | |
94 | ::SwapBuffers((HDC) m_hDC); //blits the backbuffer into DC | |
95 | } | |
96 | } | |
97 | ||
98 | void wxGLContext::SetCurrent() | |
99 | { | |
100 | if (m_glContext) | |
101 | { | |
102 | wglMakeCurrent((HDC) m_hDC, m_glContext); | |
103 | } | |
104 | ||
105 | /* | |
106 | setupPixelFormat(hDC); | |
107 | setupPalette(hDC); | |
108 | */ | |
109 | } | |
110 | ||
111 | void wxGLContext::SetColour(const char *colour) | |
112 | { | |
113 | float r = 0.0; | |
114 | float g = 0.0; | |
115 | float b = 0.0; | |
116 | wxColour *col = wxTheColourDatabase->FindColour(colour); | |
117 | if (col) | |
118 | { | |
119 | r = (float)(col->Red()/256.0); | |
120 | g = (float)(col->Green()/256.0); | |
121 | b = (float)(col->Blue()/256.0); | |
122 | glColor3f( r, g, b); | |
123 | } | |
124 | } | |
125 | ||
126 | ||
127 | /* | |
128 | * wxGLCanvas implementation | |
129 | */ | |
130 | ||
ab11ebfa | 131 | IMPLEMENT_CLASS(wxGLCanvas, wxWindow) |
19cf4f80 | 132 | |
ab11ebfa | 133 | BEGIN_EVENT_TABLE(wxGLCanvas, wxWindow) |
19cf4f80 RD |
134 | EVT_SIZE(wxGLCanvas::OnSize) |
135 | EVT_PALETTE_CHANGED(wxGLCanvas::OnPaletteChanged) | |
136 | EVT_QUERY_NEW_PALETTE(wxGLCanvas::OnQueryNewPalette) | |
137 | END_EVENT_TABLE() | |
138 | ||
139 | wxGLCanvas::wxGLCanvas(wxWindow *parent, wxWindowID id, | |
140 | const wxPoint& pos, const wxSize& size, long style, const wxString& name, | |
ab11ebfa | 141 | int *attribList, const wxPalette& palette) : wxWindow() |
19cf4f80 RD |
142 | { |
143 | m_glContext = (wxGLContext*) NULL; | |
144 | ||
145 | bool ret = Create(parent, id, pos, size, style, name); | |
146 | ||
147 | if ( ret ) | |
148 | { | |
8a053704 VS |
149 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
150 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
19cf4f80 RD |
151 | } |
152 | ||
153 | m_hDC = (WXHDC) ::GetDC((HWND) GetHWND()); | |
154 | ||
155 | SetupPixelFormat(attribList); | |
156 | SetupPalette(palette); | |
157 | ||
158 | m_glContext = new wxGLContext(TRUE, this, palette); | |
159 | } | |
160 | ||
161 | wxGLCanvas::wxGLCanvas( wxWindow *parent, | |
162 | const wxGLContext *shared, wxWindowID id, | |
163 | const wxPoint& pos, const wxSize& size, long style, const wxString& name, | |
164 | int *attribList, const wxPalette& palette ) | |
ab11ebfa | 165 | : wxWindow() |
19cf4f80 RD |
166 | { |
167 | m_glContext = (wxGLContext*) NULL; | |
168 | ||
169 | bool ret = Create(parent, id, pos, size, style, name); | |
170 | ||
171 | if ( ret ) | |
172 | { | |
8a053704 VS |
173 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
174 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
19cf4f80 RD |
175 | } |
176 | ||
177 | m_hDC = (WXHDC) ::GetDC((HWND) GetHWND()); | |
178 | ||
179 | SetupPixelFormat(attribList); | |
180 | SetupPalette(palette); | |
181 | ||
182 | m_glContext = new wxGLContext(TRUE, this, palette, shared ); | |
183 | } | |
184 | ||
185 | // Not very useful for wxMSW, but this is to be wxGTK compliant | |
186 | ||
187 | wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID id, | |
188 | const wxPoint& pos, const wxSize& size, long style, const wxString& name, | |
189 | int *attribList, const wxPalette& palette ): | |
ab11ebfa | 190 | wxWindow() |
19cf4f80 RD |
191 | { |
192 | m_glContext = (wxGLContext*) NULL; | |
193 | ||
194 | bool ret = Create(parent, id, pos, size, style, name); | |
195 | ||
196 | if ( ret ) | |
197 | { | |
8a053704 VS |
198 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
199 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
19cf4f80 RD |
200 | } |
201 | ||
202 | m_hDC = (WXHDC) ::GetDC((HWND) GetHWND()); | |
203 | ||
204 | SetupPixelFormat(attribList); | |
205 | SetupPalette(palette); | |
206 | ||
207 | wxGLContext *sharedContext=0; | |
208 | if (shared) sharedContext=shared->GetContext(); | |
209 | m_glContext = new wxGLContext(TRUE, this, palette, sharedContext ); | |
210 | } | |
211 | ||
212 | wxGLCanvas::~wxGLCanvas() | |
213 | { | |
214 | if (m_glContext) | |
215 | delete m_glContext; | |
216 | ||
217 | ::ReleaseDC((HWND) GetHWND(), (HDC) m_hDC); | |
218 | } | |
219 | ||
0122b7e3 RD |
220 | // Replaces wxWindow::Create functionality, since we need to use a different |
221 | // window class | |
222 | bool wxGLCanvas::Create(wxWindow *parent, | |
223 | wxWindowID id, | |
224 | const wxPoint& pos, | |
225 | const wxSize& size, | |
226 | long style, | |
227 | const wxString& name) | |
19cf4f80 | 228 | { |
0122b7e3 | 229 | static bool s_registeredGLCanvasClass = FALSE; |
19cf4f80 RD |
230 | |
231 | // We have to register a special window class because we need | |
232 | // the CS_OWNDC style for GLCanvas. | |
233 | ||
234 | /* | |
235 | From Angel Popov <jumpo@bitex.com> | |
236 | ||
237 | Here are two snips from a dicussion in the OpenGL Gamedev list that explains | |
238 | how this problem can be fixed: | |
239 | ||
240 | "There are 5 common DCs available in Win95. These are aquired when you call | |
241 | GetDC or GetDCEx from a window that does _not_ have the OWNDC flag. | |
242 | OWNDC flagged windows do not get their DC from the common DC pool, the issue | |
243 | is they require 800 bytes each from the limited 64Kb local heap for GDI." | |
244 | ||
245 | "The deal is, if you hold onto one of the 5 shared DC's too long (as GL apps | |
246 | do), Win95 will actually "steal" it from you. MakeCurrent fails, | |
247 | apparently, because Windows re-assigns the HDC to a different window. The | |
248 | only way to prevent this, the only reliable means, is to set CS_OWNDC." | |
249 | */ | |
250 | ||
0122b7e3 | 251 | if (!s_registeredGLCanvasClass) |
19cf4f80 RD |
252 | { |
253 | WNDCLASS wndclass; | |
254 | ||
19cf4f80 RD |
255 | // the fields which are common to all classes |
256 | wndclass.lpfnWndProc = (WNDPROC)wxWndProc; | |
257 | wndclass.cbClsExtra = 0; | |
258 | wndclass.cbWndExtra = sizeof( DWORD ); // VZ: what is this DWORD used for? | |
259 | wndclass.hInstance = wxhInstance; | |
260 | wndclass.hIcon = (HICON) NULL; | |
261 | wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW); | |
262 | wndclass.lpszMenuName = NULL; | |
263 | ||
264 | // Register the GLCanvas class name | |
265 | wndclass.hbrBackground = (HBRUSH)NULL; | |
266 | wndclass.lpszClassName = wxGLCanvasClassName; | |
0122b7e3 | 267 | wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC; |
19cf4f80 | 268 | |
0122b7e3 | 269 | if ( !::RegisterClass(&wndclass) ) |
19cf4f80 RD |
270 | { |
271 | wxLogLastError(wxT("RegisterClass(wxGLCanvasClass)")); | |
272 | return FALSE; | |
273 | } | |
274 | ||
0122b7e3 RD |
275 | // Register the GLCanvas class name for windows which don't do full repaint |
276 | // on resize | |
277 | wndclass.lpszClassName = wxGLCanvasClassNameNoRedraw; | |
278 | wndclass.style &= ~(CS_HREDRAW | CS_VREDRAW); | |
279 | ||
280 | if ( !::RegisterClass(&wndclass) ) | |
281 | { | |
282 | wxLogLastError(wxT("RegisterClass(wxGLCanvasClassNameNoRedraw)")); | |
283 | ||
284 | ::UnregisterClass(wxGLCanvasClassName, wxhInstance); | |
285 | ||
286 | return FALSE; | |
287 | } | |
288 | ||
289 | s_registeredGLCanvasClass = TRUE; | |
19cf4f80 RD |
290 | } |
291 | ||
292 | wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindow without parent") ); | |
293 | ||
294 | if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
295 | return FALSE; | |
296 | ||
297 | parent->AddChild(this); | |
298 | ||
299 | DWORD msflags = 0; | |
300 | if ( style & wxBORDER ) | |
301 | msflags |= WS_BORDER; | |
302 | if ( style & wxTHICK_FRAME ) | |
303 | msflags |= WS_THICKFRAME; | |
304 | ||
305 | /* | |
306 | A general rule with OpenGL and Win32 is that any window that will have a | |
307 | HGLRC built for it must have two flags: WS_CLIPCHILDREN & WS_CLIPSIBLINGS. | |
308 | You can find references about this within the knowledge base and most OpenGL | |
309 | books that contain the wgl function descriptions. | |
310 | */ | |
311 | ||
0122b7e3 | 312 | msflags |= WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; |
19cf4f80 RD |
313 | |
314 | bool want3D; | |
315 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); | |
316 | ||
317 | // Even with extended styles, need to combine with WS_BORDER | |
318 | // for them to look right. | |
319 | if ( want3D || (m_windowStyle & wxSIMPLE_BORDER) || (m_windowStyle & wxRAISED_BORDER ) || | |
320 | (m_windowStyle & wxSUNKEN_BORDER) || (m_windowStyle & wxDOUBLE_BORDER)) | |
321 | { | |
322 | msflags |= WS_BORDER; | |
323 | } | |
324 | ||
0122b7e3 | 325 | return MSWCreate(wxGLCanvasClassName, NULL, pos, size, msflags, exStyle); |
19cf4f80 RD |
326 | } |
327 | ||
0122b7e3 | 328 | static void AdjustPFDForAttributes(PIXELFORMATDESCRIPTOR& pfd, int *attribList) |
19cf4f80 | 329 | { |
19cf4f80 RD |
330 | if (attribList) { |
331 | pfd.dwFlags &= ~PFD_DOUBLEBUFFER; | |
332 | pfd.iPixelType = PFD_TYPE_COLORINDEX; | |
333 | pfd.cColorBits = 0; | |
334 | int arg=0; | |
335 | ||
336 | while( (attribList[arg]!=0) ) | |
337 | { | |
338 | switch( attribList[arg++] ) | |
339 | { | |
340 | case WX_GL_RGBA: | |
341 | pfd.iPixelType = PFD_TYPE_RGBA; | |
342 | break; | |
343 | case WX_GL_BUFFER_SIZE: | |
344 | pfd.cColorBits = attribList[arg++]; | |
345 | break; | |
346 | case WX_GL_LEVEL: | |
347 | // this member looks like it may be obsolete | |
348 | if (attribList[arg] > 0) { | |
f54a35fe | 349 | pfd.iLayerType = (BYTE)PFD_OVERLAY_PLANE; |
19cf4f80 | 350 | } else if (attribList[arg] < 0) { |
f54a35fe | 351 | pfd.iLayerType = (BYTE)PFD_UNDERLAY_PLANE; |
19cf4f80 | 352 | } else { |
f54a35fe | 353 | pfd.iLayerType = (BYTE)PFD_MAIN_PLANE; |
19cf4f80 RD |
354 | } |
355 | arg++; | |
356 | break; | |
357 | case WX_GL_DOUBLEBUFFER: | |
358 | pfd.dwFlags |= PFD_DOUBLEBUFFER; | |
359 | break; | |
360 | case WX_GL_STEREO: | |
361 | pfd.dwFlags |= PFD_STEREO; | |
362 | break; | |
363 | case WX_GL_AUX_BUFFERS: | |
364 | pfd.cAuxBuffers = attribList[arg++]; | |
365 | break; | |
366 | case WX_GL_MIN_RED: | |
367 | pfd.cColorBits += (pfd.cRedBits = attribList[arg++]); | |
368 | break; | |
369 | case WX_GL_MIN_GREEN: | |
370 | pfd.cColorBits += (pfd.cGreenBits = attribList[arg++]); | |
371 | break; | |
372 | case WX_GL_MIN_BLUE: | |
373 | pfd.cColorBits += (pfd.cBlueBits = attribList[arg++]); | |
374 | break; | |
375 | case WX_GL_MIN_ALPHA: | |
376 | // doesn't count in cColorBits | |
377 | pfd.cAlphaBits = attribList[arg++]; | |
378 | break; | |
379 | case WX_GL_DEPTH_SIZE: | |
380 | pfd.cDepthBits = attribList[arg++]; | |
381 | break; | |
382 | case WX_GL_STENCIL_SIZE: | |
383 | pfd.cStencilBits = attribList[arg++]; | |
384 | break; | |
385 | case WX_GL_MIN_ACCUM_RED: | |
386 | pfd.cAccumBits += (pfd.cAccumRedBits = attribList[arg++]); | |
387 | break; | |
388 | case WX_GL_MIN_ACCUM_GREEN: | |
389 | pfd.cAccumBits += (pfd.cAccumGreenBits = attribList[arg++]); | |
390 | break; | |
391 | case WX_GL_MIN_ACCUM_BLUE: | |
392 | pfd.cAccumBits += (pfd.cAccumBlueBits = attribList[arg++]); | |
393 | break; | |
394 | case WX_GL_MIN_ACCUM_ALPHA: | |
395 | pfd.cAccumBits += (pfd.cAccumAlphaBits = attribList[arg++]); | |
396 | break; | |
397 | default: | |
398 | break; | |
399 | } | |
400 | } | |
401 | } | |
0122b7e3 RD |
402 | } |
403 | ||
404 | void wxGLCanvas::SetupPixelFormat(int *attribList) // (HDC hDC) | |
405 | { | |
406 | int pixelFormat; | |
407 | PIXELFORMATDESCRIPTOR pfd = { | |
408 | sizeof(PIXELFORMATDESCRIPTOR), /* size */ | |
409 | 1, /* version */ | |
410 | PFD_SUPPORT_OPENGL | | |
411 | PFD_DRAW_TO_WINDOW | | |
412 | PFD_DOUBLEBUFFER, /* support double-buffering */ | |
413 | PFD_TYPE_RGBA, /* color type */ | |
414 | 16, /* prefered color depth */ | |
415 | 0, 0, 0, 0, 0, 0, /* color bits (ignored) */ | |
416 | 0, /* no alpha buffer */ | |
417 | 0, /* alpha bits (ignored) */ | |
418 | 0, /* no accumulation buffer */ | |
419 | 0, 0, 0, 0, /* accum bits (ignored) */ | |
420 | 16, /* depth buffer */ | |
421 | 0, /* no stencil buffer */ | |
422 | 0, /* no auxiliary buffers */ | |
423 | PFD_MAIN_PLANE, /* main layer */ | |
424 | 0, /* reserved */ | |
425 | 0, 0, 0, /* no layer, visible, damage masks */ | |
426 | }; | |
427 | ||
428 | AdjustPFDForAttributes(pfd, attribList); | |
429 | ||
19cf4f80 RD |
430 | pixelFormat = ChoosePixelFormat((HDC) m_hDC, &pfd); |
431 | if (pixelFormat == 0) { | |
0122b7e3 | 432 | wxLogWarning(_("ChoosePixelFormat failed.")); |
19cf4f80 | 433 | } |
0122b7e3 RD |
434 | else { |
435 | if (SetPixelFormat((HDC) m_hDC, pixelFormat, &pfd) != TRUE) { | |
436 | wxLogWarning(_("SetPixelFormat failed.")); | |
437 | } | |
19cf4f80 RD |
438 | } |
439 | } | |
440 | ||
441 | void wxGLCanvas::SetupPalette(const wxPalette& palette) | |
442 | { | |
443 | int pixelFormat = GetPixelFormat((HDC) m_hDC); | |
444 | PIXELFORMATDESCRIPTOR pfd; | |
445 | ||
446 | DescribePixelFormat((HDC) m_hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); | |
447 | ||
448 | if (pfd.dwFlags & PFD_NEED_PALETTE) | |
449 | { | |
450 | } | |
451 | else | |
452 | { | |
0122b7e3 | 453 | return; |
19cf4f80 RD |
454 | } |
455 | ||
456 | m_palette = palette; | |
457 | ||
458 | if ( !m_palette.Ok() ) | |
459 | { | |
460 | m_palette = CreateDefaultPalette(); | |
461 | } | |
462 | ||
463 | if (m_palette.Ok()) | |
464 | { | |
465 | SelectPalette((HDC) m_hDC, (HPALETTE) m_palette.GetHPALETTE(), FALSE); | |
466 | RealizePalette((HDC) m_hDC); | |
467 | } | |
468 | } | |
469 | ||
470 | wxPalette wxGLCanvas::CreateDefaultPalette() | |
471 | { | |
472 | PIXELFORMATDESCRIPTOR pfd; | |
473 | int paletteSize; | |
474 | int pixelFormat = GetPixelFormat((HDC) m_hDC); | |
475 | ||
476 | DescribePixelFormat((HDC) m_hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); | |
477 | ||
478 | paletteSize = 1 << pfd.cColorBits; | |
479 | ||
480 | LOGPALETTE* pPal = | |
481 | (LOGPALETTE*) malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY)); | |
482 | pPal->palVersion = 0x300; | |
483 | pPal->palNumEntries = paletteSize; | |
484 | ||
485 | /* build a simple RGB color palette */ | |
486 | { | |
0122b7e3 RD |
487 | int redMask = (1 << pfd.cRedBits) - 1; |
488 | int greenMask = (1 << pfd.cGreenBits) - 1; | |
489 | int blueMask = (1 << pfd.cBlueBits) - 1; | |
490 | int i; | |
491 | ||
492 | for (i=0; i<paletteSize; ++i) { | |
493 | pPal->palPalEntry[i].peRed = | |
494 | (((i >> pfd.cRedShift) & redMask) * 255) / redMask; | |
495 | pPal->palPalEntry[i].peGreen = | |
496 | (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask; | |
497 | pPal->palPalEntry[i].peBlue = | |
498 | (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask; | |
499 | pPal->palPalEntry[i].peFlags = 0; | |
500 | } | |
19cf4f80 RD |
501 | } |
502 | ||
503 | HPALETTE hPalette = CreatePalette(pPal); | |
504 | free(pPal); | |
505 | ||
506 | wxPalette palette; | |
507 | palette.SetHPALETTE((WXHPALETTE) hPalette); | |
508 | ||
509 | return palette; | |
510 | } | |
511 | ||
512 | void wxGLCanvas::SwapBuffers() | |
513 | { | |
514 | if (m_glContext) | |
515 | m_glContext->SwapBuffers(); | |
516 | } | |
517 | ||
518 | void wxGLCanvas::OnSize(wxSizeEvent& event) | |
519 | { | |
19cf4f80 RD |
520 | } |
521 | ||
522 | void wxGLCanvas::SetCurrent() | |
523 | { | |
524 | if (m_glContext) | |
525 | { | |
526 | m_glContext->SetCurrent(); | |
527 | } | |
528 | } | |
529 | ||
530 | void wxGLCanvas::SetColour(const char *colour) | |
531 | { | |
532 | if (m_glContext) | |
533 | m_glContext->SetColour(colour); | |
534 | } | |
535 | ||
536 | // TODO: Have to have this called by parent frame (?) | |
537 | // So we need wxFrame to call OnQueryNewPalette for all children... | |
538 | void wxGLCanvas::OnQueryNewPalette(wxQueryNewPaletteEvent& event) | |
539 | { | |
540 | /* realize palette if this is the current window */ | |
541 | if ( GetPalette()->Ok() ) { | |
542 | ::UnrealizeObject((HPALETTE) GetPalette()->GetHPALETTE()); | |
543 | ::SelectPalette((HDC) GetHDC(), (HPALETTE) GetPalette()->GetHPALETTE(), FALSE); | |
544 | ::RealizePalette((HDC) GetHDC()); | |
545 | Refresh(); | |
546 | event.SetPaletteRealized(TRUE); | |
547 | } | |
548 | else | |
549 | event.SetPaletteRealized(FALSE); | |
550 | } | |
551 | ||
552 | // I think this doesn't have to be propagated to child windows. | |
553 | void wxGLCanvas::OnPaletteChanged(wxPaletteChangedEvent& event) | |
554 | { | |
555 | /* realize palette if this is *not* the current window */ | |
556 | if ( GetPalette() && | |
557 | GetPalette()->Ok() && (this != event.GetChangedWindow()) ) | |
558 | { | |
559 | ::UnrealizeObject((HPALETTE) GetPalette()->GetHPALETTE()); | |
560 | ::SelectPalette((HDC) GetHDC(), (HPALETTE) GetPalette()->GetHPALETTE(), FALSE); | |
561 | ::RealizePalette((HDC) GetHDC()); | |
562 | Refresh(); | |
563 | } | |
564 | } | |
565 | ||
566 | /* Give extensions proper function names. */ | |
567 | ||
568 | /* EXT_vertex_array */ | |
569 | void glArrayElementEXT(GLint i) | |
570 | { | |
571 | } | |
572 | ||
573 | void glColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer) | |
574 | { | |
575 | } | |
576 | ||
577 | void glDrawArraysEXT(GLenum mode, GLint first, GLsizei count) | |
578 | { | |
579 | #ifdef GL_EXT_vertex_array | |
580 | static PFNGLDRAWARRAYSEXTPROC proc = 0; | |
581 | ||
582 | if ( !proc ) | |
583 | { | |
584 | proc = (PFNGLDRAWARRAYSEXTPROC) wglGetProcAddress("glDrawArraysEXT"); | |
585 | } | |
586 | ||
587 | if ( proc ) | |
588 | (* proc) (mode, first, count); | |
589 | #endif | |
590 | } | |
591 | ||
592 | void glEdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *pointer) | |
593 | { | |
594 | } | |
595 | ||
596 | void glGetPointervEXT(GLenum pname, GLvoid* *params) | |
597 | { | |
598 | } | |
599 | ||
600 | void glIndexPointerEXT(GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer) | |
601 | { | |
602 | } | |
603 | ||
604 | void glNormalPointerEXT(GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer) | |
605 | { | |
606 | #ifdef GL_EXT_vertex_array | |
607 | static PFNGLNORMALPOINTEREXTPROC proc = 0; | |
608 | ||
609 | if ( !proc ) | |
610 | { | |
611 | proc = (PFNGLNORMALPOINTEREXTPROC) wglGetProcAddress("glNormalPointerEXT"); | |
612 | } | |
613 | ||
614 | if ( proc ) | |
615 | (* proc) (type, stride, count, pointer); | |
616 | #endif | |
617 | } | |
618 | ||
619 | void glTexCoordPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer) | |
620 | { | |
621 | } | |
622 | ||
623 | void glVertexPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer) | |
624 | { | |
625 | #ifdef GL_EXT_vertex_array | |
626 | static PFNGLVERTEXPOINTEREXTPROC proc = 0; | |
627 | ||
628 | if ( !proc ) | |
629 | { | |
630 | proc = (PFNGLVERTEXPOINTEREXTPROC) wglGetProcAddress("glVertexPointerEXT"); | |
631 | } | |
632 | if ( proc ) | |
633 | (* proc) (size, type, stride, count, pointer); | |
634 | #endif | |
635 | } | |
636 | ||
637 | /* EXT_color_subtable */ | |
638 | void glColorSubtableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *table) | |
639 | { | |
640 | } | |
641 | ||
642 | /* EXT_color_table */ | |
643 | void glColorTableEXT(GLenum target, GLenum internalformat, GLsizei width, GLenum format, GLenum type, const GLvoid *table) | |
644 | { | |
645 | } | |
646 | ||
647 | void glCopyColorTableEXT(GLenum target, GLenum internalformat, GLint x, GLint y, GLsizei width) | |
648 | { | |
649 | } | |
650 | ||
651 | void glGetColorTableEXT(GLenum target, GLenum format, GLenum type, GLvoid *table) | |
652 | { | |
653 | } | |
654 | ||
655 | void glGetColorTableParamaterfvEXT(GLenum target, GLenum pname, GLfloat *params) | |
656 | { | |
657 | } | |
658 | ||
659 | void glGetColorTavleParameterivEXT(GLenum target, GLenum pname, GLint *params) | |
660 | { | |
661 | } | |
662 | ||
663 | /* SGI_compiled_vertex_array */ | |
664 | void glLockArraysSGI(GLint first, GLsizei count) | |
665 | { | |
666 | } | |
667 | ||
668 | void glUnlockArraysSGI() | |
669 | { | |
670 | } | |
671 | ||
672 | ||
673 | /* SGI_cull_vertex */ | |
674 | void glCullParameterdvSGI(GLenum pname, GLdouble* params) | |
675 | { | |
676 | } | |
677 | ||
678 | void glCullParameterfvSGI(GLenum pname, GLfloat* params) | |
679 | { | |
680 | } | |
681 | ||
682 | /* SGI_index_func */ | |
683 | void glIndexFuncSGI(GLenum func, GLclampf ref) | |
684 | { | |
685 | } | |
686 | ||
687 | /* SGI_index_material */ | |
688 | void glIndexMaterialSGI(GLenum face, GLenum mode) | |
689 | { | |
690 | } | |
691 | ||
692 | /* WIN_swap_hint */ | |
693 | void glAddSwapHintRectWin(GLint x, GLint y, GLsizei width, GLsizei height) | |
694 | { | |
695 | } | |
696 | ||
0122b7e3 RD |
697 | |
698 | //--------------------------------------------------------------------------- | |
699 | // wxGLApp | |
700 | //--------------------------------------------------------------------------- | |
701 | ||
702 | IMPLEMENT_CLASS(wxGLApp, wxApp) | |
703 | ||
704 | bool wxGLApp::InitGLVisual(int *attribList) | |
705 | { | |
706 | int pixelFormat; | |
707 | PIXELFORMATDESCRIPTOR pfd = { | |
708 | sizeof(PIXELFORMATDESCRIPTOR), /* size */ | |
709 | 1, /* version */ | |
710 | PFD_SUPPORT_OPENGL | | |
711 | PFD_DRAW_TO_WINDOW | | |
712 | PFD_DOUBLEBUFFER, /* support double-buffering */ | |
713 | PFD_TYPE_RGBA, /* color type */ | |
714 | 16, /* prefered color depth */ | |
715 | 0, 0, 0, 0, 0, 0, /* color bits (ignored) */ | |
716 | 0, /* no alpha buffer */ | |
717 | 0, /* alpha bits (ignored) */ | |
718 | 0, /* no accumulation buffer */ | |
719 | 0, 0, 0, 0, /* accum bits (ignored) */ | |
720 | 16, /* depth buffer */ | |
721 | 0, /* no stencil buffer */ | |
722 | 0, /* no auxiliary buffers */ | |
723 | PFD_MAIN_PLANE, /* main layer */ | |
724 | 0, /* reserved */ | |
725 | 0, 0, 0, /* no layer, visible, damage masks */ | |
726 | }; | |
727 | ||
728 | AdjustPFDForAttributes(pfd, attribList); | |
729 | ||
730 | // use DC for whole (root) screen, since no windows have yet been created | |
731 | pixelFormat = ChoosePixelFormat((HDC) ::GetDC(NULL), &pfd); | |
732 | ||
733 | if (pixelFormat == 0) { | |
734 | wxLogError(_("Failed to initialize OpenGL")); | |
735 | return FALSE; | |
736 | } | |
737 | ||
738 | return TRUE; | |
739 | } | |
740 | ||
741 | wxGLApp::~wxGLApp() | |
742 | { | |
743 | } | |
744 | ||
19cf4f80 RD |
745 | #endif |
746 | // wxUSE_GLCANVAS |