]> git.saurik.com Git - wxWidgets.git/blob - utils/glcanvas/win/glcanvas.cpp
Will now make glcanvas_d.lib if FINAL=0
[wxWidgets.git] / utils / glcanvas / win / glcanvas.cpp
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
9 // Licence: wxWindows licence
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 #ifndef WX_PRECOMP
23 #include <wx/frame.h>
24 #endif
25
26 #include "glcanvas.h"
27
28 /*
29 * GLContext implementation
30 */
31
32 wxGLContext::wxGLContext(bool isRGB, wxGLCanvas *win, const wxPalette& palette)
33 {
34 m_window = win;
35
36 m_hDC = win->GetHDC();
37
38 m_glContext = wglCreateContext((HDC) m_hDC);
39 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
40
41 wglMakeCurrent((HDC) m_hDC, m_glContext);
42 }
43
44 wxGLContext::wxGLContext(
45 bool isRGB, wxGLCanvas *win,
46 const wxPalette& palette,
47 const wxGLContext *other /* for sharing display lists */
48 )
49 {
50 m_window = win;
51
52 m_hDC = win->GetHDC();
53
54 m_glContext = wglCreateContext((HDC) m_hDC);
55 wxCHECK_RET( m_glContext, "Couldn't create OpenGl context" );
56
57 if( other != 0 )
58 wglShareLists( other->m_glContext, m_glContext );
59
60 wglMakeCurrent((HDC) m_hDC, m_glContext);
61 }
62
63 wxGLContext::~wxGLContext()
64 {
65 if (m_glContext)
66 {
67 wglMakeCurrent(NULL, NULL);
68 wglDeleteContext(m_glContext);
69 }
70 }
71
72 void wxGLContext::SwapBuffers()
73 {
74 if (m_glContext)
75 {
76 wglMakeCurrent((HDC) m_hDC, m_glContext);
77 ::SwapBuffers((HDC) m_hDC); //blits the backbuffer into DC
78 }
79 }
80
81 void wxGLContext::SetCurrent()
82 {
83 if (m_glContext)
84 {
85 wglMakeCurrent((HDC) m_hDC, m_glContext);
86 }
87
88 /*
89 setupPixelFormat(hDC);
90 setupPalette(hDC);
91 */
92 }
93
94 void wxGLContext::SetColour(const char *colour)
95 {
96 float r = 0.0;
97 float g = 0.0;
98 float b = 0.0;
99 wxColour *col = wxTheColourDatabase->FindColour(colour);
100 if (col)
101 {
102 r = (float)(col->Red()/256.0);
103 g = (float)(col->Green()/256.0);
104 b = (float)(col->Blue()/256.0);
105 glColor3f( r, g, b);
106 }
107 }
108
109
110 /*
111 * wxGLCanvas implementation
112 */
113
114 IMPLEMENT_CLASS(wxGLCanvas, wxScrolledWindow)
115
116 BEGIN_EVENT_TABLE(wxGLCanvas, wxScrolledWindow)
117 EVT_SIZE(wxGLCanvas::OnSize)
118 EVT_PALETTE_CHANGED(wxGLCanvas::OnPaletteChanged)
119 EVT_QUERY_NEW_PALETTE(wxGLCanvas::OnQueryNewPalette)
120 END_EVENT_TABLE()
121
122 wxGLCanvas::wxGLCanvas(wxWindow *parent, wxWindowID id,
123 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
124 int *attribList /* not used yet! */, const wxPalette& palette):
125 wxScrolledWindow(parent, id, pos, size, style, name)
126 {
127 m_hDC = (WXHDC) ::GetDC((HWND) GetHWND());
128
129 SetupPixelFormat();
130 SetupPalette(palette);
131
132 m_glContext = new wxGLContext(TRUE, this, palette);
133 }
134 wxGLCanvas::wxGLCanvas( wxWindow *parent,
135 const wxGLContext *shared, wxWindowID id,
136 const wxPoint& pos, const wxSize& size, long style, const wxString& name,
137 int *attribList, const wxPalette& palette ) :
138 wxScrolledWindow(parent, id, pos, size, style, name)
139 {
140 m_hDC = (WXHDC) ::GetDC((HWND) GetHWND());
141
142 SetupPixelFormat();
143 SetupPalette(palette);
144
145 m_glContext = new wxGLContext(TRUE, this, palette, shared );
146 }
147
148 wxGLCanvas::~wxGLCanvas()
149 {
150 if (m_glContext)
151 delete m_glContext;
152
153 ::ReleaseDC((HWND) GetHWND(), (HDC) m_hDC);
154 }
155
156 void wxGLCanvas::SetupPixelFormat() // (HDC hDC)
157 {
158 PIXELFORMATDESCRIPTOR pfd = {
159 sizeof(PIXELFORMATDESCRIPTOR), /* size */
160 1, /* version */
161 PFD_SUPPORT_OPENGL |
162 PFD_DRAW_TO_WINDOW |
163 PFD_DOUBLEBUFFER, /* support double-buffering */
164 PFD_TYPE_RGBA, /* color type */
165 16, /* prefered color depth */
166 0, 0, 0, 0, 0, 0, /* color bits (ignored) */
167 0, /* no alpha buffer */
168 0, /* alpha bits (ignored) */
169 0, /* no accumulation buffer */
170 0, 0, 0, 0, /* accum bits (ignored) */
171 16, /* depth buffer */
172 0, /* no stencil buffer */
173 0, /* no auxiliary buffers */
174 PFD_MAIN_PLANE, /* main layer */
175 0, /* reserved */
176 0, 0, 0, /* no layer, visible, damage masks */
177 };
178 int pixelFormat;
179
180 pixelFormat = ChoosePixelFormat((HDC) m_hDC, &pfd);
181 if (pixelFormat == 0) {
182 MessageBox(WindowFromDC((HDC) m_hDC), "ChoosePixelFormat failed.", "Error",
183 MB_ICONERROR | MB_OK);
184 exit(1);
185 }
186
187 if (SetPixelFormat((HDC) m_hDC, pixelFormat, &pfd) != TRUE) {
188 MessageBox(WindowFromDC((HDC) m_hDC), "SetPixelFormat failed.", "Error",
189 MB_ICONERROR | MB_OK);
190 exit(1);
191 }
192 }
193
194 void wxGLCanvas::SetupPalette(const wxPalette& palette)
195 {
196 int pixelFormat = GetPixelFormat((HDC) m_hDC);
197 PIXELFORMATDESCRIPTOR pfd;
198
199 DescribePixelFormat((HDC) m_hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
200
201 if (pfd.dwFlags & PFD_NEED_PALETTE)
202 {
203 }
204 else
205 {
206 return;
207 }
208
209 m_palette = palette;
210
211 if ( !m_palette.Ok() )
212 {
213 m_palette = CreateDefaultPalette();
214 }
215
216 if (m_palette.Ok())
217 {
218 SelectPalette((HDC) m_hDC, (HPALETTE) m_palette.GetHPALETTE(), FALSE);
219 RealizePalette((HDC) m_hDC);
220 }
221 }
222
223 wxPalette wxGLCanvas::CreateDefaultPalette()
224 {
225 PIXELFORMATDESCRIPTOR pfd;
226 int paletteSize;
227 int pixelFormat = GetPixelFormat((HDC) m_hDC);
228
229 DescribePixelFormat((HDC) m_hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
230
231 paletteSize = 1 << pfd.cColorBits;
232
233 LOGPALETTE* pPal =
234 (LOGPALETTE*) malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
235 pPal->palVersion = 0x300;
236 pPal->palNumEntries = paletteSize;
237
238 /* build a simple RGB color palette */
239 {
240 int redMask = (1 << pfd.cRedBits) - 1;
241 int greenMask = (1 << pfd.cGreenBits) - 1;
242 int blueMask = (1 << pfd.cBlueBits) - 1;
243 int i;
244
245 for (i=0; i<paletteSize; ++i) {
246 pPal->palPalEntry[i].peRed =
247 (((i >> pfd.cRedShift) & redMask) * 255) / redMask;
248 pPal->palPalEntry[i].peGreen =
249 (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
250 pPal->palPalEntry[i].peBlue =
251 (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
252 pPal->palPalEntry[i].peFlags = 0;
253 }
254 }
255
256 HPALETTE hPalette = CreatePalette(pPal);
257 free(pPal);
258
259 wxPalette palette;
260 palette.SetHPALETTE((WXHPALETTE) hPalette);
261
262 return palette;
263 }
264
265 void wxGLCanvas::SwapBuffers()
266 {
267 if (m_glContext)
268 m_glContext->SwapBuffers();
269 }
270
271 void wxGLCanvas::OnSize(wxSizeEvent& event)
272 {
273 int width, height;
274 GetClientSize(& width, & height);
275
276 if (m_glContext)
277 {
278 m_glContext->SetCurrent();
279
280 glViewport(0, 0, (GLint)width, (GLint)height);
281 glMatrixMode(GL_PROJECTION);
282 glLoadIdentity();
283 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
284 glMatrixMode(GL_MODELVIEW);
285 }
286 }
287
288 void wxGLCanvas::SetCurrent()
289 {
290 if (m_glContext)
291 {
292 m_glContext->SetCurrent();
293 }
294 }
295
296 void wxGLCanvas::SetColour(const char *colour)
297 {
298 if (m_glContext)
299 m_glContext->SetColour(colour);
300 }
301
302 // TODO: Have to have this called by parent frame (?)
303 // So we need wxFrame to call OnQueryNewPalette for all children...
304 void wxGLCanvas::OnQueryNewPalette(wxQueryNewPaletteEvent& event)
305 {
306 /* realize palette if this is the current window */
307 if ( GetPalette()->Ok() ) {
308 ::UnrealizeObject((HPALETTE) GetPalette()->GetHPALETTE());
309 ::SelectPalette((HDC) GetHDC(), (HPALETTE) GetPalette()->GetHPALETTE(), FALSE);
310 ::RealizePalette((HDC) GetHDC());
311 Refresh();
312 event.SetPaletteRealized(TRUE);
313 }
314 else
315 event.SetPaletteRealized(FALSE);
316 }
317
318 // I think this doesn't have to be propagated to child windows.
319 void wxGLCanvas::OnPaletteChanged(wxPaletteChangedEvent& event)
320 {
321 /* realize palette if this is *not* the current window */
322 if ( GetPalette() &&
323 GetPalette()->Ok() && (this != event.GetChangedWindow()) )
324 {
325 ::UnrealizeObject((HPALETTE) GetPalette()->GetHPALETTE());
326 ::SelectPalette((HDC) GetHDC(), (HPALETTE) GetPalette()->GetHPALETTE(), FALSE);
327 ::RealizePalette((HDC) GetHDC());
328 Refresh();
329 }
330 }
331
332 /* Give extensions proper function names. */
333
334 /* EXT_vertex_array */
335 void glArrayElementEXT(GLint i)
336 {
337 }
338
339 void glColorPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer)
340 {
341 }
342
343 void glDrawArraysEXT(GLenum mode, GLint first, GLsizei count)
344 {
345 #ifdef GL_EXT_vertex_array
346 static PFNGLDRAWARRAYSEXTPROC proc = 0;
347
348 if ( !proc )
349 {
350 proc = (PFNGLDRAWARRAYSEXTPROC) wglGetProcAddress("glDrawArraysEXT");
351 }
352
353 if ( proc )
354 (* proc) (mode, first, count);
355 #endif
356 }
357
358 void glEdgeFlagPointerEXT(GLsizei stride, GLsizei count, const GLboolean *pointer)
359 {
360 }
361
362 void glGetPointervEXT(GLenum pname, GLvoid* *params)
363 {
364 }
365
366 void glIndexPointerEXT(GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer)
367 {
368 }
369
370 void glNormalPointerEXT(GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer)
371 {
372 #ifdef GL_EXT_vertex_array
373 static PFNGLNORMALPOINTEREXTPROC proc = 0;
374
375 if ( !proc )
376 {
377 proc = (PFNGLNORMALPOINTEREXTPROC) wglGetProcAddress("glNormalPointerEXT");
378 }
379
380 if ( proc )
381 (* proc) (type, stride, count, pointer);
382 #endif
383 }
384
385 void glTexCoordPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer)
386 {
387 }
388
389 void glVertexPointerEXT(GLint size, GLenum type, GLsizei stride, GLsizei count, const GLvoid *pointer)
390 {
391 #ifdef GL_EXT_vertex_array
392 static PFNGLVERTEXPOINTEREXTPROC proc = 0;
393
394 if ( !proc )
395 {
396 proc = (PFNGLVERTEXPOINTEREXTPROC) wglGetProcAddress("glVertexPointerEXT");
397 }
398
399 if ( proc )
400 (* proc) (size, type, stride, count, pointer);
401 #endif
402 }
403
404 /* EXT_color_subtable */
405 void glColorSubtableEXT(GLenum target, GLsizei start, GLsizei count, GLenum format, GLenum type, const GLvoid *table)
406 {
407 }
408
409 /* EXT_color_table */
410 void glColorTableEXT(GLenum target, GLenum internalformat, GLsizei width, GLenum format, GLenum type, const GLvoid *table)
411 {
412 }
413
414 void glCopyColorTableEXT(GLenum target, GLenum internalformat, GLint x, GLint y, GLsizei width)
415 {
416 }
417
418 void glGetColorTableEXT(GLenum target, GLenum format, GLenum type, GLvoid *table)
419 {
420 }
421
422 void glGetColorTableParamaterfvEXT(GLenum target, GLenum pname, GLfloat *params)
423 {
424 }
425
426 void glGetColorTavleParameterivEXT(GLenum target, GLenum pname, GLint *params)
427 {
428 }
429
430 /* SGI_compiled_vertex_array */
431 void glLockArraysSGI(GLint first, GLsizei count)
432 {
433 }
434
435 void glUnlockArraysSGI()
436 {
437 }
438
439
440 /* SGI_cull_vertex */
441 void glCullParameterdvSGI(GLenum pname, GLdouble* params)
442 {
443 }
444
445 void glCullParameterfvSGI(GLenum pname, GLfloat* params)
446 {
447 }
448
449 /* SGI_index_func */
450 void glIndexFuncSGI(GLenum func, GLclampf ref)
451 {
452 }
453
454 /* SGI_index_material */
455 void glIndexMaterialSGI(GLenum face, GLenum mode)
456 {
457 }
458
459 /* WIN_swap_hint */
460 void glAddSwapHintRectWin(GLint x, GLint y, GLsizei width, GLsizei height)
461 {
462 }
463