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 #include <wx/msw/private.h>
30 wxChar wxGLCanvasClassName
[] = wxT("wxGLCanvasClass");
32 LRESULT APIENTRY _EXPORT
wxWndProc(HWND hWnd
, UINT message
,
33 WPARAM wParam
, LPARAM lParam
);
36 * GLContext implementation
39 wxGLContext::wxGLContext(bool isRGB
, wxGLCanvas
*win
, const wxPalette
& palette
)
43 m_hDC
= win
->GetHDC();
45 m_glContext
= wglCreateContext((HDC
) m_hDC
);
46 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
48 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
51 wxGLContext::wxGLContext(
52 bool isRGB
, wxGLCanvas
*win
,
53 const wxPalette
& palette
,
54 const wxGLContext
*other
/* for sharing display lists */
59 m_hDC
= win
->GetHDC();
61 m_glContext
= wglCreateContext((HDC
) m_hDC
);
62 wxCHECK_RET( m_glContext
, "Couldn't create OpenGl context" );
65 wglShareLists( other
->m_glContext
, m_glContext
);
67 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
70 wxGLContext::~wxGLContext()
74 wglMakeCurrent(NULL
, NULL
);
75 wglDeleteContext(m_glContext
);
79 void wxGLContext::SwapBuffers()
83 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
84 ::SwapBuffers((HDC
) m_hDC
); //blits the backbuffer into DC
88 void wxGLContext::SetCurrent()
92 wglMakeCurrent((HDC
) m_hDC
, m_glContext
);
96 setupPixelFormat(hDC);
101 void wxGLContext::SetColour(const char *colour
)
106 wxColour
*col
= wxTheColourDatabase
->FindColour(colour
);
109 r
= (float)(col
->Red()/256.0);
110 g
= (float)(col
->Green()/256.0);
111 b
= (float)(col
->Blue()/256.0);
118 * wxGLCanvas implementation
121 IMPLEMENT_CLASS(wxGLCanvas
, wxScrolledWindow
)
123 BEGIN_EVENT_TABLE(wxGLCanvas
, wxScrolledWindow
)
124 EVT_SIZE(wxGLCanvas::OnSize
)
125 EVT_PALETTE_CHANGED(wxGLCanvas::OnPaletteChanged
)
126 EVT_QUERY_NEW_PALETTE(wxGLCanvas::OnQueryNewPalette
)
129 wxGLCanvas::wxGLCanvas(wxWindow
*parent
, wxWindowID id
,
130 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
131 int *attribList
/* not used yet! */, const wxPalette
& palette
):
132 wxScrolledWindow(parent
, id
, pos
, size
, style
, name
)
134 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
137 SetupPalette(palette
);
139 m_glContext
= new wxGLContext(TRUE
, this, palette
);
142 wxGLCanvas::wxGLCanvas( wxWindow
*parent
,
143 const wxGLContext
*shared
, wxWindowID id
,
144 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
,
145 int *attribList
, const wxPalette
& palette
)
147 // : wxScrolledWindow(parent, id, pos, size, style, name)
149 bool ret
= Create(parent
, id
, pos
, size
, style
, name
);
153 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
154 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
157 m_hDC
= (WXHDC
) ::GetDC((HWND
) GetHWND());
160 SetupPalette(palette
);
162 m_glContext
= new wxGLContext(TRUE
, this, palette
, shared
);
165 wxGLCanvas::~wxGLCanvas()
170 ::ReleaseDC((HWND
) GetHWND(), (HDC
) m_hDC
);
173 // Replaces wxWindow::Create functionality, since we need to use a different window class
174 bool wxGLCanvas::Create(wxWindow
*parent
, wxWindowID id
,
175 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
)
177 static bool registeredGLCanvasClass
= FALSE
;
179 // We have to register a special window class because we need
180 // the CS_OWNDC style for GLCanvas.
183 Here are two snips from a dicussion in the OpenGL Gamedev list that explains
184 how this problem can be fixed:
186 "There are 5 common DCs available in Win95. These are aquired when you call
187 GetDC or GetDCEx from a window that does _not_ have the OWNDC flag.
188 OWNDC flagged windows do not get their DC from the common DC pool, the issue
189 is they require 800 bytes each from the limited 64Kb local heap for GDI."
191 "The deal is, if you hold onto one of the 5 shared DC's too long (as GL apps
192 do), Win95 will actually "steal" it from you. MakeCurrent fails,
193 apparently, because Windows re-assigns the HDC to a different window. The
194 only way to prevent this, the only reliable means, is to set CS_OWNDC."
197 if (!registeredGLCanvasClass
)
201 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
| CS_OWNDC
;
203 // the fields which are common to all classes
204 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
205 wndclass
.cbClsExtra
= 0;
206 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
207 wndclass
.hInstance
= wxhInstance
;
208 wndclass
.hIcon
= (HICON
) NULL
;
209 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
210 wndclass
.lpszMenuName
= NULL
;
212 // Register the GLCanvas class name
213 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
214 wndclass
.lpszClassName
= wxGLCanvasClassName
;
215 wndclass
.style
= styleNormal
;
217 if ( !RegisterClass(&wndclass
) )
219 wxLogLastError("RegisterClass(wxGLCanvasClass)");
225 wxCHECK_MSG( parent
, FALSE
, wxT("can't create wxWindow without parent") );
227 if ( !CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
230 parent
->AddChild(this);
233 if ( style
& wxBORDER
)
234 msflags
|= WS_BORDER
;
235 if ( style
& wxTHICK_FRAME
)
236 msflags
|= WS_THICKFRAME
;
238 msflags
|= WS_CHILD
| WS_VISIBLE
;
239 if ( style
& wxCLIP_CHILDREN
)
240 msflags
|= WS_CLIPCHILDREN
;
243 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
245 // Even with extended styles, need to combine with WS_BORDER
246 // for them to look right.
247 if ( want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
248 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
250 msflags
|= WS_BORDER
;
253 // calculate the value to return from WM_GETDLGCODE handler
254 if ( GetWindowStyleFlag() & wxWANTS_CHARS
)
256 // want everything: i.e. all keys and WM_CHAR message
257 m_lDlgCode
= DLGC_WANTARROWS
| DLGC_WANTCHARS
|
258 DLGC_WANTTAB
| DLGC_WANTMESSAGE
;
261 MSWCreate(m_windowId
, parent
, wxGLCanvasClassName
, this, NULL
,
263 WidthDefault(size
.x
), HeightDefault(size
.y
),
264 msflags
, NULL
, exStyle
);
270 void wxGLCanvas::SetupPixelFormat() // (HDC hDC)
272 PIXELFORMATDESCRIPTOR pfd
= {
273 sizeof(PIXELFORMATDESCRIPTOR
), /* size */
277 PFD_DOUBLEBUFFER
, /* support double-buffering */
278 PFD_TYPE_RGBA
, /* color type */
279 16, /* prefered color depth */
280 0, 0, 0, 0, 0, 0, /* color bits (ignored) */
281 0, /* no alpha buffer */
282 0, /* alpha bits (ignored) */
283 0, /* no accumulation buffer */
284 0, 0, 0, 0, /* accum bits (ignored) */
285 16, /* depth buffer */
286 0, /* no stencil buffer */
287 0, /* no auxiliary buffers */
288 PFD_MAIN_PLANE
, /* main layer */
290 0, 0, 0, /* no layer, visible, damage masks */
294 pixelFormat
= ChoosePixelFormat((HDC
) m_hDC
, &pfd
);
295 if (pixelFormat
== 0) {
296 MessageBox(WindowFromDC((HDC
) m_hDC
), "ChoosePixelFormat failed.", "Error",
297 MB_ICONERROR
| MB_OK
);
301 if (SetPixelFormat((HDC
) m_hDC
, pixelFormat
, &pfd
) != TRUE
) {
302 MessageBox(WindowFromDC((HDC
) m_hDC
), "SetPixelFormat failed.", "Error",
303 MB_ICONERROR
| MB_OK
);
308 void wxGLCanvas::SetupPalette(const wxPalette
& palette
)
310 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
311 PIXELFORMATDESCRIPTOR pfd
;
313 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
315 if (pfd
.dwFlags
& PFD_NEED_PALETTE
)
325 if ( !m_palette
.Ok() )
327 m_palette
= CreateDefaultPalette();
332 SelectPalette((HDC
) m_hDC
, (HPALETTE
) m_palette
.GetHPALETTE(), FALSE
);
333 RealizePalette((HDC
) m_hDC
);
337 wxPalette
wxGLCanvas::CreateDefaultPalette()
339 PIXELFORMATDESCRIPTOR pfd
;
341 int pixelFormat
= GetPixelFormat((HDC
) m_hDC
);
343 DescribePixelFormat((HDC
) m_hDC
, pixelFormat
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
);
345 paletteSize
= 1 << pfd
.cColorBits
;
348 (LOGPALETTE
*) malloc(sizeof(LOGPALETTE
) + paletteSize
* sizeof(PALETTEENTRY
));
349 pPal
->palVersion
= 0x300;
350 pPal
->palNumEntries
= paletteSize
;
352 /* build a simple RGB color palette */
354 int redMask
= (1 << pfd
.cRedBits
) - 1;
355 int greenMask
= (1 << pfd
.cGreenBits
) - 1;
356 int blueMask
= (1 << pfd
.cBlueBits
) - 1;
359 for (i
=0; i
<paletteSize
; ++i
) {
360 pPal
->palPalEntry
[i
].peRed
=
361 (((i
>> pfd
.cRedShift
) & redMask
) * 255) / redMask
;
362 pPal
->palPalEntry
[i
].peGreen
=
363 (((i
>> pfd
.cGreenShift
) & greenMask
) * 255) / greenMask
;
364 pPal
->palPalEntry
[i
].peBlue
=
365 (((i
>> pfd
.cBlueShift
) & blueMask
) * 255) / blueMask
;
366 pPal
->palPalEntry
[i
].peFlags
= 0;
370 HPALETTE hPalette
= CreatePalette(pPal
);
374 palette
.SetHPALETTE((WXHPALETTE
) hPalette
);
379 void wxGLCanvas::SwapBuffers()
382 m_glContext
->SwapBuffers();
385 void wxGLCanvas::OnSize(wxSizeEvent
& event
)
388 GetClientSize(& width
, & height
);
392 m_glContext
->SetCurrent();
394 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
395 glMatrixMode(GL_PROJECTION
);
397 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
398 glMatrixMode(GL_MODELVIEW
);
402 void wxGLCanvas::SetCurrent()
406 m_glContext
->SetCurrent();
410 void wxGLCanvas::SetColour(const char *colour
)
413 m_glContext
->SetColour(colour
);
416 // TODO: Have to have this called by parent frame (?)
417 // So we need wxFrame to call OnQueryNewPalette for all children...
418 void wxGLCanvas::OnQueryNewPalette(wxQueryNewPaletteEvent
& event
)
420 /* realize palette if this is the current window */
421 if ( GetPalette()->Ok() ) {
422 ::UnrealizeObject((HPALETTE
) GetPalette()->GetHPALETTE());
423 ::SelectPalette((HDC
) GetHDC(), (HPALETTE
) GetPalette()->GetHPALETTE(), FALSE
);
424 ::RealizePalette((HDC
) GetHDC());
426 event
.SetPaletteRealized(TRUE
);
429 event
.SetPaletteRealized(FALSE
);
432 // I think this doesn't have to be propagated to child windows.
433 void wxGLCanvas::OnPaletteChanged(wxPaletteChangedEvent
& event
)
435 /* realize palette if this is *not* the current window */
437 GetPalette()->Ok() && (this != event
.GetChangedWindow()) )
439 ::UnrealizeObject((HPALETTE
) GetPalette()->GetHPALETTE());
440 ::SelectPalette((HDC
) GetHDC(), (HPALETTE
) GetPalette()->GetHPALETTE(), FALSE
);
441 ::RealizePalette((HDC
) GetHDC());
446 /* Give extensions proper function names. */
448 /* EXT_vertex_array */
449 void glArrayElementEXT(GLint i
)
453 void glColorPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
457 void glDrawArraysEXT(GLenum mode
, GLint first
, GLsizei count
)
459 #ifdef GL_EXT_vertex_array
460 static PFNGLDRAWARRAYSEXTPROC proc
= 0;
464 proc
= (PFNGLDRAWARRAYSEXTPROC
) wglGetProcAddress("glDrawArraysEXT");
468 (* proc
) (mode
, first
, count
);
472 void glEdgeFlagPointerEXT(GLsizei stride
, GLsizei count
, const GLboolean
*pointer
)
476 void glGetPointervEXT(GLenum pname
, GLvoid
* *params
)
480 void glIndexPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
484 void glNormalPointerEXT(GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
486 #ifdef GL_EXT_vertex_array
487 static PFNGLNORMALPOINTEREXTPROC proc
= 0;
491 proc
= (PFNGLNORMALPOINTEREXTPROC
) wglGetProcAddress("glNormalPointerEXT");
495 (* proc
) (type
, stride
, count
, pointer
);
499 void glTexCoordPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
503 void glVertexPointerEXT(GLint size
, GLenum type
, GLsizei stride
, GLsizei count
, const GLvoid
*pointer
)
505 #ifdef GL_EXT_vertex_array
506 static PFNGLVERTEXPOINTEREXTPROC proc
= 0;
510 proc
= (PFNGLVERTEXPOINTEREXTPROC
) wglGetProcAddress("glVertexPointerEXT");
514 (* proc
) (size
, type
, stride
, count
, pointer
);
518 /* EXT_color_subtable */
519 void glColorSubtableEXT(GLenum target
, GLsizei start
, GLsizei count
, GLenum format
, GLenum type
, const GLvoid
*table
)
523 /* EXT_color_table */
524 void glColorTableEXT(GLenum target
, GLenum internalformat
, GLsizei width
, GLenum format
, GLenum type
, const GLvoid
*table
)
528 void glCopyColorTableEXT(GLenum target
, GLenum internalformat
, GLint x
, GLint y
, GLsizei width
)
532 void glGetColorTableEXT(GLenum target
, GLenum format
, GLenum type
, GLvoid
*table
)
536 void glGetColorTableParamaterfvEXT(GLenum target
, GLenum pname
, GLfloat
*params
)
540 void glGetColorTavleParameterivEXT(GLenum target
, GLenum pname
, GLint
*params
)
544 /* SGI_compiled_vertex_array */
545 void glLockArraysSGI(GLint first
, GLsizei count
)
549 void glUnlockArraysSGI()
554 /* SGI_cull_vertex */
555 void glCullParameterdvSGI(GLenum pname
, GLdouble
* params
)
559 void glCullParameterfvSGI(GLenum pname
, GLfloat
* params
)
564 void glIndexFuncSGI(GLenum func
, GLclampf ref
)
568 /* SGI_index_material */
569 void glIndexMaterialSGI(GLenum face
, GLenum mode
)
574 void glAddSwapHintRectWin(GLint x
, GLint y
, GLsizei width
, GLsizei height
)