/* ** The OpenGL Extension Wrangler Library ** Copyright (C) 2002-2008, Milan Ikits ** Copyright (C) 2002-2008, Marcelo E. Magallon ** Copyright (C) 2002, Lev Povalahev ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are met: ** ** * Redistributions of source code must retain the above copyright notice, ** this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright notice, ** this list of conditions and the following disclaimer in the documentation ** and/or other materials provided with the distribution. ** * The name of the author may be used to endorse or promote products ** derived from this software without specific prior written permission. ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF ** THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #if defined(_WIN32) #include #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) #include #endif #ifdef GLEW_REGAL #include #endif static FILE* f; #ifdef GLEW_MX GLEWContext _glewctx; #define glewGetContext() (&_glewctx) #ifdef _WIN32 WGLEWContext _wglewctx; #define wglewGetContext() (&_wglewctx) #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) GLXEWContext _glxewctx; #define glxewGetContext() (&_glxewctx) #endif #endif #if defined(_WIN32) GLboolean glewCreateContext (int* pixelformat); #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) GLboolean glewCreateContext (const char* display, int* visual); #else GLboolean glewCreateContext (); #endif #if defined(_WIN32) || !defined(__APPLE__) || defined(GLEW_APPLE_GLX) GLboolean glewParseArgs (int argc, char** argv, char** display, int* visual); #endif void glewDestroyContext (); /* ------------------------------------------------------------------------- */ static void glewPrintExt (const char* name, GLboolean def1, GLboolean def2, GLboolean def3) { unsigned int i; fprintf(f, "\n%s:", name); for (i=0; i<62-strlen(name); i++) fprintf(f, " "); fprintf(f, "%s ", def1 ? "OK" : "MISSING"); if (def1 != def2) fprintf(f, "[%s] ", def2 ? "OK" : "MISSING"); if (def1 != def3) fprintf(f, "[%s]\n", def3 ? "OK" : "MISSING"); else fprintf(f, "\n"); for (i=0; i]\n"); #else fprintf(stderr, "Usage: glewinfo [-display ] [-visual ]\n"); #endif return 1; } #endif #if defined(_WIN32) if (GL_TRUE == glewCreateContext(&visual)) #elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX) if (GL_TRUE == glewCreateContext()) #else if (GL_TRUE == glewCreateContext(display, &visual)) #endif { fprintf(stderr, "Error: glewCreateContext failed\n"); glewDestroyContext(); return 1; } glewExperimental = GL_TRUE; #ifdef GLEW_MX err = glewContextInit(glewGetContext()); #ifdef _WIN32 err = err || wglewContextInit(wglewGetContext()); #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) err = err || glxewContextInit(glxewGetContext()); #endif #else err = glewInit(); #endif if (GLEW_OK != err) { fprintf(stderr, "Error [main]: glewInit failed: %s\n", glewGetErrorString(err)); glewDestroyContext(); return 1; } #if defined(_WIN32) f = fopen("glewinfo.txt", "w"); if (f == NULL) f = stdout; #else f = stdout; #endif fprintf(f, "---------------------------\n"); fprintf(f, " GLEW Extension Info\n"); fprintf(f, "---------------------------\n\n"); fprintf(f, "GLEW version %s\n", glewGetString(GLEW_VERSION)); #if defined(_WIN32) fprintf(f, "Reporting capabilities of pixelformat %d\n", visual); #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) fprintf(f, "Reporting capabilities of display %s, visual 0x%x\n", display == NULL ? getenv("DISPLAY") : display, visual); #endif fprintf(f, "Running on a %s from %s\n", glGetString(GL_RENDERER), glGetString(GL_VENDOR)); fprintf(f, "OpenGL version %s is supported\n", glGetString(GL_VERSION)); glewInfo(); #if defined(_WIN32) wglewInfo(); #else glxewInfo(); #endif if (f != stdout) fclose(f); glewDestroyContext(); return 0; } /* ------------------------------------------------------------------------ */ #if defined(_WIN32) || !defined(__APPLE__) || defined(GLEW_APPLE_GLX) GLboolean glewParseArgs (int argc, char** argv, char** display, int* visual) { int p = 0; while (p < argc) { #if defined(_WIN32) if (!strcmp(argv[p], "-pf") || !strcmp(argv[p], "-pixelformat")) { if (++p >= argc) return GL_TRUE; *display = 0; *visual = strtol(argv[p++], NULL, 0); } else return GL_TRUE; #else if (!strcmp(argv[p], "-display")) { if (++p >= argc) return GL_TRUE; *display = argv[p++]; } else if (!strcmp(argv[p], "-visual")) { if (++p >= argc) return GL_TRUE; *visual = (int)strtol(argv[p++], NULL, 0); } else return GL_TRUE; #endif } return GL_FALSE; } #endif /* ------------------------------------------------------------------------ */ #if defined(_WIN32) HWND wnd = NULL; HDC dc = NULL; HGLRC rc = NULL; GLboolean glewCreateContext (int* pixelformat) { WNDCLASS wc; PIXELFORMATDESCRIPTOR pfd; /* register window class */ ZeroMemory(&wc, sizeof(WNDCLASS)); wc.hInstance = GetModuleHandle(NULL); wc.lpfnWndProc = DefWindowProc; wc.lpszClassName = "GLEW"; if (0 == RegisterClass(&wc)) return GL_TRUE; /* create window */ wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); if (NULL == wnd) return GL_TRUE; /* get the device context */ dc = GetDC(wnd); if (NULL == dc) return GL_TRUE; /* find pixel format */ ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR)); if (*pixelformat == -1) /* find default */ { pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; *pixelformat = ChoosePixelFormat(dc, &pfd); if (*pixelformat == 0) return GL_TRUE; } /* set the pixel format for the dc */ if (FALSE == SetPixelFormat(dc, *pixelformat, &pfd)) return GL_TRUE; /* create rendering context */ rc = wglCreateContext(dc); if (NULL == rc) return GL_TRUE; if (FALSE == wglMakeCurrent(dc, rc)) return GL_TRUE; return GL_FALSE; } void glewDestroyContext () { if (NULL != rc) wglMakeCurrent(NULL, NULL); if (NULL != rc) wglDeleteContext(rc); if (NULL != wnd && NULL != dc) ReleaseDC(wnd, dc); if (NULL != wnd) DestroyWindow(wnd); UnregisterClass("GLEW", GetModuleHandle(NULL)); } /* ------------------------------------------------------------------------ */ #elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX) #include AGLContext ctx, octx; GLboolean glewCreateContext () { int attrib[] = { AGL_RGBA, AGL_NONE }; AGLPixelFormat pf; /*int major, minor; SetPortWindowPort(wnd); aglGetVersion(&major, &minor); fprintf(stderr, "GL %d.%d\n", major, minor);*/ pf = aglChoosePixelFormat(NULL, 0, attrib); if (NULL == pf) return GL_TRUE; ctx = aglCreateContext(pf, NULL); if (NULL == ctx || AGL_NO_ERROR != aglGetError()) return GL_TRUE; aglDestroyPixelFormat(pf); /*aglSetDrawable(ctx, GetWindowPort(wnd));*/ octx = aglGetCurrentContext(); if (GL_FALSE == aglSetCurrentContext(ctx)) return GL_TRUE; /* Needed for Regal on the Mac */ #if defined(GLEW_REGAL) && defined(__APPLE__) RegalMakeCurrent(octx); #endif return GL_FALSE; } void glewDestroyContext () { aglSetCurrentContext(octx); if (NULL != ctx) aglDestroyContext(ctx); } /* ------------------------------------------------------------------------ */ #else /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */ Display* dpy = NULL; XVisualInfo* vi = NULL; XVisualInfo* vis = NULL; GLXContext ctx = NULL; Window wnd = 0; Colormap cmap = 0; GLboolean glewCreateContext (const char* display, int* visual) { int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None }; int erb, evb; XSetWindowAttributes swa; /* open display */ dpy = XOpenDisplay(display); if (NULL == dpy) return GL_TRUE; /* query for glx */ if (!glXQueryExtension(dpy, &erb, &evb)) return GL_TRUE; /* choose visual */ if (*visual == -1) { vi = glXChooseVisual(dpy, DefaultScreen(dpy), attrib); if (NULL == vi) return GL_TRUE; *visual = (int)XVisualIDFromVisual(vi->visual); } else { int n_vis, i; vis = XGetVisualInfo(dpy, 0, NULL, &n_vis); for (i=0; iscreen), 0, 0, 1, 1, 1, 0, 0);*/ cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone); swa.border_pixel = 0; swa.colormap = cmap; wnd = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 1, 1, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap, &swa); /* make context current */ if (!glXMakeCurrent(dpy, wnd, ctx)) return GL_TRUE; return GL_FALSE; } void glewDestroyContext () { if (NULL != dpy && NULL != ctx) glXDestroyContext(dpy, ctx); if (NULL != dpy && 0 != wnd) XDestroyWindow(dpy, wnd); if (NULL != dpy && 0 != cmap) XFreeColormap(dpy, cmap); if (NULL != vis) XFree(vis); else if (NULL != vi) XFree(vi); if (NULL != dpy) XCloseDisplay(dpy); } #endif /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */