00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <windows.h>
00020 #include <gl\gl.h>
00021 #include <gl\glu.h>
00022 #include <gl\glaux.h>
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
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
00079 title.Format("OpenGl - %d Errors",nbError);
00080 MessageBox(NULL,fullText,title,0);
00081
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 {
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
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
00139 PROC wglGetExtString = wglGetProcAddress("wglGetExtensionsStringARB");
00140
00141 if (wglGetExtString) supported = ((char*(__stdcall*)(HDC))wglGetExtString)(wglGetCurrentDC());
00142
00143
00144 if (!supported) supported = (char*)glGetString(GL_EXTENSIONS);
00145
00146
00147 if (!supported ) return FALSE;
00148
00149
00150 for (const char* p = supported; ; p++)
00151 {
00152
00153 p = strstr(p, extension);
00154 if (!p) return false;
00155
00156
00157
00158
00159
00160
00161
00162 if ((p==supported || p[-1]==' ') && (p[extlen]=='\0' || p[extlen]==' ')) return true;
00163 }
00164 }
00165
00166
00167
00168
00169
00170 #include "wglext.h"
00171 #include "glext.h"
00172
00173 int OpenGL_InitMultisample(HWND hWnd)
00174 {
00175
00176 if (!OpenGL_ExtensionSupported("WGL_ARB_multisample")) return FALSE;
00177
00178
00179 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
00180 if (!wglChoosePixelFormatARB) return 0;
00181
00182
00183 HDC hDC = GetDC(hWnd);
00184
00185 int pixelFormat;
00186 int valid;
00187 UINT numFormats;
00188 float fAttributes[] = {0,0};
00189
00190
00191
00192
00193
00194
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
00211 valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00212
00213
00214 if (valid && numFormats >= 1) return pixelFormat;
00215
00216
00217 iAttributes[19] = 2;
00218 valid = wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixelFormat,&numFormats);
00219 if (valid && numFormats >= 1) return pixelFormat;
00220
00221 return 0;
00222 }
00223
00224
00225
00226 #define PI_ 3.14159265358979323846
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
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
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
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 }