Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ARB_Multisample.cpp

Go to the documentation of this file.
00001 /*========================================================================================
00002 
00003   Name:   ARB_multisample.cpp
00004         Author: Colt "MainRoach" McAnlis
00005         Date:   4/29/04
00006         Desc:   This file contains the context to load a WGL extension from a string
00007                     As well as collect the sample format available based upon the graphics card.
00008 
00009 ========================================================================================*/
00010 
00011 #include <windows.h>
00012 #include <gl/gl.h>
00013 #include <gl/glu.h>
00014 
00015 #include "arb_multisample.h"
00016 
00017 // Declairations We'll Use
00018 #define WGL_SAMPLE_BUFFERS_ARB           0x2041
00019 #define WGL_SAMPLES_ARB                      0x2042
00020 
00021 bool    arbMultisampleSupported = false;
00022 int             arbMultisampleFormat    = 0;
00023 
00024 // WGLisExtensionSupported: This Is A Form Of The Extension For WGL
00025 bool WGLisExtensionSupported(const char *extension)
00026 {
00027         const size_t extlen = strlen(extension);
00028         const char *supported = NULL;
00029 
00030         // Try To Use wglGetExtensionStringARB On Current DC, If Possible
00031         PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
00032 
00033         if (wglGetExtString)
00034                 supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
00035 
00036         // If That Failed, Try Standard Opengl Extensions String
00037         if (supported == NULL)
00038                 supported = (char*)glGetString(GL_EXTENSIONS);
00039 
00040         // If That Failed Too, Must Be No Extensions Supported
00041         if (supported == NULL)
00042                 return false;
00043 
00044         // Begin Examination At Start Of String, Increment By 1 On False Match
00045         for (const char* p = supported; ; p++)
00046         {
00047                 // Advance p Up To The Next Possible Match
00048                 p = strstr(p, extension);
00049 
00050                 if (p == NULL)
00051                         return false;                                                                                                                   // No Match
00052 
00053                 // Make Sure That Match Is At The Start Of The String Or That
00054                 // The Previous Char Is A Space, Or Else We Could Accidentally
00055                 // Match "wglFunkywglExtension" With "wglExtension"
00056 
00057                 // Also, Make Sure That The Following Character Is Space Or NULL
00058                 // Or Else "wglExtensionTwo" Might Match "wglExtension"
00059                 if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' '))
00060                         return true;                                                                                                                    // Match
00061         }
00062 }
00063 
00064 // InitMultisample: Used To Query The Multisample Frequencies
00065 bool InitMultisample(HINSTANCE hInstance,HWND hWnd,PIXELFORMATDESCRIPTOR pfd)
00066 {  
00067          // See If The String Exists In WGL!
00068         if (!WGLisExtensionSupported("WGL_ARB_multisample"))
00069         {
00070                 arbMultisampleSupported=false;
00071                 return false;
00072         }
00073 
00074         // Get Our Pixel Format
00075         PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");  
00076         if (!wglChoosePixelFormatARB) 
00077         {
00078                 arbMultisampleSupported=false;
00079                 return false;
00080         }
00081 
00082         // Get Our Current Device Context
00083         HDC hDC = GetDC(hWnd);
00084 
00085         int             pixelFormat;
00086         int             valid;
00087         UINT    numFormats;
00088         float   fAttributes[] = {0,0};
00089 
00090         // These Attributes Are The Bits We Want To Test For In Our Sample
00091         // Everything Is Pretty Standard, The Only One We Want To 
00092         // Really Focus On Is The SAMPLE BUFFERS ARB And WGL SAMPLES
00093         // These Two Are Going To Do The Main Testing For Whether Or Not
00094         // We Support Multisampling On This Hardware.
00095         int iAttributes[] =
00096         {
00097                 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
00098                 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
00099                 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
00100                 WGL_COLOR_BITS_ARB,24,
00101                 WGL_ALPHA_BITS_ARB,8,
00102                 WGL_DEPTH_BITS_ARB,16,
00103                 WGL_STENCIL_BITS_ARB,0,
00104                 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
00105                 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
00106                 WGL_SAMPLES_ARB,4,
00107                 0,0
00108         };
00109 
00110         // First We Check To See If We Can Get A Pixel Format For 4 Samples
00111         valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00112  
00113         // If We Returned True, And Our Format Count Is Greater Than 1
00114         if (valid && numFormats >= 1)
00115         {
00116                 arbMultisampleSupported = true;
00117                 arbMultisampleFormat = pixelFormat;     
00118                 return arbMultisampleSupported;
00119         }
00120 
00121         // Our Pixel Format With 4 Samples Failed, Test For 2 Samples
00122         iAttributes[19] = 2;
00123         valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00124         if (valid && numFormats >= 1)
00125         {
00126                 arbMultisampleSupported = true;
00127                 arbMultisampleFormat = pixelFormat;      
00128                 return arbMultisampleSupported;
00129         }
00130           
00131         // Return The Valid Format
00132         return  arbMultisampleSupported;
00133 }

Generated on Fri Aug 20 19:19:42 2004 for 3d Controls by doxygen 1.3.6