1 /* Window-specific OpenGL functions implementation.
3 * Copyright (c) 1999 Lionel Ulmer
4 * Copyright (c) 2005 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
28 #include "opengl_ext.h"
37 #define WGL_WGLEXT_PROTOTYPES
38 #include "wine/wglext.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/wgl_driver.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
44 WINE_DECLARE_DEBUG_CHANNEL(fps);
46 static HMODULE opengl32_handle;
48 extern struct opengl_funcs null_opengl_funcs;
50 /* handle management */
52 #define MAX_WGL_HANDLES 1024
56 HANDLE_CONTEXT = 0 << 12,
57 HANDLE_PBUFFER = 1 << 12,
58 HANDLE_TYPE_MASK = 15 << 12
63 DWORD tid; /* thread that the context is current in */
64 HDC draw_dc; /* current drawing DC */
65 HDC read_dc; /* current reading DC */
66 GLubyte *extensions; /* extension string */
67 struct wgl_context *drv_ctx; /* driver context */
73 struct opengl_funcs *funcs;
76 struct opengl_context *context; /* for HANDLE_CONTEXT */
77 struct wgl_pbuffer *pbuffer; /* for HANDLE_PBUFFER */
78 struct wgl_handle *next; /* for free handles */
82 static struct wgl_handle wgl_handles[MAX_WGL_HANDLES];
83 static struct wgl_handle *next_free;
84 static unsigned int handle_count;
86 static CRITICAL_SECTION wgl_section;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": wgl_section") }
93 static CRITICAL_SECTION wgl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
95 static inline struct opengl_funcs *get_dc_funcs( HDC hdc )
97 struct opengl_funcs *funcs = __wine_get_wgl_driver( hdc, WINE_WGL_DRIVER_VERSION );
98 if (funcs == (void *)-1) funcs = &null_opengl_funcs;
102 static inline HANDLE next_handle( struct wgl_handle *ptr, enum wgl_handle_type type )
104 WORD generation = HIWORD( ptr->handle ) + 1;
105 if (!generation) generation++;
106 ptr->handle = MAKELONG( ptr - wgl_handles, generation ) | type;
107 return ULongToHandle( ptr->handle );
110 /* the current context is assumed valid and doesn't need locking */
111 static inline struct wgl_handle *get_current_context_ptr(void)
113 if (!NtCurrentTeb()->glCurrentRC) return NULL;
114 return &wgl_handles[LOWORD(NtCurrentTeb()->glCurrentRC) & ~HANDLE_TYPE_MASK];
117 static struct wgl_handle *get_handle_ptr( HANDLE handle, enum wgl_handle_type type )
119 unsigned int index = LOWORD( handle ) & ~HANDLE_TYPE_MASK;
121 EnterCriticalSection( &wgl_section );
122 if (index < handle_count && ULongToHandle(wgl_handles[index].handle) == handle)
123 return &wgl_handles[index];
125 LeaveCriticalSection( &wgl_section );
126 SetLastError( ERROR_INVALID_HANDLE );
130 static void release_handle_ptr( struct wgl_handle *ptr )
132 if (ptr) LeaveCriticalSection( &wgl_section );
135 static HANDLE alloc_handle( enum wgl_handle_type type, struct opengl_funcs *funcs, void *user_ptr )
138 struct wgl_handle *ptr = NULL;
140 EnterCriticalSection( &wgl_section );
141 if ((ptr = next_free))
142 next_free = next_free->u.next;
143 else if (handle_count < MAX_WGL_HANDLES)
144 ptr = &wgl_handles[handle_count++];
149 ptr->u.context = user_ptr;
150 handle = next_handle( ptr, type );
152 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
153 LeaveCriticalSection( &wgl_section );
157 static void free_handle_ptr( struct wgl_handle *ptr )
159 ptr->handle |= 0xffff;
160 ptr->u.next = next_free;
163 LeaveCriticalSection( &wgl_section );
166 /***********************************************************************
167 * wglCopyContext (OPENGL32.@)
169 BOOL WINAPI wglCopyContext(HGLRC hglrcSrc, HGLRC hglrcDst, UINT mask)
171 struct wgl_handle *src, *dst;
174 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
175 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
177 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
178 else ret = src->funcs->wgl.p_wglCopyContext( src->u.context->drv_ctx,
179 dst->u.context->drv_ctx, mask );
181 release_handle_ptr( dst );
182 release_handle_ptr( src );
186 /***********************************************************************
187 * wglDeleteContext (OPENGL32.@)
189 BOOL WINAPI wglDeleteContext(HGLRC hglrc)
191 struct wgl_handle *ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT );
193 if (!ptr) return FALSE;
195 if (ptr->u.context->tid && ptr->u.context->tid != GetCurrentThreadId())
197 SetLastError( ERROR_BUSY );
198 release_handle_ptr( ptr );
201 if (hglrc == NtCurrentTeb()->glCurrentRC) wglMakeCurrent( 0, 0 );
202 ptr->funcs->wgl.p_wglDeleteContext( ptr->u.context->drv_ctx );
203 HeapFree( GetProcessHeap(), 0, ptr->u.context->extensions );
204 HeapFree( GetProcessHeap(), 0, ptr->u.context );
205 free_handle_ptr( ptr );
209 /***********************************************************************
210 * wglMakeCurrent (OPENGL32.@)
212 BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
215 struct wgl_handle *ptr, *prev = get_current_context_ptr();
219 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
220 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
222 ret = ptr->funcs->wgl.p_wglMakeCurrent( hdc, ptr->u.context->drv_ctx );
225 if (prev) prev->u.context->tid = 0;
226 ptr->u.context->tid = GetCurrentThreadId();
227 ptr->u.context->draw_dc = hdc;
228 ptr->u.context->read_dc = hdc;
229 NtCurrentTeb()->glCurrentRC = hglrc;
230 NtCurrentTeb()->glTable = ptr->funcs;
235 SetLastError( ERROR_BUSY );
238 release_handle_ptr( ptr );
242 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
243 prev->u.context->tid = 0;
244 NtCurrentTeb()->glCurrentRC = 0;
245 NtCurrentTeb()->glTable = &null_opengl_funcs;
249 SetLastError( ERROR_INVALID_HANDLE );
255 /***********************************************************************
256 * wglCreateContextAttribsARB
258 * Provided by the WGL_ARB_create_context extension.
260 HGLRC WINAPI wglCreateContextAttribsARB( HDC hdc, HGLRC share, const int *attribs )
263 struct wgl_context *drv_ctx;
264 struct wgl_handle *share_ptr = NULL;
265 struct opengl_context *context;
266 struct opengl_funcs *funcs = get_dc_funcs( hdc );
268 if (!funcs || !funcs->ext.p_wglCreateContextAttribsARB) return 0;
269 if (share && !(share_ptr = get_handle_ptr( share, HANDLE_CONTEXT ))) return 0;
270 if ((drv_ctx = funcs->ext.p_wglCreateContextAttribsARB( hdc,
271 share_ptr ? share_ptr->u.context->drv_ctx : NULL, attribs )))
273 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
275 context->drv_ctx = drv_ctx;
276 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
277 HeapFree( GetProcessHeap(), 0, context );
279 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
281 release_handle_ptr( share_ptr );
286 /***********************************************************************
287 * wglMakeContextCurrentARB
289 * Provided by the WGL_ARB_make_current_read extension.
291 BOOL WINAPI wglMakeContextCurrentARB( HDC draw_hdc, HDC read_hdc, HGLRC hglrc )
294 struct wgl_handle *ptr, *prev = get_current_context_ptr();
298 if (!(ptr = get_handle_ptr( hglrc, HANDLE_CONTEXT ))) return FALSE;
299 if (!ptr->u.context->tid || ptr->u.context->tid == GetCurrentThreadId())
301 ret = (ptr->funcs->ext.p_wglMakeContextCurrentARB &&
302 ptr->funcs->ext.p_wglMakeContextCurrentARB( draw_hdc, read_hdc,
303 ptr->u.context->drv_ctx ));
306 if (prev) prev->u.context->tid = 0;
307 ptr->u.context->tid = GetCurrentThreadId();
308 ptr->u.context->draw_dc = draw_hdc;
309 ptr->u.context->read_dc = read_hdc;
310 NtCurrentTeb()->glCurrentRC = hglrc;
311 NtCurrentTeb()->glTable = ptr->funcs;
316 SetLastError( ERROR_BUSY );
319 release_handle_ptr( ptr );
323 if (!prev->funcs->wgl.p_wglMakeCurrent( 0, NULL )) return FALSE;
324 prev->u.context->tid = 0;
325 NtCurrentTeb()->glCurrentRC = 0;
326 NtCurrentTeb()->glTable = &null_opengl_funcs;
331 /***********************************************************************
332 * wglGetCurrentReadDCARB
334 * Provided by the WGL_ARB_make_current_read extension.
336 HDC WINAPI wglGetCurrentReadDCARB(void)
338 struct wgl_handle *ptr = get_current_context_ptr();
341 return ptr->u.context->read_dc;
344 /***********************************************************************
345 * wglShareLists (OPENGL32.@)
347 BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
350 struct wgl_handle *src, *dst;
352 if (!(src = get_handle_ptr( hglrcSrc, HANDLE_CONTEXT ))) return FALSE;
353 if ((dst = get_handle_ptr( hglrcDst, HANDLE_CONTEXT )))
355 if (src->funcs != dst->funcs) SetLastError( ERROR_INVALID_HANDLE );
356 else ret = src->funcs->wgl.p_wglShareLists( src->u.context->drv_ctx, dst->u.context->drv_ctx );
358 release_handle_ptr( dst );
359 release_handle_ptr( src );
363 /***********************************************************************
364 * wglGetCurrentDC (OPENGL32.@)
366 HDC WINAPI wglGetCurrentDC(void)
368 struct wgl_handle *ptr = get_current_context_ptr();
371 return ptr->u.context->draw_dc;
374 /***********************************************************************
375 * wglCreateContext (OPENGL32.@)
377 HGLRC WINAPI wglCreateContext(HDC hdc)
380 struct wgl_context *drv_ctx;
381 struct opengl_context *context;
382 struct opengl_funcs *funcs = get_dc_funcs( hdc );
384 if (!funcs) return 0;
385 if (!(drv_ctx = funcs->wgl.p_wglCreateContext( hdc ))) return 0;
386 if ((context = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*context) )))
388 context->drv_ctx = drv_ctx;
389 if (!(ret = alloc_handle( HANDLE_CONTEXT, funcs, context )))
390 HeapFree( GetProcessHeap(), 0, context );
392 if (!ret) funcs->wgl.p_wglDeleteContext( drv_ctx );
396 /***********************************************************************
397 * wglGetCurrentContext (OPENGL32.@)
399 HGLRC WINAPI wglGetCurrentContext(void)
401 return NtCurrentTeb()->glCurrentRC;
404 /***********************************************************************
405 * wglDescribePixelFormat (OPENGL32.@)
407 INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
409 struct opengl_funcs *funcs = get_dc_funcs( hdc );
410 if (!funcs) return 0;
411 return funcs->wgl.p_wglDescribePixelFormat( hdc, format, size, descr );
414 /***********************************************************************
415 * wglChoosePixelFormat (OPENGL32.@)
417 INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
419 PIXELFORMATDESCRIPTOR format, best;
420 int i, count, best_format;
421 int bestDBuffer = -1, bestStereo = -1;
423 TRACE_(wgl)( "%p %p: size %u version %u flags %u type %u color %u %u,%u,%u,%u "
424 "accum %u depth %u stencil %u aux %u\n",
425 hdc, ppfd, ppfd->nSize, ppfd->nVersion, ppfd->dwFlags, ppfd->iPixelType,
426 ppfd->cColorBits, ppfd->cRedBits, ppfd->cGreenBits, ppfd->cBlueBits, ppfd->cAlphaBits,
427 ppfd->cAccumBits, ppfd->cDepthBits, ppfd->cStencilBits, ppfd->cAuxBuffers );
429 count = wglDescribePixelFormat( hdc, 0, 0, NULL );
430 if (!count) return 0;
434 best.cAlphaBits = -1;
435 best.cColorBits = -1;
436 best.cDepthBits = -1;
437 best.cStencilBits = -1;
438 best.cAuxBuffers = -1;
440 for (i = 1; i <= count; i++)
442 if (!wglDescribePixelFormat( hdc, i, sizeof(format), &format )) continue;
444 if (ppfd->iPixelType != format.iPixelType)
446 TRACE( "pixel type mismatch for iPixelFormat=%d\n", i );
450 /* only use bitmap capable for formats for bitmap rendering */
451 if( (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) != (format.dwFlags & PFD_DRAW_TO_BITMAP))
453 TRACE( "PFD_DRAW_TO_BITMAP mismatch for iPixelFormat=%d\n", i );
457 /* The behavior of PDF_STEREO/PFD_STEREO_DONTCARE and PFD_DOUBLEBUFFER / PFD_DOUBLEBUFFER_DONTCARE
458 * is not very clear on MSDN. They specify that ChoosePixelFormat tries to match pixel formats
459 * with the flag (PFD_STEREO / PFD_DOUBLEBUFFERING) set. Otherwise it says that it tries to match
460 * formats without the given flag set.
461 * A test on Windows using a Radeon 9500pro on WinXP (the driver doesn't support Stereo)
462 * has indicated that a format without stereo is returned when stereo is unavailable.
463 * So in case PFD_STEREO is set, formats that support it should have priority above formats
464 * without. In case PFD_STEREO_DONTCARE is set, stereo is ignored.
466 * To summarize the following is most likely the correct behavior:
467 * stereo not set -> prefer non-stereo formats, but also accept stereo formats
468 * stereo set -> prefer stereo formats, but also accept non-stereo formats
469 * stereo don't care -> it doesn't matter whether we get stereo or not
471 * In Wine we will treat non-stereo the same way as don't care because it makes
472 * format selection even more complicated and second drivers with Stereo advertise
473 * each format twice anyway.
476 /* Doublebuffer, see the comments above */
477 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE))
479 if (((ppfd->dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) &&
480 ((format.dwFlags & PFD_DOUBLEBUFFER) == (ppfd->dwFlags & PFD_DOUBLEBUFFER)))
483 if (bestDBuffer != -1 && (format.dwFlags & PFD_DOUBLEBUFFER) != bestDBuffer) continue;
486 /* Stereo, see the comments above. */
487 if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE))
489 if (((ppfd->dwFlags & PFD_STEREO) != bestStereo) &&
490 ((format.dwFlags & PFD_STEREO) == (ppfd->dwFlags & PFD_STEREO)))
493 if (bestStereo != -1 && (format.dwFlags & PFD_STEREO) != bestStereo) continue;
496 /* Below we will do a number of checks to select the 'best' pixelformat.
497 * We assume the precedence cColorBits > cAlphaBits > cDepthBits > cStencilBits -> cAuxBuffers.
498 * The code works by trying to match the most important options as close as possible.
499 * When a reasonable format is found, we will try to match more options.
500 * It appears (see the opengl32 test) that Windows opengl drivers ignore options
501 * like cColorBits, cAlphaBits and friends if they are set to 0, so they are considered
502 * as DONTCARE. At least Serious Sam TSE relies on this behavior. */
504 if (ppfd->cColorBits)
506 if (((ppfd->cColorBits > best.cColorBits) && (format.cColorBits > best.cColorBits)) ||
507 ((format.cColorBits >= ppfd->cColorBits) && (format.cColorBits < best.cColorBits)))
510 if (best.cColorBits != format.cColorBits) /* Do further checks if the format is compatible */
512 TRACE( "color mismatch for iPixelFormat=%d\n", i );
516 if (ppfd->cAlphaBits)
518 if (((ppfd->cAlphaBits > best.cAlphaBits) && (format.cAlphaBits > best.cAlphaBits)) ||
519 ((format.cAlphaBits >= ppfd->cAlphaBits) && (format.cAlphaBits < best.cAlphaBits)))
522 if (best.cAlphaBits != format.cAlphaBits)
524 TRACE( "alpha mismatch for iPixelFormat=%d\n", i );
528 if (ppfd->cDepthBits)
530 if (((ppfd->cDepthBits > best.cDepthBits) && (format.cDepthBits > best.cDepthBits)) ||
531 ((format.cDepthBits >= ppfd->cDepthBits) && (format.cDepthBits < best.cDepthBits)))
534 if (best.cDepthBits != format.cDepthBits)
536 TRACE( "depth mismatch for iPixelFormat=%d\n", i );
540 if (ppfd->cStencilBits)
542 if (((ppfd->cStencilBits > best.cStencilBits) && (format.cStencilBits > best.cStencilBits)) ||
543 ((format.cStencilBits >= ppfd->cStencilBits) && (format.cStencilBits < best.cStencilBits)))
546 if (best.cStencilBits != format.cStencilBits)
548 TRACE( "stencil mismatch for iPixelFormat=%d\n", i );
552 if (ppfd->cAuxBuffers)
554 if (((ppfd->cAuxBuffers > best.cAuxBuffers) && (format.cAuxBuffers > best.cAuxBuffers)) ||
555 ((format.cAuxBuffers >= ppfd->cAuxBuffers) && (format.cAuxBuffers < best.cAuxBuffers)))
558 if (best.cAuxBuffers != format.cAuxBuffers)
560 TRACE( "aux mismatch for iPixelFormat=%d\n", i );
569 bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
570 bestStereo = format.dwFlags & PFD_STEREO;
573 TRACE( "returning %u\n", best_format );
577 /***********************************************************************
578 * wglGetPixelFormat (OPENGL32.@)
580 INT WINAPI wglGetPixelFormat(HDC hdc)
582 struct opengl_funcs *funcs = get_dc_funcs( hdc );
583 if (!funcs) return 0;
584 return funcs->wgl.p_wglGetPixelFormat( hdc );
587 /***********************************************************************
588 * wglSetPixelFormat(OPENGL32.@)
590 BOOL WINAPI wglSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
592 struct opengl_funcs *funcs = get_dc_funcs( hdc );
593 if (!funcs) return 0;
594 return funcs->wgl.p_wglSetPixelFormat( hdc, format, descr );
597 /***********************************************************************
598 * wglSwapBuffers (OPENGL32.@)
600 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
602 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
604 if (!funcs || !funcs->wgl.p_wglSwapBuffers) return FALSE;
605 if (!funcs->wgl.p_wglSwapBuffers( hdc )) return FALSE;
609 static long prev_time, start_time;
610 static unsigned long frames, frames_total;
612 DWORD time = GetTickCount();
615 /* every 1.5 seconds */
616 if (time - prev_time > 1500)
618 TRACE_(fps)("@ approx %.2ffps, total %.2ffps\n",
619 1000.0*frames/(time - prev_time), 1000.0*frames_total/(time - start_time));
622 if (start_time == 0) start_time = time;
628 /***********************************************************************
629 * wglCreateLayerContext (OPENGL32.@)
631 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
633 TRACE("(%p,%d)\n", hdc, iLayerPlane);
635 if (iLayerPlane == 0) {
636 return wglCreateContext(hdc);
638 FIXME("no handler for layer %d\n", iLayerPlane);
643 /***********************************************************************
644 * wglDescribeLayerPlane (OPENGL32.@)
646 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
650 LPLAYERPLANEDESCRIPTOR plpd) {
651 FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
656 /***********************************************************************
657 * wglGetLayerPaletteEntries (OPENGL32.@)
659 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
663 const COLORREF *pcr) {
664 FIXME("(): stub!\n");
669 /* check if the extension is present in the list */
670 static BOOL has_extension( const char *list, const char *ext )
672 size_t len = strlen( ext );
676 while (*list == ' ') list++;
677 if (!strncmp( list, ext, len ) && (!list[len] || list[len] == ' ')) return TRUE;
678 list = strchr( list, ' ' );
683 static int compar(const void *elt_a, const void *elt_b) {
684 return strcmp(((const OpenGL_extension *) elt_a)->name,
685 ((const OpenGL_extension *) elt_b)->name);
688 /* Check if a GL extension is supported */
689 static BOOL is_extension_supported(const char* extension)
691 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
692 const char *gl_ext_string = (const char*)glGetString(GL_EXTENSIONS);
694 TRACE("Checking for extension '%s'\n", extension);
697 ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
701 /* We use the GetProcAddress function from the display driver to retrieve function pointers
702 * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
703 * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
704 * require the function to return NULL when an extension isn't found. For this reason we check
705 * if the OpenGL extension required for the function we are looking up is supported. */
707 /* Check if the extension is part of the GL extension string to see if it is supported. */
708 if (has_extension(gl_ext_string, extension))
711 /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
712 * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
713 * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
714 * Check if we are searching for a core GL function */
715 if(strncmp(extension, "GL_VERSION_", 11) == 0)
717 const GLubyte *gl_version = funcs->gl.p_glGetString(GL_VERSION);
718 const char *version = extension + 11; /* Move past 'GL_VERSION_' */
721 ERR("No OpenGL version found!\n");
725 /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
726 * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
727 if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
730 WARN("The function requires OpenGL version '%c.%c' while your drivers only provide '%c.%c'\n", version[0], version[2], gl_version[0], gl_version[2]);
736 /***********************************************************************
737 * wglGetProcAddress (OPENGL32.@)
739 PROC WINAPI wglGetProcAddress( LPCSTR name )
741 struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
743 OpenGL_extension ext;
744 const OpenGL_extension *ext_ret;
746 if (!name) return NULL;
748 /* Without an active context opengl32 doesn't know to what
749 * driver it has to dispatch wglGetProcAddress.
751 if (!get_current_context_ptr())
753 WARN("No active WGL context found\n");
758 ext_ret = bsearch(&ext, extension_registry, extension_registry_size, sizeof(ext), compar);
761 WARN("Function %s unknown\n", name);
765 func_ptr = (void **)&funcs->ext + (ext_ret - extension_registry);
768 void *driver_func = funcs->wgl.p_wglGetProcAddress( name );
770 if (!is_extension_supported(ext_ret->extension))
771 WARN("Extension %s required for %s not supported\n", ext_ret->extension, name);
773 if (driver_func == NULL)
775 WARN("Function %s not supported by driver\n", name);
778 *func_ptr = driver_func;
781 TRACE("returning %s -> %p\n", name, ext_ret->func);
782 return ext_ret->func;
785 /***********************************************************************
786 * wglRealizeLayerPalette (OPENGL32.@)
788 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
796 /***********************************************************************
797 * wglSetLayerPaletteEntries (OPENGL32.@)
799 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
803 const COLORREF *pcr) {
804 FIXME("(): stub!\n");
809 /***********************************************************************
810 * wglSwapLayerBuffers (OPENGL32.@)
812 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
814 TRACE("(%p, %08x)\n", hdc, fuPlanes);
816 if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
817 if (!wglSwapBuffers( hdc )) return FALSE;
818 fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
822 WARN("Following layers unhandled: %08x\n", fuPlanes);
828 /***********************************************************************
829 * wglAllocateMemoryNV
831 * Provided by the WGL_NV_vertex_array_range extension.
833 void * WINAPI wglAllocateMemoryNV( GLsizei size, GLfloat readfreq, GLfloat writefreq, GLfloat priority )
835 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
837 if (!funcs->ext.p_wglAllocateMemoryNV) return NULL;
838 return funcs->ext.p_wglAllocateMemoryNV( size, readfreq, writefreq, priority );
841 /***********************************************************************
844 * Provided by the WGL_NV_vertex_array_range extension.
846 void WINAPI wglFreeMemoryNV( void *pointer )
848 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
850 if (funcs->ext.p_wglFreeMemoryNV) funcs->ext.p_wglFreeMemoryNV( pointer );
853 /***********************************************************************
856 * Provided by the WGL_ARB_render_texture extension.
858 BOOL WINAPI wglBindTexImageARB( HPBUFFERARB handle, int buffer )
860 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
863 if (!ptr) return FALSE;
864 ret = ptr->funcs->ext.p_wglBindTexImageARB( ptr->u.pbuffer, buffer );
865 release_handle_ptr( ptr );
869 /***********************************************************************
870 * wglReleaseTexImageARB
872 * Provided by the WGL_ARB_render_texture extension.
874 BOOL WINAPI wglReleaseTexImageARB( HPBUFFERARB handle, int buffer )
876 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
879 if (!ptr) return FALSE;
880 ret = ptr->funcs->ext.p_wglReleaseTexImageARB( ptr->u.pbuffer, buffer );
881 release_handle_ptr( ptr );
885 /***********************************************************************
886 * wglSetPbufferAttribARB
888 * Provided by the WGL_ARB_render_texture extension.
890 BOOL WINAPI wglSetPbufferAttribARB( HPBUFFERARB handle, const int *attribs )
892 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
895 if (!ptr) return FALSE;
896 ret = ptr->funcs->ext.p_wglSetPbufferAttribARB( ptr->u.pbuffer, attribs );
897 release_handle_ptr( ptr );
901 /***********************************************************************
902 * wglChoosePixelFormatARB
904 * Provided by the WGL_ARB_pixel_format extension.
906 BOOL WINAPI wglChoosePixelFormatARB( HDC hdc, const int *iattribs, const FLOAT *fattribs,
907 UINT max, int *formats, UINT *count )
909 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
911 if (!funcs || !funcs->ext.p_wglChoosePixelFormatARB) return FALSE;
912 return funcs->ext.p_wglChoosePixelFormatARB( hdc, iattribs, fattribs, max, formats, count );
915 /***********************************************************************
916 * wglGetPixelFormatAttribivARB
918 * Provided by the WGL_ARB_pixel_format extension.
920 BOOL WINAPI wglGetPixelFormatAttribivARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
923 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
925 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribivARB) return FALSE;
926 return funcs->ext.p_wglGetPixelFormatAttribivARB( hdc, format, layer, count, attribs, values );
929 /***********************************************************************
930 * wglGetPixelFormatAttribfvARB
932 * Provided by the WGL_ARB_pixel_format extension.
934 BOOL WINAPI wglGetPixelFormatAttribfvARB( HDC hdc, int format, int layer, UINT count, const int *attribs,
937 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
939 if (!funcs || !funcs->ext.p_wglGetPixelFormatAttribfvARB) return FALSE;
940 return funcs->ext.p_wglGetPixelFormatAttribfvARB( hdc, format, layer, count, attribs, values );
943 /***********************************************************************
944 * wglCreatePbufferARB
946 * Provided by the WGL_ARB_pbuffer extension.
948 HPBUFFERARB WINAPI wglCreatePbufferARB( HDC hdc, int format, int width, int height, const int *attribs )
951 struct wgl_pbuffer *pbuffer;
952 struct opengl_funcs *funcs = get_dc_funcs( hdc );
954 if (!funcs || !funcs->ext.p_wglCreatePbufferARB) return 0;
955 if (!(pbuffer = funcs->ext.p_wglCreatePbufferARB( hdc, format, width, height, attribs ))) return 0;
956 ret = alloc_handle( HANDLE_PBUFFER, funcs, pbuffer );
957 if (!ret) funcs->ext.p_wglDestroyPbufferARB( pbuffer );
961 /***********************************************************************
964 * Provided by the WGL_ARB_pbuffer extension.
966 HDC WINAPI wglGetPbufferDCARB( HPBUFFERARB handle )
968 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
972 ret = ptr->funcs->ext.p_wglGetPbufferDCARB( ptr->u.pbuffer );
973 release_handle_ptr( ptr );
977 /***********************************************************************
978 * wglReleasePbufferDCARB
980 * Provided by the WGL_ARB_pbuffer extension.
982 int WINAPI wglReleasePbufferDCARB( HPBUFFERARB handle, HDC hdc )
984 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
987 if (!ptr) return FALSE;
988 ret = ptr->funcs->ext.p_wglReleasePbufferDCARB( ptr->u.pbuffer, hdc );
989 release_handle_ptr( ptr );
993 /***********************************************************************
994 * wglDestroyPbufferARB
996 * Provided by the WGL_ARB_pbuffer extension.
998 BOOL WINAPI wglDestroyPbufferARB( HPBUFFERARB handle )
1000 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1002 if (!ptr) return FALSE;
1003 ptr->funcs->ext.p_wglDestroyPbufferARB( ptr->u.pbuffer );
1004 free_handle_ptr( ptr );
1008 /***********************************************************************
1009 * wglQueryPbufferARB
1011 * Provided by the WGL_ARB_pbuffer extension.
1013 BOOL WINAPI wglQueryPbufferARB( HPBUFFERARB handle, int attrib, int *value )
1015 struct wgl_handle *ptr = get_handle_ptr( handle, HANDLE_PBUFFER );
1018 if (!ptr) return FALSE;
1019 ret = ptr->funcs->ext.p_wglQueryPbufferARB( ptr->u.pbuffer, attrib, value );
1020 release_handle_ptr( ptr );
1024 /***********************************************************************
1025 * wglGetExtensionsStringARB
1027 * Provided by the WGL_ARB_extensions_string extension.
1029 const char * WINAPI wglGetExtensionsStringARB( HDC hdc )
1031 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1033 if (!funcs || !funcs->ext.p_wglGetExtensionsStringARB) return NULL;
1034 return (const char *)funcs->ext.p_wglGetExtensionsStringARB( hdc );
1037 /***********************************************************************
1038 * wglGetExtensionsStringEXT
1040 * Provided by the WGL_EXT_extensions_string extension.
1042 const char * WINAPI wglGetExtensionsStringEXT(void)
1044 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1046 if (!funcs->ext.p_wglGetExtensionsStringEXT) return NULL;
1047 return (const char *)funcs->ext.p_wglGetExtensionsStringEXT();
1050 /***********************************************************************
1051 * wglSwapIntervalEXT
1053 * Provided by the WGL_EXT_swap_control extension.
1055 BOOL WINAPI wglSwapIntervalEXT( int interval )
1057 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1059 if (!funcs->ext.p_wglSwapIntervalEXT) return FALSE;
1060 return funcs->ext.p_wglSwapIntervalEXT( interval );
1063 /***********************************************************************
1064 * wglGetSwapIntervalEXT
1066 * Provided by the WGL_EXT_swap_control extension.
1068 int WINAPI wglGetSwapIntervalEXT(void)
1070 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1072 if (!funcs->ext.p_wglGetSwapIntervalEXT) return FALSE;
1073 return funcs->ext.p_wglGetSwapIntervalEXT();
1076 /***********************************************************************
1077 * wglSetPixelFormatWINE
1079 * Provided by the WGL_WINE_pixel_format_passthrough extension.
1081 BOOL WINAPI wglSetPixelFormatWINE( HDC hdc, int format )
1083 const struct opengl_funcs *funcs = get_dc_funcs( hdc );
1085 if (!funcs || !funcs->ext.p_wglSetPixelFormatWINE) return FALSE;
1086 return funcs->ext.p_wglSetPixelFormatWINE( hdc, format );
1089 /***********************************************************************
1090 * wglUseFontBitmaps_common
1092 static BOOL wglUseFontBitmaps_common( HDC hdc, DWORD first, DWORD count, DWORD listBase, BOOL unicode )
1094 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1096 unsigned int glyph, size = 0;
1097 void *bitmap = NULL, *gl_bitmap = NULL;
1101 funcs->gl.p_glGetIntegerv(GL_UNPACK_ALIGNMENT, &org_alignment);
1102 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
1104 for (glyph = first; glyph < first + count; glyph++) {
1105 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1106 unsigned int needed_size, height, width, width_int;
1109 needed_size = GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1111 needed_size = GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, 0, NULL, &identity);
1113 TRACE("Glyph: %3d / List: %d size %d\n", glyph, listBase, needed_size);
1114 if (needed_size == GDI_ERROR) {
1119 if (needed_size > size) {
1121 HeapFree(GetProcessHeap(), 0, bitmap);
1122 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1123 bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1124 gl_bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
1127 ret = (GetGlyphOutlineW(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
1129 ret = (GetGlyphOutlineA(hdc, glyph, GGO_BITMAP, &gm, size, bitmap, &identity) != GDI_ERROR);
1132 if (TRACE_ON(wgl)) {
1133 unsigned int bitmask;
1134 unsigned char *bitmap_ = bitmap;
1136 TRACE(" - bbox: %d x %d\n", gm.gmBlackBoxX, gm.gmBlackBoxY);
1137 TRACE(" - origin: (%d, %d)\n", gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
1138 TRACE(" - increment: %d - %d\n", gm.gmCellIncX, gm.gmCellIncY);
1139 if (needed_size != 0) {
1140 TRACE(" - bitmap:\n");
1141 for (height = 0; height < gm.gmBlackBoxY; height++) {
1143 for (width = 0, bitmask = 0x80; width < gm.gmBlackBoxX; width++, bitmask >>= 1) {
1148 if (*bitmap_ & bitmask)
1153 bitmap_ += (4 - ((UINT_PTR)bitmap_ & 0x03));
1159 /* In OpenGL, the bitmap is drawn from the bottom to the top... So we need to invert the
1160 * glyph for it to be drawn properly.
1162 if (needed_size != 0) {
1163 width_int = (gm.gmBlackBoxX + 31) / 32;
1164 for (height = 0; height < gm.gmBlackBoxY; height++) {
1165 for (width = 0; width < width_int; width++) {
1166 ((int *) gl_bitmap)[(gm.gmBlackBoxY - height - 1) * width_int + width] =
1167 ((int *) bitmap)[height * width_int + width];
1172 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1173 if (needed_size != 0) {
1174 funcs->gl.p_glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,
1175 0 - gm.gmptGlyphOrigin.x, (int) gm.gmBlackBoxY - gm.gmptGlyphOrigin.y,
1176 gm.gmCellIncX, gm.gmCellIncY,
1179 /* This is the case of 'empty' glyphs like the space character */
1180 funcs->gl.p_glBitmap(0, 0, 0, 0, gm.gmCellIncX, gm.gmCellIncY, NULL);
1182 funcs->gl.p_glEndList();
1185 funcs->gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, org_alignment);
1186 HeapFree(GetProcessHeap(), 0, bitmap);
1187 HeapFree(GetProcessHeap(), 0, gl_bitmap);
1191 /***********************************************************************
1192 * wglUseFontBitmapsA (OPENGL32.@)
1194 BOOL WINAPI wglUseFontBitmapsA(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1196 return wglUseFontBitmaps_common( hdc, first, count, listBase, FALSE );
1199 /***********************************************************************
1200 * wglUseFontBitmapsW (OPENGL32.@)
1202 BOOL WINAPI wglUseFontBitmapsW(HDC hdc, DWORD first, DWORD count, DWORD listBase)
1204 return wglUseFontBitmaps_common( hdc, first, count, listBase, TRUE );
1207 /* FIXME: should probably have a glu.h header */
1209 typedef struct GLUtesselator GLUtesselator;
1210 typedef void (WINAPI *_GLUfuncptr)(void);
1212 #define GLU_TESS_BEGIN 100100
1213 #define GLU_TESS_VERTEX 100101
1214 #define GLU_TESS_END 100102
1216 static GLUtesselator * (WINAPI *pgluNewTess)(void);
1217 static void (WINAPI *pgluDeleteTess)(GLUtesselator *tess);
1218 static void (WINAPI *pgluTessBeginPolygon)(GLUtesselator *tess, void *polygon_data);
1219 static void (WINAPI *pgluTessEndPolygon)(GLUtesselator *tess);
1220 static void (WINAPI *pgluTessCallback)(GLUtesselator *tess, GLenum which, _GLUfuncptr fn);
1221 static void (WINAPI *pgluTessBeginContour)(GLUtesselator *tess);
1222 static void (WINAPI *pgluTessEndContour)(GLUtesselator *tess);
1223 static void (WINAPI *pgluTessVertex)(GLUtesselator *tess, GLdouble *location, GLvoid* data);
1225 static HMODULE load_libglu(void)
1227 static const WCHAR glu32W[] = {'g','l','u','3','2','.','d','l','l',0};
1228 static int already_loaded;
1229 static HMODULE module;
1231 if (already_loaded) return module;
1234 TRACE("Trying to load GLU library\n");
1235 module = LoadLibraryW( glu32W );
1238 WARN("Failed to load glu32\n");
1241 #define LOAD_FUNCPTR(f) p##f = (void *)GetProcAddress( module, #f )
1242 LOAD_FUNCPTR(gluNewTess);
1243 LOAD_FUNCPTR(gluDeleteTess);
1244 LOAD_FUNCPTR(gluTessBeginContour);
1245 LOAD_FUNCPTR(gluTessBeginPolygon);
1246 LOAD_FUNCPTR(gluTessCallback);
1247 LOAD_FUNCPTR(gluTessEndContour);
1248 LOAD_FUNCPTR(gluTessEndPolygon);
1249 LOAD_FUNCPTR(gluTessVertex);
1254 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
1256 vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;
1257 vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;
1261 static void WINAPI tess_callback_vertex(GLvoid *vertex)
1263 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1264 GLdouble *dbl = vertex;
1265 TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
1266 funcs->gl.p_glVertex3dv(vertex);
1269 static void WINAPI tess_callback_begin(GLenum which)
1271 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1272 TRACE("%d\n", which);
1273 funcs->gl.p_glBegin(which);
1276 static void WINAPI tess_callback_end(void)
1278 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1280 funcs->gl.p_glEnd();
1283 typedef struct _bezier_vector {
1288 static double bezier_deviation_squared(const bezier_vector *p)
1290 bezier_vector deviation;
1291 bezier_vector vertex;
1296 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4 - p[0].x;
1297 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4 - p[0].y;
1299 base.x = p[2].x - p[0].x;
1300 base.y = p[2].y - p[0].y;
1302 base_length = sqrt(base.x*base.x + base.y*base.y);
1303 base.x /= base_length;
1304 base.y /= base_length;
1306 dot = base.x*vertex.x + base.y*vertex.y;
1307 dot = min(max(dot, 0.0), base_length);
1311 deviation.x = vertex.x-base.x;
1312 deviation.y = vertex.y-base.y;
1314 return deviation.x*deviation.x + deviation.y*deviation.y;
1317 static int bezier_approximate(const bezier_vector *p, bezier_vector *points, FLOAT deviation)
1319 bezier_vector first_curve[3];
1320 bezier_vector second_curve[3];
1321 bezier_vector vertex;
1324 if(bezier_deviation_squared(p) <= deviation*deviation)
1331 vertex.x = (p[0].x + p[1].x*2 + p[2].x)/4;
1332 vertex.y = (p[0].y + p[1].y*2 + p[2].y)/4;
1334 first_curve[0] = p[0];
1335 first_curve[1].x = (p[0].x + p[1].x)/2;
1336 first_curve[1].y = (p[0].y + p[1].y)/2;
1337 first_curve[2] = vertex;
1339 second_curve[0] = vertex;
1340 second_curve[1].x = (p[2].x + p[1].x)/2;
1341 second_curve[1].y = (p[2].y + p[1].y)/2;
1342 second_curve[2] = p[2];
1344 total_vertices = bezier_approximate(first_curve, points, deviation);
1346 points += total_vertices;
1347 total_vertices += bezier_approximate(second_curve, points, deviation);
1348 return total_vertices;
1351 /***********************************************************************
1352 * wglUseFontOutlines_common
1354 static BOOL wglUseFontOutlines_common(HDC hdc,
1361 LPGLYPHMETRICSFLOAT lpgmf,
1364 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1366 const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
1367 GLUtesselator *tess = NULL;
1369 HFONT old_font, unscaled_font;
1370 UINT em_size = 1024;
1373 TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
1374 listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
1376 if(deviation <= 0.0)
1377 deviation = 1.0/em_size;
1379 if(format == WGL_FONT_POLYGONS)
1383 ERR("glu32 is required for this function but isn't available\n");
1387 tess = pgluNewTess();
1388 if(!tess) return FALSE;
1389 pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
1390 pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
1391 pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
1394 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1395 rc.left = rc.right = rc.bottom = 0;
1397 DPtoLP(hdc, (POINT*)&rc, 2);
1398 lf.lfHeight = -abs(rc.top - rc.bottom);
1399 lf.lfOrientation = lf.lfEscapement = 0;
1400 unscaled_font = CreateFontIndirectW(&lf);
1401 old_font = SelectObject(hdc, unscaled_font);
1403 for (glyph = first; glyph < first + count; glyph++)
1408 TTPOLYGONHEADER *pph;
1410 GLdouble *vertices = NULL;
1411 int vertex_total = -1;
1414 needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1416 needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
1418 if(needed == GDI_ERROR)
1421 buf = HeapAlloc(GetProcessHeap(), 0, needed);
1424 GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1426 GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
1428 TRACE("glyph %d\n", glyph);
1432 lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
1433 lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
1434 lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
1435 lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
1436 lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
1437 lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
1439 TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
1440 lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY);
1444 funcs->gl.p_glNewList(listBase++, GL_COMPILE);
1445 funcs->gl.p_glFrontFace(GL_CW);
1446 if(format == WGL_FONT_POLYGONS)
1447 pgluTessBeginPolygon(tess, NULL);
1451 if(vertex_total != -1)
1452 vertices = HeapAlloc(GetProcessHeap(), 0, vertex_total * 3 * sizeof(GLdouble));
1455 pph = (TTPOLYGONHEADER*)buf;
1456 while((BYTE*)pph < buf + needed)
1458 GLdouble previous[3];
1459 fixed_to_double(pph->pfxStart, em_size, previous);
1462 TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
1464 if(format == WGL_FONT_POLYGONS)
1465 pgluTessBeginContour(tess);
1467 funcs->gl.p_glBegin(GL_LINE_LOOP);
1471 fixed_to_double(pph->pfxStart, em_size, vertices);
1472 if(format == WGL_FONT_POLYGONS)
1473 pgluTessVertex(tess, vertices, vertices);
1475 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1480 ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
1481 while((char*)ppc < (char*)pph + pph->cb)
1486 switch(ppc->wType) {
1488 for(i = 0; i < ppc->cpfx; i++)
1492 TRACE("\t\tline to %d, %d\n",
1493 ppc->apfx[i].x.value, ppc->apfx[i].y.value);
1494 fixed_to_double(ppc->apfx[i], em_size, vertices);
1495 if(format == WGL_FONT_POLYGONS)
1496 pgluTessVertex(tess, vertices, vertices);
1498 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1501 fixed_to_double(ppc->apfx[i], em_size, previous);
1506 case TT_PRIM_QSPLINE:
1507 for(i = 0; i < ppc->cpfx-1; i++)
1509 bezier_vector curve[3];
1510 bezier_vector *points;
1511 GLdouble curve_vertex[3];
1514 TRACE("\t\tcurve %d,%d %d,%d\n",
1515 ppc->apfx[i].x.value, ppc->apfx[i].y.value,
1516 ppc->apfx[i + 1].x.value, ppc->apfx[i + 1].y.value);
1518 curve[0].x = previous[0];
1519 curve[0].y = previous[1];
1520 fixed_to_double(ppc->apfx[i], em_size, curve_vertex);
1521 curve[1].x = curve_vertex[0];
1522 curve[1].y = curve_vertex[1];
1523 fixed_to_double(ppc->apfx[i + 1], em_size, curve_vertex);
1524 curve[2].x = curve_vertex[0];
1525 curve[2].y = curve_vertex[1];
1528 curve[2].x = (curve[1].x + curve[2].x)/2;
1529 curve[2].y = (curve[1].y + curve[2].y)/2;
1531 num = bezier_approximate(curve, NULL, deviation);
1532 points = HeapAlloc(GetProcessHeap(), 0, num*sizeof(bezier_vector));
1533 num = bezier_approximate(curve, points, deviation);
1534 vertex_total += num;
1537 for(j=0; j<num; j++)
1539 TRACE("\t\t\tvertex at %f,%f\n", points[j].x, points[j].y);
1540 vertices[0] = points[j].x;
1541 vertices[1] = points[j].y;
1543 if(format == WGL_FONT_POLYGONS)
1544 pgluTessVertex(tess, vertices, vertices);
1546 funcs->gl.p_glVertex3d(vertices[0], vertices[1], vertices[2]);
1550 HeapFree(GetProcessHeap(), 0, points);
1551 previous[0] = curve[2].x;
1552 previous[1] = curve[2].y;
1556 ERR("\t\tcurve type = %d\n", ppc->wType);
1557 if(format == WGL_FONT_POLYGONS)
1558 pgluTessEndContour(tess);
1560 funcs->gl.p_glEnd();
1564 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
1565 (ppc->cpfx - 1) * sizeof(POINTFX));
1567 if(format == WGL_FONT_POLYGONS)
1568 pgluTessEndContour(tess);
1570 funcs->gl.p_glEnd();
1571 pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
1576 if(format == WGL_FONT_POLYGONS)
1577 pgluTessEndPolygon(tess);
1578 funcs->gl.p_glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
1579 funcs->gl.p_glEndList();
1580 HeapFree(GetProcessHeap(), 0, buf);
1581 HeapFree(GetProcessHeap(), 0, vertices);
1585 DeleteObject(SelectObject(hdc, old_font));
1586 if(format == WGL_FONT_POLYGONS)
1587 pgluDeleteTess(tess);
1592 /***********************************************************************
1593 * wglUseFontOutlinesA (OPENGL32.@)
1595 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
1602 LPGLYPHMETRICSFLOAT lpgmf)
1604 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
1607 /***********************************************************************
1608 * wglUseFontOutlinesW (OPENGL32.@)
1610 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
1617 LPGLYPHMETRICSFLOAT lpgmf)
1619 return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
1622 /***********************************************************************
1623 * glDebugEntry (OPENGL32.@)
1625 GLint WINAPI glDebugEntry( GLint unknown1, GLint unknown2 )
1630 /* build the extension string by filtering out the disabled extensions */
1631 static GLubyte *filter_extensions( const char *extensions )
1633 static const char *disabled;
1637 TRACE( "GL_EXTENSIONS:\n" );
1639 if (!extensions) extensions = "";
1647 /* @@ Wine registry key: HKCU\Software\Wine\OpenGL */
1648 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey ))
1650 if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size ))
1652 str = HeapAlloc( GetProcessHeap(), 0, size );
1653 if (RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (BYTE *)str, &size )) *str = 0;
1655 RegCloseKey( hkey );
1659 if (InterlockedCompareExchangePointer( (void **)&disabled, str, NULL ))
1660 HeapFree( GetProcessHeap(), 0, str );
1665 if (!disabled[0]) return NULL;
1666 if ((str = HeapAlloc( GetProcessHeap(), 0, strlen(extensions) + 2 )))
1671 while (*extensions == ' ') extensions++;
1672 if (!*extensions) break;
1673 if (!(end = strchr( extensions, ' ' ))) end = extensions + strlen( extensions );
1674 memcpy( p, extensions, end - extensions );
1675 p[end - extensions] = 0;
1676 if (!has_extension( disabled, p ))
1678 TRACE("++ %s\n", p );
1679 p += end - extensions;
1682 else TRACE("-- %s (disabled by config)\n", p );
1687 return (GLubyte *)str;
1690 /***********************************************************************
1691 * glGetString (OPENGL32.@)
1693 const GLubyte * WINAPI glGetString( GLenum name )
1695 const struct opengl_funcs *funcs = NtCurrentTeb()->glTable;
1696 const GLubyte *ret = funcs->gl.p_glGetString( name );
1698 if (name == GL_EXTENSIONS && ret)
1700 struct wgl_handle *ptr = get_current_context_ptr();
1701 if (ptr->u.context->extensions ||
1702 ((ptr->u.context->extensions = filter_extensions( (const char *)ret ))))
1703 ret = ptr->u.context->extensions;
1708 /***********************************************************************
1709 * OpenGL initialisation routine
1711 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1715 case DLL_PROCESS_ATTACH:
1716 opengl32_handle = hinst;
1717 DisableThreadLibraryCalls(hinst);
1718 NtCurrentTeb()->glTable = &null_opengl_funcs;
1720 case DLL_THREAD_ATTACH:
1721 NtCurrentTeb()->glTable = &null_opengl_funcs;