00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "glUtil.h"
00030
00031 #include<GL/glew.h>
00032
00033
00034
00035
00036
00037
00038 #ifdef WIN32
00039 #include <windows.h>
00040 #endif
00041
00042 #include <iostream>
00043 using namespace std;
00044
00045
00046
00047
00048
00049
00050 #ifndef NDEBUG // DEBUG mode
00051 bool glErr(std::ostream &os, const char *where, const char *when)
00052 {
00053 GLuint errnum;
00054 const char *errstr;
00055 bool state = false;
00056 while (errnum = glGetError())
00057 {
00058
00059 errstr = reinterpret_cast<const char *>(gluErrorString(errnum));
00060 os << errstr;
00061
00062 if(where) os << " at " << where;
00063 if(when) os << "::" << when;
00064
00065 os << "\n";
00066 state = true;
00067 }
00068 return state;
00069 }
00070 #else
00071 bool glErr(std::ostream &os, const char *where, const char *when)
00072 {
00073 return false;
00074 }
00075 #endif
00076
00077
00078
00079
00080
00081 #ifndef NDEBUG // DEBUG mode
00082 #if WIN32
00083 void wglGetLastError(std::ostream &os, const char *where)
00084 {
00085 DWORD err = GetLastError();
00086 switch(err)
00087 {
00088 case ERROR_INVALID_PIXEL_FORMAT:
00089 os << "Win32 Error: ERROR_INVALID_PIXEL_FORMAT ";
00090 break;
00091 case ERROR_NO_SYSTEM_RESOURCES:
00092 os << "Win32 Error: ERROR_NO_SYSTEM_RESOURCES ";
00093 break;
00094 case ERROR_INVALID_DATA:
00095 os << "Win32 Error: ERROR_INVALID_DATA ";
00096 break;
00097 case ERROR_INVALID_WINDOW_HANDLE:
00098 os << "Win32 Error: ERROR_INVALID_WINDOW_HANDLE ";
00099 break;
00100 case ERROR_RESOURCE_TYPE_NOT_FOUND:
00101 os << "Win32 Error: ERROR_RESOURCE_TYPE_NOT_FOUND ";
00102 break;
00103 case ERROR_SUCCESS:
00104
00105 break;
00106 default:
00107 os << "Win32 Error: " << err;
00108 break;
00109 }
00110
00111 if(err>0) {
00112 if(where) os << " at " << where << "\n";
00113 else os << "\n";
00114 }
00115 SetLastError(0);
00116 }
00117 #endif
00118 #else
00119 void wglGetLastError(std::ostream &os, const char *where)
00120 {}
00121 #endif
00122
00123
00124
00125
00126 WinStatsGL getGLWinStats(bool print)
00127 {
00128 WinStatsGL gws;
00129
00130 glGetIntegerv(GL_RED_BITS, &gws.colorBits[0]);
00131 glGetIntegerv(GL_GREEN_BITS, &gws.colorBits[1]);
00132 glGetIntegerv(GL_BLUE_BITS, &gws.colorBits[2]);
00133 glGetIntegerv(GL_ALPHA_BITS, &gws.colorBits[3]);
00134 glGetIntegerv(GL_ACCUM_RED_BITS, &gws.accumBits[0]);
00135 glGetIntegerv(GL_ACCUM_GREEN_BITS, &gws.accumBits[1]);
00136 glGetIntegerv(GL_ACCUM_BLUE_BITS, &gws.accumBits[2]);
00137 glGetIntegerv(GL_ACCUM_ALPHA_BITS, &gws.accumBits[3]);
00138 glGetIntegerv(GL_DEPTH_BITS, &gws.depthBits);
00139 glGetIntegerv(GL_STENCIL_BITS, &gws.stencilBits);
00140 glGetBooleanv(GL_DOUBLEBUFFER, &gws.doubleBuffer);
00141 glGetBooleanv(GL_STEREO, &gws.stereoBuffer);
00142 glGetBooleanv(GL_AUX_BUFFERS, &gws.auxBuffer);
00143
00144 if(print)
00145 {
00146 cerr << "Window Attributes" << endl;
00147 cerr << "-----------------" << endl;
00148 cerr << "color: " << gws.colorBits[0] + gws.colorBits[1] + gws.colorBits[2] + gws.colorBits[3] << " bits";
00149 cerr << " (" << gws.colorBits[0] << "," << gws.colorBits[1] << "," << gws.colorBits[2] << "," << gws.colorBits[3] << ")" << endl;
00150 cerr << "accum: " << gws.accumBits[0] + gws.accumBits[1] + gws.accumBits[2] + gws.accumBits[3] << " bits";
00151 cerr << " (" << gws.accumBits[0] << "," << gws.accumBits[1] << "," << gws.accumBits[2] << "," << gws.accumBits[3] << ")" << endl;
00152 cerr << "depth: " << gws.depthBits << " bits" << endl;
00153 cerr << "stencil: " << gws.stencilBits << " bits" << endl;
00154 cerr << "double: ";
00155 if(gws.doubleBuffer) cerr << "YES" << endl;
00156 else cerr << "NO" << endl;
00157 cerr << "aux: ";
00158 if(gws.auxBuffer) cerr << "YES" << endl;
00159 else cerr << "NO" << endl;
00160 cerr << "stereo : ";
00161 if(gws.stereoBuffer) cerr << "YES" << endl;
00162 else cerr << "NO" << endl;
00163 cerr << "-----------------" << endl;
00164 }
00165
00166 return gws;
00167 }
00168
00169
00170
00171
00172
00173 const LimitsGL getLimitsGL()
00174 {
00175 static LimitsGL glim = {0,0};
00176
00177 if( glim.maxTexUnits == 0 ) {
00178 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &glim.maxTexUnits);
00179
00180
00181 glim.maxTexCoords = glim.maxTexUnits > 8 ? 8 : glim.maxTexUnits;
00182 }
00183
00184 return glim;
00185 }
00186
00187
00188
00189
00190
00191 void setColorMask( GLenum channel )
00192 {
00193 switch(channel){
00194 case GL_NONE: glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); break;
00195 case GL_RED: glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE); break;
00196 case GL_GREEN: glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE); break;
00197 case GL_BLUE: glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_FALSE); break;
00198 case GL_ALPHA: glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE ); break;
00199 case GL_RGB: glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE ); break;
00200 case GL_RGBA: glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); break;
00201 default: cerr << "setColorMask(...):\n\tUnsupported channel\n";
00202
00203 }
00204 }
00205
00206
00207
00208 GLenum validateSingleRGBChannel( GLenum channel )
00209 {
00210 if( channel != GL_RED &&
00211 channel != GL_GREEN &&
00212 channel != GL_BLUE ) {
00213 cerr << "validateSingleChannel(...) Error :\n"
00214 << "\t'channel' (" << channel << ") is not a single-channel R, G, or B.\n";
00215
00216 }
00217
00218 return channel;
00219 }
00220
00221
00222 bool isSingleChannel( GLenum channel )
00223 {
00224 bool okay=false;
00225
00226 switch( channel ) {
00227 case GL_RED:
00228 case GL_GREEN:
00229 case GL_BLUE:
00230 case GL_ALPHA:
00231 case GL_LUMINANCE:
00232 okay = true;
00233 break;
00234 default:
00235 okay = false;
00236 }
00237
00238 return okay;
00239 }
00240
00241 int getNumChannels( GLenum format )
00242 {
00243 int numChannels;
00244 switch(format) {
00245 case GL_COLOR_INDEX:
00246 case GL_STENCIL_INDEX:
00247 case GL_DEPTH_COMPONENT:
00248 case GL_RED:
00249 case GL_GREEN:
00250 case GL_BLUE:
00251 case GL_ALPHA:
00252 case GL_LUMINANCE: numChannels = 1; break;
00253
00254 case GL_RGB: numChannels = 3; break;
00255
00256 case GL_RGBA: numChannels = 4; break;
00257
00258 default: cerr << "getNumChannels Error: Unknown format " << format << endl;
00259
00260 }
00261
00262 return numChannels;
00263 }
00264
00265
00266 int getTexDim( GLenum texType )
00267 {
00268 int texDim;
00269
00270 switch(texType) {
00271 case GL_TEXTURE_1D: texDim = 1; break;
00272 case GL_TEXTURE_2D: texDim = 2; break;
00273 case GL_TEXTURE_3D: texDim = 3; break;
00274 case GL_TEXTURE_CUBE_MAP_ARB: texDim = 2; break;
00275 default:
00276 cerr << "getTexDim(" << texType << ") Error:\n\tUnrecognized texture target name.\n";
00277
00278 }
00279
00280 return texDim;
00281 }
00282
00283 #if 0
00284
00285
00286
00287 void renderBitmapString( float x, float y, int spacing, void *font, char *string)
00288 {
00289 char *c;
00290 int x1=x;
00291 for (c=string; *c != '\0'; c++) {
00292 glRasterPos2f(x1,y);
00293 glutBitmapCharacter(font, *c);
00294 x1 = x1 + glutBitmapWidth(font,*c) + spacing;
00295 }
00296 }
00297
00298
00299
00300
00301 void MakeGlutWindowCurrent()
00302 {
00303 static int glutWinId = glutGetWindow();
00304 glutSetWindow( glutWinId );
00305 }
00306 #endif
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322