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__)
30 #include <wx/msw/private.h>
31 #include <wx/settings.h>
34 #include <wx/glcanvas.h>
36 wxChar wxGLCanvasClassName
[] = wxT("wxGLCanvasClass");
38 LRESULT WXDLLEXPORT APIENTRY _EXPORT
wxWndProc(HWND hWnd
, UINT message
,
39 WPARAM wParam
, LPARAM lParam
);
42 * GLContext implementation
45 wxGLContext::wxGLContext(bool isRGB
, wxGLCanvas
*win
, const wxPalette
& palette
)
49 m_hDC
= win
->GetHDC();
51 m_glContext
= wglCreateContext((HDC
) m_hDC
);
52 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
54 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
57 wxGLContext::wxGLContext(
58 bool isRGB
, wxGLCanvas
*win
,
59 const wxPalette
& palette
,
60 const wxGLContext
*other
/* for sharing display lists */
65 m_hDC
= win
->GetHDC();
67 m_glContext
= wglCreateContext((HDC
) m_hDC
);
68 wxCHECK_RET( m_glContext
, wxT("Couldn't create OpenGl context") );
71 wglShareLists( other
->m_glContext
, m_glContext
);
73 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
76 wxGLContext::~wxGLContext()
80 wglMakeCurrent(NULL
, NULL
);
81 wglDeleteContext(m_glContext
);
85 void wxGLContext::SwapBuffers()
89 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
90 ::SwapBuffers((HDC
) m_hDC
); //blits the backbuffer into DC
94 void wxGLContext::SetCurrent()
98 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
102 setupPixelFormat(hDC);
107 void wxGLContext::SetColour(const char *colour
)
112 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
115 r
= (float)(col
->Red()/256.0);
116 g
= (float)(col
->Green()/256.0);
117 b
= (float)(col
->Blue()/256.0);
124 * wxGLCanvas implementation
127 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
129 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
130 EVT_SIZE(wxGLCanvas::OnSize
)
131 EVT_PALETTE_CHANGED(wxGLCanvas::OnPaletteChanged
)
132 EVT_QUERY_NEW_PALETTE(wxGLCanvas::OnQueryNewPalette
)
135 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
136 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
137 int *attribList
, const wxPalette
& palette
) : wxScrolledWindow()
139 m_glContext
= (wxGLContext
*) NULL
;
141 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
145 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
146 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
149 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
151 SetupPixelFormat(attribList
);
152 SetupPalette(palette
);
154 m_glContext
= new wxGLContext(TRUE
, this, palette
);
157 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
158 const wxGLContext
*shared
, wxWindowID id
,
159 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
160 int *attribList
, const wxPalette
& palette
)
163 m_glContext
= (wxGLContext
*) NULL
;
165 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
169 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
170 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
173 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
175 SetupPixelFormat(attribList
);
176 SetupPalette(palette
);
178 m_glContext
= new wxGLContext(TRUE
, this, palette
, shared
);
181 // Not very useful for wxMSW, but this is to be wxGTK compliant
183 wxGLCanvas::wxGLCanvas( wxWindow
*parent
, const wxGLCanvas
*shared
, wxWindowID id
,
184 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
185 int *attribList
, const wxPalette
& palette
):
188 m_glContext
= (wxGLContext
*) NULL
;
190 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
194 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
195 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
198 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
200 SetupPixelFormat(attribList
);
201 SetupPalette(palette
);
203 wxGLContext
*sharedContext
=0;
204 if (shared
) sharedContext
=shared
->GetContext();
205 m_glContext
= new wxGLContext(TRUE
, this, palette
, sharedContext
);
208 wxGLCanvas::~wxGLCanvas()
213 ::ReleaseDC((HWND
) GetHWND(), (HDC
) m_hDC
);
216 // Replaces wxWindow::Create functionality, since we need to use a different window class
217 bool wxGLCanvas::Create(wxWindow
*parent
, wxWindowID id
,
218 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
221 Suggestion from Kelly Brock <kbrock@8cs.com> (not yet implemented):
223 OpenGL corruption fix is simple assuming it doesn't screw anything else
224 up. Add the following line to the top of the create function:
226 wxSize parentSize = GetClientSize();
228 All locations within the function that use 'size' are changed to
230 The above corrects the initial display corruption with the GeForce and
231 TNT2, not sure about other NVidia cards yet.
234 static bool registeredGLCanvasClass
= FALSE
;
236 // We have to register a special window class because we need
237 // the CS_OWNDC style for GLCanvas.
240 From Angel Popov <jumpo@bitex.com>
242 Here are two snips from a dicussion in the OpenGL Gamedev list that explains
243 how this problem can be fixed:
245 "There are 5 common DCs available in Win95. These are aquired when you call
246 GetDC or GetDCEx from a window that does _not_ have the OWNDC flag.
247 OWNDC flagged windows do not get their DC from the common DC pool, the issue
248 is they require 800 bytes each from the limited 64Kb local heap for GDI."
250 "The deal is, if you hold onto one of the 5 shared DC's too long (as GL apps
251 do), Win95 will actually "steal" it from you. MakeCurrent fails,
252 apparently, because Windows re-assigns the HDC to a different window. The
253 only way to prevent this, the only reliable means, is to set CS_OWNDC."
256 if (!registeredGLCanvasClass
)
260 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
| CS_OWNDC
;
262 // the fields which are common to all classes
263 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
264 wndclass
.cbClsExtra
= 0;
265 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
266 wndclass
.hInstance
= wxhInstance
;
267 wndclass
.hIcon
= (HICON
) NULL
;
268 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
269 wndclass
.lpszMenuName
= NULL
;
271 // Register the GLCanvas class name
272 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
273 wndclass
.lpszClassName
= wxGLCanvasClassName
;
274 wndclass
.style
= styleNormal
;
276 if ( !RegisterClass(&wndclass
) )
278 wxLogLastError(wxT("RegisterClass(wxGLCanvasClass)"));
282 registeredGLCanvasClass
= TRUE
;
285 wxCHECK_MSG( parent
, FALSE
, wxT("can't create wxWindow without parent") );
287 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
290 parent
->AddChild(this);
293 if ( style
& wxBORDER
)
294 msflags
|= WS_BORDER
;
295 if ( style
& wxTHICK_FRAME
)
296 msflags
|= WS_THICKFRAME
;
299 A general rule with OpenGL and Win32 is that any window that will have a
300 HGLRC built for it must have two flags: WS_CLIPCHILDREN & WS_CLIPSIBLINGS.
301 You can find references about this within the knowledge base and most OpenGL
302 books that contain the wgl function descriptions.
305 msflags
|= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
;
306 // if ( style & wxCLIP_CHILDREN )
307 // msflags |= WS_CLIPCHILDREN;
308 msflags
|= WS_CLIPCHILDREN
;
311 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
313 // Even with extended styles, need to combine with WS_BORDER
314 // for them to look right.
315 if ( want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
316 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
318 msflags
|= WS_BORDER
;
321 // calculate the value to return from WM_GETDLGCODE handler
322 if ( GetWindowStyleFlag() & wxWANTS_CHARS
)
324 // want everything: i.e. all keys and WM_CHAR message
325 m_lDlgCode
= DLGC_WANTARROWS
| DLGC_WANTCHARS
|
326 DLGC_WANTTAB
| DLGC_WANTMESSAGE
;
329 MSWCreate(m_windowId
, parent
, wxGLCanvasClassName
, this, NULL
,
331 WidthDefault(size
.x
), HeightDefault(size
.y
),
332 msflags
, NULL
, exStyle
);
337 static void AdjustPFDForAttributes(PIXELFORMATDESCRIPTOR
& pfd
, int *attribList
)
340 pfd
.dwFlags
&= ~PFD_DOUBLEBUFFER
;
341 pfd
.iPixelType
= PFD_TYPE_COLORINDEX
;
345 while( (attribList
[arg
]!=0) )
347 switch( attribList
[arg
++] )
350 pfd
.iPixelType
= PFD_TYPE_RGBA
;
352 case WX_GL_BUFFER_SIZE
:
353 pfd
.cColorBits
= attribList
[arg
++];
356 // this member looks like it may be obsolete
357 if (attribList
[arg
] > 0) {
358 pfd
.iLayerType
= PFD_OVERLAY_PLANE
;
359 } else if (attribList
[arg
] < 0) {
360 pfd
.iLayerType
= PFD_UNDERLAY_PLANE
;
362 pfd
.iLayerType
= PFD_MAIN_PLANE
;
366 case WX_GL_DOUBLEBUFFER
:
367 pfd
.dwFlags
|= PFD_DOUBLEBUFFER
;
370 pfd
.dwFlags
|= PFD_STEREO
;
372 case WX_GL_AUX_BUFFERS
:
373 pfd
.cAuxBuffers
= attribList
[arg
++];
376 pfd
.cColorBits
+= (pfd
.cRedBits
= attribList
[arg
++]);
378 case WX_GL_MIN_GREEN
:
379 pfd
.cColorBits
+= (pfd
.cGreenBits
= attribList
[arg
++]);
382 pfd
.cColorBits
+= (pfd
.cBlueBits
= attribList
[arg
++]);
384 case WX_GL_MIN_ALPHA
:
385 // doesn't count in cColorBits
386 pfd
.cAlphaBits
= attribList
[arg
++];
388 case WX_GL_DEPTH_SIZE
:
389 pfd
.cDepthBits
= attribList
[arg
++];
391 case WX_GL_STENCIL_SIZE
:
392 pfd
.cStencilBits
= attribList
[arg
++];
394 case WX_GL_MIN_ACCUM_RED
:
395 pfd
.cAccumBits
+= (pfd
.cAccumRedBits
= attribList
[arg
++]);
397 case WX_GL_MIN_ACCUM_GREEN
:
398 pfd
.cAccumBits
+= (pfd
.cAccumGreenBits
= attribList
[arg
++]);
400 case WX_GL_MIN_ACCUM_BLUE
:
401 pfd
.cAccumBits
+= (pfd
.cAccumBlueBits
= attribList
[arg
++]);
403 case WX_GL_MIN_ACCUM_ALPHA
:
404 pfd
.cAccumBits
+= (pfd
.cAccumAlphaBits
= attribList
[arg
++]);
413 void wxGLCanvas::SetupPixelFormat(int *attribList
) // (HDC hDC)
416 PIXELFORMATDESCRIPTOR pfd
= {
417 sizeof(PIXELFORMATDESCRIPTOR
), /* size */
421 PFD_DOUBLEBUFFER
, /* support double-buffering */
422 PFD_TYPE_RGBA
, /* color type */
423 16, /* prefered color depth */
424 0, 0, 0, 0, 0, 0, /* color bits (ignored) */
425 0, /* no alpha buffer */
426 0, /* alpha bits (ignored) */
427 0, /* no accumulation buffer */
428 0, 0, 0, 0, /* accum bits (ignored) */
429 16, /* depth buffer */
430 0, /* no stencil buffer */
431 0, /* no auxiliary buffers */
432 PFD_MAIN_PLANE
, /* main layer */
434 0, 0, 0, /* no layer, visible, damage masks */
437 AdjustPFDForAttributes(pfd
, attribList
);
439 pixelFormat
= ChoosePixelFormat((HDC
) m_hDC
, &pfd
);
440 if (pixelFormat
== 0) {
441 wxLogWarning(_("ChoosePixelFormat failed."));
444 if (SetPixelFormat((HDC
) m_hDC
, pixelFormat
, &pfd
) != TRUE
) {
445 wxLogWarning(_("SetPixelFormat failed."));
450 void wxGLCanvas::SetupPalette(const wxPalette
& palette
)
452 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
453 PIXELFORMATDESCRIPTOR pfd
;
455 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
457 if (pfd
.dwFlags
& PFD_NEED_PALETTE
)
467 if ( !m_palette
.Ok() )
469 m_palette
= CreateDefaultPalette();
474 SelectPalette((HDC
) m_hDC
, (HPALETTE
) m_palette
.GetHPALETTE(), FALSE
);
475 RealizePalette((HDC
) m_hDC
);
479 wxPalette
wxGLCanvas::CreateDefaultPalette()
481 PIXELFORMATDESCRIPTOR pfd
;
483 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
485 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
487 paletteSize
= 1 << pfd
.cColorBits
;
490 (LOGPALETTE
*) malloc(sizeof(LOGPALETTE
) + paletteSize
* sizeof(PALETTEENTRY
));
491 pPal
->palVersion
= 0x300;
492 pPal
->palNumEntries
= paletteSize
;
494 /* build a simple RGB color palette */
496 int redMask
= (1 << pfd
.cRedBits
) - 1;
497 int greenMask
= (1 << pfd
.cGreenBits
) - 1;
498 int blueMask
= (1 << pfd
.cBlueBits
) - 1;
501 for (i
=0; i
<paletteSize
; ++i
) {
502 pPal
->palPalEntry
[i
].peRed
=
503 (((i
>> pfd
.cRedShift
) & redMask
) * 255) / redMask
;
504 pPal
->palPalEntry
[i
].peGreen
=
505 (((i
>> pfd
.cGreenShift
) & greenMask
) * 255) / greenMask
;
506 pPal
->palPalEntry
[i
].peBlue
=
507 (((i
>> pfd
.cBlueShift
) & blueMask
) * 255) / blueMask
;
508 pPal
->palPalEntry
[i
].peFlags
= 0;
512 HPALETTE hPalette
= CreatePalette(pPal
);
516 palette
.SetHPALETTE((WXHPALETTE
) hPalette
);
521 void wxGLCanvas::SwapBuffers()
524 m_glContext
->SwapBuffers();
527 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
530 GetClientSize(& width
, & height
);
534 m_glContext
->SetCurrent();
536 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
537 glMatrixMode(GL_PROJECTION
);
539 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
540 glMatrixMode(GL_MODELVIEW
);
544 void wxGLCanvas::SetCurrent()
548 m_glContext
->SetCurrent();
552 void wxGLCanvas::SetColour(const char *colour
)
555 m_glContext
->SetColour(colour
);
558 // TODO: Have to have this called by parent frame (?)
559 // So we need wxFrame to call OnQueryNewPalette for all children...
560 void wxGLCanvas::OnQueryNewPalette(wxQueryNewPaletteEvent
& event
)
562 /* realize palette if this is the current window */
563 if ( GetPalette()->Ok() ) {
564 ::UnrealizeObject((HPALETTE
) GetPalette()->GetHPALETTE());
565 ::SelectPalette((HDC
) GetHDC(), (HPALETTE
) GetPalette()->GetHPALETTE(), FALSE
);
566 ::RealizePalette((HDC
) GetHDC());
568 event
.SetPaletteRealized(TRUE
);
571 event
.SetPaletteRealized(FALSE
);
574 // I think this doesn't have to be propagated to child windows.
575 void wxGLCanvas::OnPaletteChanged(wxPaletteChangedEvent
& event
)
577 /* realize palette if this is *not* the current window */
579 GetPalette()->Ok() && (this != event
.GetChangedWindow()) )
581 ::UnrealizeObject((HPALETTE
) GetPalette()->GetHPALETTE());
582 ::SelectPalette((HDC
) GetHDC(), (HPALETTE
) GetPalette()->GetHPALETTE(), FALSE
);
583 ::RealizePalette((HDC
) GetHDC());
588 /* Give extensions proper function names. */
590 /* EXT_vertex_array */
591 void glArrayElementEXT(GLint i
)
595 void glColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
599 void glDrawArraysEXT(GLenum mode
, GLint first
, GLsizei count
)
601 #ifdef GL_EXT_vertex_array
602 static PFNGLDRAWARRAYSEXTPROC proc
= 0;
606 proc
= (PFNGLDRAWARRAYSEXTPROC
) wglGetProcAddress("glDrawArraysEXT");
610 (* proc
) (mode
, first
, count
);
614 void glEdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*pointer
)
618 void glGetPointervEXT(GLenum pname
, GLvoid
* *params
)
622 void glIndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
626 void glNormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
628 #ifdef GL_EXT_vertex_array
629 static PFNGLNORMALPOINTEREXTPROC proc
= 0;
633 proc
= (PFNGLNORMALPOINTEREXTPROC
) wglGetProcAddress("glNormalPointerEXT");
637 (* proc
) (type
, stride
, count
, pointer
);
641 void glTexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
645 void glVertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
647 #ifdef GL_EXT_vertex_array
648 static PFNGLVERTEXPOINTEREXTPROC proc
= 0;
652 proc
= (PFNGLVERTEXPOINTEREXTPROC
) wglGetProcAddress("glVertexPointerEXT");
655 (* proc
) (size
, type
, stride
, count
, pointer
);
659 /* EXT_color_subtable */
660 void glColorSubtableEXT(GLenum target
, GLsizei start
, GLsizei count
, GLenum format
, GLenum type
, const GLvoid
*table
)
664 /* EXT_color_table */
665 void glColorTableEXT(GLenum target
, GLenum internalformat
, GLsizei width
, GLenum format
, GLenum type
, const GLvoid
*table
)
669 void glCopyColorTableEXT(GLenum target
, GLenum internalformat
, GLint x
, GLint y
, GLsizei width
)
673 void glGetColorTableEXT(GLenum target
, GLenum format
, GLenum type
, GLvoid
*table
)
677 void glGetColorTableParamaterfvEXT(GLenum target
, GLenum pname
, GLfloat
*params
)
681 void glGetColorTavleParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
685 /* SGI_compiled_vertex_array */
686 void glLockArraysSGI(GLint first
, GLsizei count
)
690 void glUnlockArraysSGI()
695 /* SGI_cull_vertex */
696 void glCullParameterdvSGI(GLenum pname
, GLdouble
* params
)
700 void glCullParameterfvSGI(GLenum pname
, GLfloat
* params
)
705 void glIndexFuncSGI(GLenum func
, GLclampf ref
)
709 /* SGI_index_material */
710 void glIndexMaterialSGI(GLenum face
, GLenum mode
)
715 void glAddSwapHintRectWin(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
720 //---------------------------------------------------------------------------
722 //---------------------------------------------------------------------------
724 IMPLEMENT_CLASS(wxGLApp
, wxApp
)
726 bool wxGLApp::InitGLVisual(int *attribList
)
729 PIXELFORMATDESCRIPTOR pfd
= {
730 sizeof(PIXELFORMATDESCRIPTOR
), /* size */
734 PFD_DOUBLEBUFFER
, /* support double-buffering */
735 PFD_TYPE_RGBA
, /* color type */
736 16, /* prefered color depth */
737 0, 0, 0, 0, 0, 0, /* color bits (ignored) */
738 0, /* no alpha buffer */
739 0, /* alpha bits (ignored) */
740 0, /* no accumulation buffer */
741 0, 0, 0, 0, /* accum bits (ignored) */
742 16, /* depth buffer */
743 0, /* no stencil buffer */
744 0, /* no auxiliary buffers */
745 PFD_MAIN_PLANE
, /* main layer */
747 0, 0, 0, /* no layer, visible, damage masks */
750 AdjustPFDForAttributes(pfd
, attribList
);
752 // use DC for whole (root) screen, since no windows have yet been created
753 pixelFormat
= ChoosePixelFormat((HDC
) ::GetDC(NULL
), &pfd
);
755 if (pixelFormat
== 0) {
756 wxLogError(_("Failed to initialize OpenGL"));