src/gui/opengl/glutils.cpp

Go to the documentation of this file.
00001 /*  $Id: glutils.cpp 14974 2007-09-14 12:13:37Z dicuccio $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Mike DiCuccio
00027  *
00028  * File Description:
00029  *    OpenGL utility functions
00030  */
00031 
00032 
00033 #include <ncbi_pch.hpp>
00034 #include <corelib/ncbifile.hpp>
00035 #include <corelib/ncbi_system.hpp>
00036 #include <gui/opengl.h>
00037 #include <gui/opengl/glutils.hpp>
00038 #include <gui/opengl/glexception.hpp>
00039 
00040 #ifdef NCBI_OS_UNIX
00041 #include <connect/ncbi_pipe.hpp>
00042 #endif
00043 
00044 #ifdef NCBI_OS_DARWIN
00045 #include <Carbon/Carbon.h>
00046 #endif
00047 
00048 
00049 BEGIN_NCBI_SCOPE
00050 
00051 
00052 // static accelerated state - default to 'not determined'
00053 CGlUtils::EAccelState CGlUtils::m_Accel = CGlUtils::eNotDetermined;
00054 
00055 
00056 //
00057 // access the accelerated flag
00058 // this defaults to autodetect, and is user-overridable
00059 //
00060 CGlUtils::EAccelState CGlUtils::GetAccelerated(void)
00061 {
00062     if ( m_Accel != eNotDetermined ) {
00063         return m_Accel;
00064     }
00065 
00066     const char* str = reinterpret_cast<const char*> (glGetString(GL_RENDERER));
00067     if ( !str ) {
00068         return eNotDetermined;
00069     }
00070 
00071     _TRACE("GL_VERSION = "    << glGetString(GL_VERSION));
00072     _TRACE("GL_RENDERER = "   << glGetString(GL_RENDERER));
00073     _TRACE("GL_EXTENSIONS = " << glGetString(GL_EXTENSIONS));
00074 
00075     // Solaris software renderer returns:
00076     // GL_RENDERER = Sun dpa software renderer, VIS
00077     string s(str);
00078     if (s.find("software renderer") != string::npos) {
00079         LOG_POST(Info
00080                  << "CGlUtils::GetAccelerated(): "
00081                  "auto-detected non-hardware-accelerated platform");
00082         m_Accel = eNotAccelerated;
00083     } else {
00084         LOG_POST(Info
00085                  << "CGlUtils::GetAccelerated(): "
00086                  "auto-detected hardware-accelerated platform");
00087         m_Accel = eAccelerated;
00088     }
00089 
00090     return m_Accel;
00091 }
00092 
00093 
00094 //
00095 // check for and report OpenGL errors
00096 //
00097 enum EGlDiagMode {
00098     eUndefined,
00099     eIgnore,
00100     eLogPost,
00101     eThrow,
00102     eAbort
00103 };
00104 
00105 void CGlUtils::CheckGlError(void)
00106 {
00107     GLint error = glGetError();
00108     if (error == GL_NO_ERROR) {
00109         return;
00110     }
00111 
00112     static EGlDiagMode mode = eUndefined;
00113     if ( mode == eUndefined ) {
00114         const char* value = getenv("NCBI_GBENCH_GLERROR");
00115         if ( !value ) {
00116             mode = eIgnore;
00117         } else if (strcmp(value, "ABORT") == 0) {
00118             mode = eAbort;
00119         } else if (strcmp(value, "LOGPOST") == 0) {
00120             mode = eLogPost;
00121         } else if (strcmp(value, "THROW") == 0) {
00122             mode = eThrow;
00123         } else {
00124             mode = eIgnore;
00125         }
00126     }
00127 
00128     string msg;
00129     switch ( error ) {
00130     default:
00131         msg = "CGlUtils::CheckGlError(): unknown error";
00132         break;
00133 
00134     case GL_INVALID_OPERATION:
00135         msg = "CGlUtils::CheckGlError(): invalid operation";
00136         break;
00137 
00138     case GL_INVALID_ENUM:
00139         msg = "CGlUtils::CheckGlError(): invalid enum";
00140         break;
00141 
00142     case GL_INVALID_VALUE:
00143         msg = "CGlUtils::CheckGlError(): invalid value";
00144         break;
00145 
00146     case GL_STACK_OVERFLOW:
00147         msg = "CGlUtils::CheckGlError(): stack overflow";
00148         break;
00149 
00150     case GL_STACK_UNDERFLOW:
00151         msg = "CGlUtils::CheckGlError(): stack underflow";
00152         break;
00153 
00154     case GL_OUT_OF_MEMORY:
00155         msg = "CGlUtils::CheckGlError(): out of memory";
00156         break;
00157     }
00158 
00159     switch (mode) {
00160     case eUndefined:
00161     case eIgnore:
00162     default:
00163         break;
00164 
00165     case eLogPost:
00166         LOG_POST(Error << msg);
00167         break;
00168 
00169     case eAbort:
00170         // abort
00171         LOG_POST(Error << msg);
00172         Abort();
00173         break;
00174 
00175     case eThrow:
00176         // throw
00177         NCBI_THROW(COpenGLException, eGlError, msg);
00178         break;
00179     }
00180 }
00181 
00182 
00183 #ifdef _DEBUG
00184 
00185 //
00186 // dumpState()
00187 // this is a debugging function designed to show a bunch of OpenGL enables
00188 //
00189 void CGlUtils::DumpState(void)
00190 {
00191     LOG_POST(Info << "OpenGL Vendor: " << glGetString(GL_VENDOR));
00192     LOG_POST(Info << "OpenGL Renderer: " << glGetString(GL_RENDERER));
00193     LOG_POST(Info << "OpenGL Version: " << glGetString(GL_VERSION));
00194     LOG_POST(Info << "OpenGL Extensions: " << glGetString(GL_EXTENSIONS));
00195     LOG_POST(Info << "\n");
00196 
00197     GLint viewport[4];
00198     float modelview[16];
00199     float projection[16];
00200     float color[4];
00201 
00202     glGetIntegerv(GL_VIEWPORT, viewport);
00203     glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
00204     glGetFloatv(GL_PROJECTION_MATRIX, projection);
00205     glGetFloatv(GL_CURRENT_COLOR, color);
00206 
00207     GLint red_bits = 0;
00208     GLint green_bits = 0;
00209     GLint blue_bits = 0;
00210     GLint alpha_bits = 0;
00211     GLint depth_bits = 0;
00212     GLint stencil_bits = 0;
00213     glGetIntegerv(GL_RED_BITS,     &red_bits);
00214     glGetIntegerv(GL_GREEN_BITS,   &green_bits);
00215     glGetIntegerv(GL_BLUE_BITS,    &blue_bits);
00216     glGetIntegerv(GL_ALPHA_BITS,   &alpha_bits);
00217     glGetIntegerv(GL_DEPTH_BITS,   &depth_bits);
00218     glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
00219 
00220     LOG_POST(Info << "Buffers:");
00221     LOG_POST(Info << "  Color:"
00222              << " Red=" << red_bits << " bits"
00223              << " Green=" << green_bits << " bits"
00224              << " Blue=" << blue_bits << " bits"
00225              << " Alpha=" << alpha_bits << " bits");
00226     LOG_POST(Info << "  Depth: " << depth_bits << " bits");
00227     LOG_POST(Info << "  Stencil: " << alpha_bits << " bits");
00228 
00229     LOG_POST(Info << "Viewport: "
00230         << viewport[0] << ", " << viewport[1] << ", "
00231         << viewport[2] << ", " << viewport[3]);
00232 
00233     int i;
00234     int j;
00235     LOG_POST(Info << "Projection matrix:");
00236     for (i = 0;  i < 4;  ++i) {
00237         string msg;
00238         for (j = 0;  j < 4;  ++j) {
00239             // remember, OpenGL matrices are transposed!
00240             msg += NStr::DoubleToString(projection[j * 4+i]) + " ";
00241         }
00242         LOG_POST(Info << msg);
00243     }
00244 
00245     LOG_POST(Info << "Modelview matrix:");
00246     for (i = 0;  i < 4;  ++i) {
00247         string msg;
00248         for (j = 0;  j < 4;  ++j) {
00249             // remember, OpenGL matrices are transposed!
00250             msg += NStr::DoubleToString(modelview[j * 4+i]) + " ";
00251         }
00252         LOG_POST(Info << msg);
00253     }
00254 
00255     LOG_POST(Info << "Current draw color: "
00256         << color[0] << ", " << color[1] << ", "
00257         << color[2] << ", " << color[3]);
00258 
00259     LOG_POST(Info << "Lighting:      "
00260              << (glIsEnabled(GL_LIGHTING)   ? "enabled" : "disabled"));
00261     LOG_POST(Info << "Depth Testing: "
00262              << (glIsEnabled(GL_DEPTH_TEST) ? "enabled" : "disabled"));
00263     LOG_POST(Info << "Face Culling:  "
00264              << (glIsEnabled(GL_CULL_FACE)  ? "enabled" : "disabled"));
00265     LOG_POST(Info << "Blending:      "
00266              << (glIsEnabled(GL_BLEND)      ? "enabled" : "disabled"));
00267     LOG_POST(Info << "Alpha Testing: "
00268              << (glIsEnabled(GL_ALPHA_TEST) ? "enabled" : "disabled"));
00269     LOG_POST(Info << "2D Texture:    "
00270              << (glIsEnabled(GL_TEXTURE_2D) ? "enabled" : "disabled"));
00271 }
00272 
00273 #endif
00274 
00275 END_NCBI_SCOPE
00276 
00277 

Generated on Sun Dec 6 22:28:49 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:21:01 2009 by modify_doxy.py rev. 173732