1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGLCanvas, for using OpenGL with wxWindows under MS Windows
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "glcanvas.h"
16 #include "wx/wxprec.h"
18 #if defined(__BORLANDC__)
26 #define wxUSE_GLCANVAS 1
33 #include <wx/msw/private.h>
34 #include <wx/settings.h>
37 #include "myglcanvas.h"
39 static const wxChar
*wxGLCanvasClassName
= wxT("wxGLCanvasClass");
40 static const wxChar
*wxGLCanvasClassNameNoRedraw
= wxT("wxGLCanvasClassNR");
42 LRESULT WXDLLEXPORT APIENTRY _EXPORT
wxWndProc(HWND hWnd
, UINT message
,
43 WPARAM wParam
, LPARAM lParam
);
46 * GLContext implementation
49 wxGLContext::wxGLContext(bool isRGB
, wxGLCanvas
*win
, const wxPalette
& palette
)
53 m_hDC
= win
->GetHDC();
55 m_glContext
= wglCreateContext((HDC
) m_hDC
);
56 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
58 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
61 wxGLContext::wxGLContext(
62 bool isRGB
, wxGLCanvas
*win
,
63 const wxPalette
& palette
,
64 const wxGLContext
*other
/* for sharing display lists */
69 m_hDC
= win
->GetHDC();
71 m_glContext
= wglCreateContext((HDC
) m_hDC
);
72 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
75 wglShareLists( other
->m_glContext
, m_glContext
);
77 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
80 wxGLContext::~wxGLContext()
84 wglMakeCurrent(NULL
, NULL
);
85 wglDeleteContext(m_glContext
);
89 void wxGLContext::SwapBuffers()
93 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
94 ::SwapBuffers((HDC
) m_hDC
); //blits the backbuffer into DC
98 void wxGLContext::SetCurrent()
102 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
106 setupPixelFormat(hDC);
111 void wxGLContext::SetColour(const char *colour
)
116 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
119 r
= (float)(col
->Red()/256.0);
120 g
= (float)(col
->Green()/256.0);
121 b
= (float)(col
->Blue()/256.0);
128 * wxGLCanvas implementation
131 IMPLEMENT_CLASS(wxGLCanvas
, wxWindow
)
133 BEGIN_EVENT_TABLE(wxGLCanvas
, wxWindow
)
134 EVT_SIZE(wxGLCanvas::OnSize
)
135 EVT_PALETTE_CHANGED(wxGLCanvas::OnPaletteChanged
)
136 EVT_QUERY_NEW_PALETTE(wxGLCanvas::OnQueryNewPalette
)
139 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
140 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
141 int *attribList
, const wxPalette
& palette
) : wxWindow()
143 m_glContext
= (wxGLContext
*) NULL
;
145 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
149 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
150 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
153 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
155 SetupPixelFormat(attribList
);
156 SetupPalette(palette
);
158 m_glContext
= new wxGLContext(TRUE
, this, palette
);
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
)
167 m_glContext
= (wxGLContext
*) NULL
;
169 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
173 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
174 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
177 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
179 SetupPixelFormat(attribList
);
180 SetupPalette(palette
);
182 m_glContext
= new wxGLContext(TRUE
, this, palette
, shared
);
185 // Not very useful for wxMSW, but this is to be wxGTK compliant
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
):
192 m_glContext
= (wxGLContext
*) NULL
;
194 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
198 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
199 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
202 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
204 SetupPixelFormat(attribList
);
205 SetupPalette(palette
);
207 wxGLContext
*sharedContext
=0;
208 if (shared
) sharedContext
=shared
->GetContext();
209 m_glContext
= new wxGLContext(TRUE
, this, palette
, sharedContext
);
212 wxGLCanvas::~wxGLCanvas()
217 ::ReleaseDC((HWND
) GetHWND(), (HDC
) m_hDC
);
220 // Replaces wxWindow::Create functionality, since we need to use a different
222 bool wxGLCanvas::Create(wxWindow
*parent
,
227 const wxString
& name
)
229 static bool s_registeredGLCanvasClass
= FALSE
;
231 // We have to register a special window class because we need
232 // the CS_OWNDC style for GLCanvas.
235 From Angel Popov <jumpo@bitex.com>
237 Here are two snips from a dicussion in the OpenGL Gamedev list that explains
238 how this problem can be fixed:
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."
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."
251 if (!s_registeredGLCanvasClass
)
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
;
264 // Register the GLCanvas class name
265 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
266 wndclass
.lpszClassName
= wxGLCanvasClassName
;
267 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
| CS_OWNDC
;
269 if ( !::RegisterClass(&wndclass
) )
271 wxLogLastError(wxT("RegisterClass(wxGLCanvasClass)"));
275 // Register the GLCanvas class name for windows which don't do full repaint
277 wndclass
.lpszClassName
= wxGLCanvasClassNameNoRedraw
;
278 wndclass
.style
&= ~(CS_HREDRAW
| CS_VREDRAW
);
280 if ( !::RegisterClass(&wndclass
) )
282 wxLogLastError(wxT("RegisterClass(wxGLCanvasClassNameNoRedraw)"));
284 ::UnregisterClass(wxGLCanvasClassName
, wxhInstance
);
289 s_registeredGLCanvasClass
= TRUE
;
292 wxCHECK_MSG( parent
, FALSE
, wxT("can't create wxWindow without parent") );
294 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
297 parent
->AddChild(this);
300 if ( style
& wxBORDER
)
301 msflags
|= WS_BORDER
;
302 if ( style
& wxTHICK_FRAME
)
303 msflags
|= WS_THICKFRAME
;
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.
312 msflags
|= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
315 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
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
))
322 msflags
|= WS_BORDER
;
325 return MSWCreate(wxGLCanvasClassName
, NULL
, pos
, size
, msflags
, exStyle
);
328 static void AdjustPFDForAttributes(PIXELFORMATDESCRIPTOR
& pfd
, int *attribList
)
331 pfd
.dwFlags
&= ~PFD_DOUBLEBUFFER
;
332 pfd
.iPixelType
= PFD_TYPE_COLORINDEX
;
336 while( (attribList
[arg
]!=0) )
338 switch( attribList
[arg
++] )
341 pfd
.iPixelType
= PFD_TYPE_RGBA
;
343 case WX_GL_BUFFER_SIZE
:
344 pfd
.cColorBits
= attribList
[arg
++];
347 // this member looks like it may be obsolete
348 if (attribList
[arg
] > 0) {
349 pfd
.iLayerType
= (BYTE
)PFD_OVERLAY_PLANE
;
350 } else if (attribList
[arg
] < 0) {
351 pfd
.iLayerType
= (BYTE
)PFD_UNDERLAY_PLANE
;
353 pfd
.iLayerType
= (BYTE
)PFD_MAIN_PLANE
;
357 case WX_GL_DOUBLEBUFFER
:
358 pfd
.dwFlags
|= PFD_DOUBLEBUFFER
;
361 pfd
.dwFlags
|= PFD_STEREO
;
363 case WX_GL_AUX_BUFFERS
:
364 pfd
.cAuxBuffers
= attribList
[arg
++];
367 pfd
.cColorBits
+= (pfd
.cRedBits
= attribList
[arg
++]);
369 case WX_GL_MIN_GREEN
:
370 pfd
.cColorBits
+= (pfd
.cGreenBits
= attribList
[arg
++]);
373 pfd
.cColorBits
+= (pfd
.cBlueBits
= attribList
[arg
++]);
375 case WX_GL_MIN_ALPHA
:
376 // doesn't count in cColorBits
377 pfd
.cAlphaBits
= attribList
[arg
++];
379 case WX_GL_DEPTH_SIZE
:
380 pfd
.cDepthBits
= attribList
[arg
++];
382 case WX_GL_STENCIL_SIZE
:
383 pfd
.cStencilBits
= attribList
[arg
++];
385 case WX_GL_MIN_ACCUM_RED
:
386 pfd
.cAccumBits
+= (pfd
.cAccumRedBits
= attribList
[arg
++]);
388 case WX_GL_MIN_ACCUM_GREEN
:
389 pfd
.cAccumBits
+= (pfd
.cAccumGreenBits
= attribList
[arg
++]);
391 case WX_GL_MIN_ACCUM_BLUE
:
392 pfd
.cAccumBits
+= (pfd
.cAccumBlueBits
= attribList
[arg
++]);
394 case WX_GL_MIN_ACCUM_ALPHA
:
395 pfd
.cAccumBits
+= (pfd
.cAccumAlphaBits
= attribList
[arg
++]);
404 void wxGLCanvas::SetupPixelFormat(int *attribList
) // (HDC hDC)
407 PIXELFORMATDESCRIPTOR pfd
= {
408 sizeof(PIXELFORMATDESCRIPTOR
), /* size */
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 */
425 0, 0, 0, /* no layer, visible, damage masks */
428 AdjustPFDForAttributes(pfd
, attribList
);
430 pixelFormat
= ChoosePixelFormat((HDC
) m_hDC
, &pfd
);
431 if (pixelFormat
== 0) {
432 wxLogWarning(_("ChoosePixelFormat failed."));
435 if (SetPixelFormat((HDC
) m_hDC
, pixelFormat
, &pfd
) != TRUE
) {
436 wxLogWarning(_("SetPixelFormat failed."));
441 void wxGLCanvas::SetupPalette(const wxPalette
& palette
)
443 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
444 PIXELFORMATDESCRIPTOR pfd
;
446 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
448 if (pfd
.dwFlags
& PFD_NEED_PALETTE
)
458 if ( !m_palette
.Ok() )
460 m_palette
= CreateDefaultPalette();
465 SelectPalette((HDC
) m_hDC
, (HPALETTE
) m_palette
.GetHPALETTE(), FALSE
);
466 RealizePalette((HDC
) m_hDC
);
470 wxPalette
wxGLCanvas::CreateDefaultPalette()
472 PIXELFORMATDESCRIPTOR pfd
;
474 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
476 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
478 paletteSize
= 1 << pfd
.cColorBits
;
481 (LOGPALETTE
*) malloc(sizeof(LOGPALETTE
) + paletteSize
* sizeof(PALETTEENTRY
));
482 pPal
->palVersion
= 0x300;
483 pPal
->palNumEntries
= paletteSize
;
485 /* build a simple RGB color palette */
487 int redMask
= (1 << pfd
.cRedBits
) - 1;
488 int greenMask
= (1 << pfd
.cGreenBits
) - 1;
489 int blueMask
= (1 << pfd
.cBlueBits
) - 1;
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;
503 HPALETTE hPalette
= CreatePalette(pPal
);
507 palette
.SetHPALETTE((WXHPALETTE
) hPalette
);
512 void wxGLCanvas::SwapBuffers()
515 m_glContext
->SwapBuffers();
518 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
522 void wxGLCanvas::SetCurrent()
526 m_glContext
->SetCurrent();
530 void wxGLCanvas::SetColour(const char *colour
)
533 m_glContext
->SetColour(colour
);
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
)
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());
546 event
.SetPaletteRealized(TRUE
);
549 event
.SetPaletteRealized(FALSE
);
552 // I think this doesn't have to be propagated to child windows.
553 void wxGLCanvas::OnPaletteChanged(wxPaletteChangedEvent
& event
)
555 /* realize palette if this is *not* the current window */
557 GetPalette()->Ok() && (this != event
.GetChangedWindow()) )
559 ::UnrealizeObject((HPALETTE
) GetPalette()->GetHPALETTE());
560 ::SelectPalette((HDC
) GetHDC(), (HPALETTE
) GetPalette()->GetHPALETTE(), FALSE
);
561 ::RealizePalette((HDC
) GetHDC());
566 /* Give extensions proper function names. */
568 /* EXT_vertex_array */
569 void glArrayElementEXT(GLint i
)
573 void glColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
577 void glDrawArraysEXT(GLenum mode
, GLint first
, GLsizei count
)
579 #ifdef GL_EXT_vertex_array
580 static PFNGLDRAWARRAYSEXTPROC proc
= 0;
584 proc
= (PFNGLDRAWARRAYSEXTPROC
) wglGetProcAddress("glDrawArraysEXT");
588 (* proc
) (mode
, first
, count
);
592 void glEdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*pointer
)
596 void glGetPointervEXT(GLenum pname
, GLvoid
* *params
)
600 void glIndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
604 void glNormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
606 #ifdef GL_EXT_vertex_array
607 static PFNGLNORMALPOINTEREXTPROC proc
= 0;
611 proc
= (PFNGLNORMALPOINTEREXTPROC
) wglGetProcAddress("glNormalPointerEXT");
615 (* proc
) (type
, stride
, count
, pointer
);
619 void glTexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
623 void glVertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
625 #ifdef GL_EXT_vertex_array
626 static PFNGLVERTEXPOINTEREXTPROC proc
= 0;
630 proc
= (PFNGLVERTEXPOINTEREXTPROC
) wglGetProcAddress("glVertexPointerEXT");
633 (* proc
) (size
, type
, stride
, count
, pointer
);
637 /* EXT_color_subtable */
638 void glColorSubtableEXT(GLenum target
, GLsizei start
, GLsizei count
, GLenum format
, GLenum type
, const GLvoid
*table
)
642 /* EXT_color_table */
643 void glColorTableEXT(GLenum target
, GLenum internalformat
, GLsizei width
, GLenum format
, GLenum type
, const GLvoid
*table
)
647 void glCopyColorTableEXT(GLenum target
, GLenum internalformat
, GLint x
, GLint y
, GLsizei width
)
651 void glGetColorTableEXT(GLenum target
, GLenum format
, GLenum type
, GLvoid
*table
)
655 void glGetColorTableParamaterfvEXT(GLenum target
, GLenum pname
, GLfloat
*params
)
659 void glGetColorTavleParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
663 /* SGI_compiled_vertex_array */
664 void glLockArraysSGI(GLint first
, GLsizei count
)
668 void glUnlockArraysSGI()
673 /* SGI_cull_vertex */
674 void glCullParameterdvSGI(GLenum pname
, GLdouble
* params
)
678 void glCullParameterfvSGI(GLenum pname
, GLfloat
* params
)
683 void glIndexFuncSGI(GLenum func
, GLclampf ref
)
687 /* SGI_index_material */
688 void glIndexMaterialSGI(GLenum face
, GLenum mode
)
693 void glAddSwapHintRectWin(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
698 //---------------------------------------------------------------------------
700 //---------------------------------------------------------------------------
702 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
704 bool wxGLApp::InitGLVisual(int *attribList
)
707 PIXELFORMATDESCRIPTOR pfd
= {
708 sizeof(PIXELFORMATDESCRIPTOR
), /* size */
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 */
725 0, 0, 0, /* no layer, visible, damage masks */
728 AdjustPFDForAttributes(pfd
, attribList
);
730 // use DC for whole (root) screen, since no windows have yet been created
731 pixelFormat
= ChoosePixelFormat((HDC
) ::GetDC(NULL
), &pfd
);
733 if (pixelFormat
== 0) {
734 wxLogError(_("Failed to initialize OpenGL"));