arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

glUtil.cpp

Go to the documentation of this file.
00001 /*
00002 * $Id: glUtil.cpp,v 1.6 2003/09/05 06:53:30 jmk Exp $
00003 */
00004 
00005 /*
00006 **  Joe Michael Kniss (c) 2002
00007 **
00008 **  This library is free software; you can redistribute it and/or
00009 **  modify it under the terms of the GNU Lesser General Public
00010 **  License as published by the Free Software Foundation; either
00011 **  version 2.1 of the License, or (at your option) any later version.
00012 **
00013 **  This library is distributed in the hope that it will be useful,
00014 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 **  Lesser General Public License for more details.
00017 **
00018 **  You should have received a copy of the GNU Lesser General Public
00019 **  License along with this library; if not, write to the Free Software
00020 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 // glUtils.cpp
00024 
00025 //#ifdef WIN32
00026 //#  include <windows.h>
00027 //#endif
00028 
00029 #include "glUtil.h"
00030 
00031 #include<GL/glew.h>
00032 
00033 //#include <GL/gl.h>
00034 //#include <GL/glu.h>
00035 //#include <GL/glew.h>
00036 //#include <GL/glut.h>
00037 
00038 #ifdef WIN32
00039 #include <windows.h>
00040 #endif
00041 
00042 #include <iostream>
00043 using namespace std;
00044 
00045 
00046 //=========================================================================
00047 // Report GL Errors
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 /// no-op
00071 bool glErr(std::ostream &os, const char *where, const char *when)
00072 {
00073    return false;
00074 }
00075 #endif
00076 
00077 
00078 //=========================================================================
00079 // Get Last Windows GL (WGL) Error
00080 //=========================================================================
00081 #ifndef NDEBUG // DEBUG mode
00082 #if WIN32
00083 void wglGetLastError(std::ostream &os, const char *where)  //from Sebastian
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       // no error
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)  //from Sebastian
00120 {}
00121 #endif
00122 
00123 //=========================================================================
00124 // Get GL Window Statistics
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 // Get GL Limits
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       //TODO: This is not actually querying ogl for the number of texcoords,
00180       //needs to be fixed
00181       glim.maxTexCoords = glim.maxTexUnits > 8 ? 8 : glim.maxTexUnits;
00182    }
00183 
00184    return glim;
00185 }
00186 
00187 //=========================================================================
00188 // GL Color & Texture Utils
00189 //=========================================================================
00190 /// Set color mask: Use GL_NONE to turn all off and GL_RGBA to turn all on
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       //exit(1);
00203    }
00204 }
00205 
00206 // Returns 'channel' if is GL_RED, GL_GREEN, or GL_BLUE, otherwise
00207 // gives error message and dies.
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          //exit(1);
00216       }
00217 
00218       return channel;
00219 }
00220 
00221 /// Return true if 'channel' is single-channel GLenum
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          //exit(1);
00260    }
00261 
00262    return numChannels;
00263 }
00264 
00265 /// Get the dimensionality of the texture type
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       //exit(1);
00278    }
00279 
00280    return texDim;
00281 }
00282 
00283 #if 0
00284 //=========================================================================
00285 // GL Text Utils
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 // Make Glut Window Current - first call initiallizes this
00300 //=========================================================================
00301 void MakeGlutWindowCurrent()
00302 {
00303    static int glutWinId = glutGetWindow();
00304    glutSetWindow( glutWinId );
00305 }
00306 #endif
00307 
00308 
00309 
00310 //------------------------------------------------------------------------
00311 //C++ 
00312 //                   ________    ____   ___ 
00313 //                  |        \  /    | /  /
00314 //                  +---+     \/     |/  /
00315 //                  +--+|  |\    /|     < 
00316 //                  |  ||  | \  / |  |\  \ 
00317 //                  |      |  \/  |  | \  \ 
00318 //                   \_____|      |__|  \__\
00319 //                       Copyright  2001 
00320 //                      Joe Michael Kniss
00321 //               "All Your Base are Belong to Us"
00322 //-------------------------------------------------------------------------

Send questions, comments, and bug reports to:
jmk