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

openGL_tools.cpp

Go to the documentation of this file.
00001 /* *************************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2003-2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 ************************************************************************************* */
00018 
00019 #include <windows.h>
00020 #include <gl\gl.h>                   // Header for OpenGL32 Lib
00021 #include <gl\glu.h>                  // GLu32 Lib
00022 #include <gl\glaux.h>                // GLaux Lib
00023 
00024 #include "MyString.h"
00025 #include "openGL_tools.h"
00026 #include "MyDebug.h"
00027 #include <math.h>
00028 
00035 BOOL OpenGL_TestError(BOOL resultOnMessageBox,BOOL resultOnMyDebug,HWND hWnd) 
00036 {
00037         MyString fullText;
00038         MyString text;
00039         BOOL next = TRUE;
00040         int nbError=0;
00041         while(next)
00042         {
00043                 int error = glGetError();
00044                 //on peut aussi utiliser "gluErrorString", mais on pert le tittle
00045                 switch (error)
00046                 {
00047                 case GL_NO_ERROR: next=FALSE; break;
00048                 case GL_INVALID_ENUM: 
00049                         text="GL_INVALID_ENUM - An unacceptable value is specified for an enumerated argument.";
00050                         break;
00051                 case GL_INVALID_VALUE: 
00052                         text="GL_INVALID_VALUE - A numeric argument is out of range.";
00053                         break;
00054                 case GL_INVALID_OPERATION:
00055                         text="GL_INVALID_OPERATION - The specified operation is not allowed in the current state.";
00056                         break;
00057                 case GL_STACK_OVERFLOW: 
00058                         text="GL_STACK_OVERFLOW - This function would cause a stack overflow.";
00059                         break;
00060                 case GL_STACK_UNDERFLOW: 
00061                         text="GL_STACK_UNDERFLOW - This function would cause a stack underflow.";
00062                         break;
00063                 case GL_OUT_OF_MEMORY: 
00064                         text="GL_OUT_OF_MEMORY - There is not enough memory left to execute the function.";
00065                         break;
00066                 }
00067                 if (error!=GL_NO_ERROR)
00068                 {
00069                         nbError++;
00070                         fullText.AddFormat("#%d - %s\n",nbError,(char *)text);
00071                 }       
00072         }
00073         if (nbError)
00074         {
00075                 if (resultOnMessageBox)
00076                 {
00077                         MyString title;
00078                         //if (hWnd) KillTimer(hWnd,0);
00079                         title.Format("OpenGl - %d Errors",nbError);
00080                         MessageBox(NULL,fullText,title,0);
00081                         //SetTimer(hWnd,0,timer,NULL);
00082                 }
00083                 if (resultOnMyDebug)
00084                 {
00085                         if (nbError>1)
00086                         MyDebug::SendLine("glError","(%d error)\n%s",nbError,(char *)fullText);
00087                         else MyDebug::SendLine("glError",fullText);
00088                 }
00089                 return TRUE;
00090         }
00091         return FALSE;
00092 }
00093 
00094 void OpenGL_ViewVersionExtansions(char *extensions,MyString &result)
00095 {
00096         MyString temp;
00097         result="";
00098         for (int i=0;i<(int)strlen(extensions);i++) 
00099         {
00100                 char letter = extensions[i];
00101                 if (letter==' ')
00102                 {
00103                         result<<"\n* "<<temp;
00104                         temp="";
00105                 }
00106                 else temp<<letter;
00107         }
00108         if (temp!="") result<<"\n* "<<temp;
00109 }
00110 
00111 void OpenGL_ViewVersion()
00112 {   // fonction dois etre appeler apres l'initialisation d'openGL sinon ne retourne rien
00113         char *extensions = (char *)glGetString(GL_EXTENSIONS);
00114         MyString temp,resultExt;
00115         temp<<"Vendor:\t\t"<<(char *)glGetString(GL_VENDOR)<<"\n";
00116         temp<<"Render:\t\t"<<(char *)glGetString(GL_RENDERER)<<"\n";
00117         temp<<"Version:\t\t"<<(char *)glGetString(GL_VERSION)<<"\n";
00118         temp<<"Version(GLU):\t"<<(char *)gluGetString(GLU_VERSION)<<"\n";
00119         extensions = (char *)glGetString(GL_EXTENSIONS);
00120         OpenGL_ViewVersionExtansions(extensions,resultExt);
00121         temp<<"Extensions:"<<resultExt;
00122         extensions = (char *)gluGetString(GLU_EXTENSIONS);
00123         OpenGL_ViewVersionExtansions(extensions,resultExt);
00124         temp<<"\nExtensions(GLU):"<<resultExt;
00125 
00126         MessageBox(NULL,temp,"Info OpenGL",0);          
00127 }
00128 
00129 // pour la gestion des extanssion permetant la gestion de l'anti-aliasing
00130 #define WGL_SAMPLE_BUFFERS_ARB           0x2041
00131 #define WGL_SAMPLES_ARB                      0x2042
00132 
00133 BOOL OpenGL_ExtensionSupported(const char *extension)
00134 {
00135         const size_t extlen = strlen(extension);
00136         const char *supported = NULL;
00137 
00138         // Try To Use wglGetExtensionStringARB On Current DC, If Possible
00139         PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
00140 
00141         if (wglGetExtString) supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
00142 
00143         // If That Failed, Try Standard Opengl Extensions String
00144         if (!supported) supported = (char*)glGetString(GL_EXTENSIONS);
00145 
00146         // If That Failed Too, Must Be No Extensions Supported
00147         if (!supported ) return FALSE;
00148 
00149         // Begin Examination At Start Of String, Increment By 1 On False Match
00150         for (const char* p = supported; ; p++)
00151         {
00152                 // Advance p Up To The Next Possible Match
00153                 p = strstr(p, extension);
00154                 if (!p) return false; // No Match
00155 
00156                 // Make Sure That Match Is At The Start Of The String Or That
00157                 // The Previous Char Is A Space, Or Else We Could Accidentally
00158                 // Match "wglFunkywglExtension" With "wglExtension"
00159 
00160                 // Also, Make Sure That The Following Character Is Space Or NULL
00161                 // Or Else "wglExtensionTwo" Might Match "wglExtension"
00162                 if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' ')) return true; // Match
00163         }
00164 }
00165 
00166 /* InitMultisample: Used To Query The Multisample Frequencies ***********************
00167         retour pixelFormat -ou- 0 si impossible
00168         
00169 ********************************************************************************** */
00170 #include "wglext.h"             //WGL extensions
00171 #include "glext.h"              //GL extensions
00172 
00173 int OpenGL_InitMultisample(HWND hWnd)
00174 {  
00175         // See If The String Exists In WGL!
00176         if (!OpenGL_ExtensionSupported("WGL_ARB_multisample")) return FALSE;
00177 
00178         // Get Our Pixel Format
00179         PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");  
00180         if (!wglChoosePixelFormatARB) return 0;
00181 
00182         // Get Our Current Device Context
00183         HDC hDC = GetDC(hWnd);
00184 
00185         int             pixelFormat;
00186         int             valid;
00187         UINT    numFormats;
00188         float   fAttributes[] = {0,0};
00189 
00190         // These Attributes Are The Bits We Want To Test For In Our Sample
00191         // Everything Is Pretty Standard, The Only One We Want To 
00192         // Really Focus On Is The SAMPLE BUFFERS ARB And WGL SAMPLES
00193         // These Two Are Going To Do The Main Testing For Whether Or Not
00194         // We Support Multisampling On This Hardware.
00195         int iAttributes[] =
00196         {
00197                 WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
00198                 WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
00199                 WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
00200                 WGL_COLOR_BITS_ARB,24,
00201                 WGL_ALPHA_BITS_ARB,8,
00202                 WGL_DEPTH_BITS_ARB,16,
00203                 WGL_STENCIL_BITS_ARB,0,
00204                 WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
00205                 WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
00206                 WGL_SAMPLES_ARB,4,
00207                 0,0
00208         };
00209 
00210         // First We Check To See If We Can Get A Pixel Format For 4 Samples
00211         valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00212 
00213         // If We Returned True, And Our Format Count Is Greater Than 1
00214         if (valid && numFormats >= 1) return  pixelFormat;      
00215 
00216         // Our Pixel Format With 4 Samples Failed, Test For 2 Samples
00217         iAttributes[19] = 2;
00218         valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00219         if (valid && numFormats >= 1) return pixelFormat;
00220         
00221         return  0; // Return The Valid Format
00222 }
00223 
00224 
00225 // from URL: http://www.opengl.org/resources/code/basics/redbook/index.html
00226 #define PI_ 3.14159265358979323846
00227 
00228 /* accFrustum()
00229 * The first 6 arguments are identical to the glFrustum() call.
00230 *  
00231 * pixdx and pixdy are anti-alias jitter in pixels. 
00232 * Set both equal to 0.0 for no anti-alias jitter.
00233 * eyedx and eyedy are depth-of field jitter in pixels. 
00234 * Set both equal to 0.0 for no depth of field effects.
00235 *
00236 * focus is distance from eye to plane in focus. 
00237 * focus must be greater than, but not equal to 0.0.
00238 *
00239 * Note that accFrustum() calls glTranslatef().  You will 
00240 * probably want to insure that your ModelView matrix has been 
00241 * initialized to identity before calling accFrustum().
00242 */
00243 void accFrustum(GLdouble left, GLdouble right, GLdouble bottom, 
00244                                 GLdouble top, GLdouble znear, GLdouble zfar, GLdouble pixdx, 
00245                                 GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus)
00246 {
00247         GLdouble xwsize, ywsize; 
00248         GLdouble dx, dy;
00249         GLint viewport[4];
00250 
00251         glGetIntegerv (GL_VIEWPORT, viewport);
00252 
00253         xwsize = right - left;
00254         ywsize = top - bottom;
00255 
00256         dx = -(pixdx*xwsize/(GLdouble)viewport[2] + eyedx*znear/focus);
00257         dy = -(pixdy*ywsize/(GLdouble)viewport[3] + eyedy*znear/focus);
00258 
00259         glMatrixMode(GL_PROJECTION);
00260         glLoadIdentity();
00261         glFrustum (left+dx, right+dx, bottom+dy, top+dy, znear, zfar);
00262         glMatrixMode(GL_MODELVIEW);
00263         glLoadIdentity();
00264         glTranslatef (-eyedx, -eyedy, 0.0);
00265 }
00266 
00267 /* accPerspective()
00268 * 
00269 * The first 4 arguments are identical to the gluPerspective() call.
00270 * pixdx and pixdy are anti-alias jitter in pixels. 
00271 * Set both equal to 0.0 for no anti-alias jitter.
00272 * eyedx and eyedy are depth-of field jitter in pixels. 
00273 * Set both equal to 0.0 for no depth of field effects.
00274 *
00275 * focus is distance from eye to plane in focus. 
00276 * focus must be greater than, but not equal to 0.0.
00277 *
00278 * Note that accPerspective() calls accFrustum().
00279 */
00280 void accPerspective(GLdouble fovy, GLdouble aspect, 
00281                                         GLdouble znear, GLdouble zfar, GLdouble pixdx, GLdouble pixdy, 
00282                                         GLdouble eyedx, GLdouble eyedy, GLdouble focus)
00283 {
00284         GLdouble fov2,left,right,bottom,top;
00285 
00286         fov2 = ((fovy*PI_) / 180.0) / 2.0;
00287 
00288         top = znear / (cos(fov2) / sin(fov2));
00289         bottom = -top;
00290 
00291         right = top * aspect;
00292         left = -right;
00293 
00294         accFrustum (left, right, bottom, top, znear, zfar,
00295                 pixdx, pixdy, eyedx, eyedy, focus);
00296 }

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