TAPs 0.7.7.3
TAPsOpenGLObj.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsOpenGLObj.cpp
00003 
00004 OpenGLObj class (cpp file).
00005 
00006 SUKITTI PUNAK   (08/14/2009)
00007 ******************************************************************************/
00008 #include "TAPsOpenGLObj.hpp"
00009 // Using Inclusion Model (i.e. definitions are included in declarations)
00010 //                       (this name.cpp is included in name.hpp)
00011 // Each friend is defined directly inside its declaration.
00012 
00013 BEGIN_NAMESPACE_TAPs__OpenGL
00014 //=============================================================================
00015 char        OpenGLObj::m_GL_Version[3]          = {-1,-1,-1};
00016 std::string OpenGLObj::m_GL_TAPsResourcePath    = "";
00017 //=============================================================================
00018 // Constructors
00019 //-----------------------------------------------------------------------------
00020 OpenGLObj::OpenGLObj ()
00021 {
00022     InitOnce();
00023 }
00024 //-----------------------------------------------------------------------------
00025 OpenGLObj::OpenGLObj ( OpenGLObj const &orig )
00026 {
00027     Material = orig.Material;
00028 }
00029 //-----------------------------------------------------------------------------
00030 OpenGLObj::~OpenGLObj ()
00031 {
00032     #ifdef  TAPs_USE_GLSL
00033         ClearShaders();
00034     #endif//TAPs_USE_GLSL
00035 
00036     #ifdef  TAPs_USE_WXWIDGETS
00037         ClearTextures();
00038     #endif//TAPs_USE_WXWIDGETS
00039 }
00040 //-----------------------------------------------------------------------------
00041 std::string OpenGLObj::StrInfo () const
00042 {
00043     std::ostringstream ss;
00044     ss << "-----<<<<< OpenGLObj >>>>>-----";
00045     ss << "\n---<<< Material >>>---\n" << Material;
00046     ss << "\n---<<< Texture >>>---\n" << Texture;
00047     ss << "\n";
00048     return ss.str();
00049 }
00050 //-----------------------------------------------------------------------------
00051 //=============================================================================
00052 // Assignment Operator
00053 //-----------------------------------------------------------------------------
00054 OpenGLObj & OpenGLObj::operator= ( OpenGLObj const &orig )
00055 {   
00056     Material = orig.Material;
00057     return *this;
00058 }
00059 //-----------------------------------------------------------------------------
00060 //=============================================================================
00061 // Draw
00062 //-----------------------------------------------------------------------------
00063 void OpenGLObj::Draw () const
00064 {   
00065     //???
00066 }
00067 //-----------------------------------------------------------------------------
00068 //=============================================================================
00069 // Check OpenGL version
00070 //-----------------------------------------------------------------------------
00071 bool OpenGLObj::InitOnce ()
00072 {   
00073     if      ( m_GL_Version[0] > 0 )     return true;
00074     else if ( m_GL_Version[0] == 0 )    return false;
00075 
00076     SetTAPsResourcePath();
00077 
00078     m_GL_Version[0] = 0;  m_GL_Version[1] = 0;  m_GL_Version[2] = 0;
00079 
00080     #ifdef  TAPs_USE_GLSL
00081         GLenum err = glewInit();
00082         if ( GLEW_OK != err )
00083         {
00084             // Problem: glewInit failed
00085             std::cerr << "Error (FILE:"<<__FILE__<<"; LINE:"<<__LINE__<<"): OpenGL is not supported (" << glewGetErrorString(err) << ")!" << std::endl;
00086             return false;
00087         }
00088         std::cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << "\n";
00089         if (GLEW_VERSION_1_5)   {
00090             std::cout << "OpenGL Version 1.5 is supported.\n";
00091             m_GL_Version[0] = 1;  m_GL_Version[1] = 5;  m_GL_Version[2] = 0;
00092         }
00093         else    {
00094             std::cout << "OpenGL Version 1.5 is NOT supported.\n";
00095         }
00096         if (GLEW_VERSION_2_0)   {
00097             std::cout << "OpenGL Version 2.0 is supported.\n";
00098             m_GL_Version[0] = 2;  m_GL_Version[1] = 0;  m_GL_Version[2] = 0;
00099         }
00100         else    {
00101             std::cout << "OpenGL Version 2.0 is NOT supported.\n";
00102         }
00103         if (GLEW_VERSION_2_1)   {
00104             std::cout << "OpenGL Version 2.1 is supported.\n";
00105             m_GL_Version[0] = 2;  m_GL_Version[1] = 1;  m_GL_Version[2] = 0;
00106         }
00107         else    {
00108             std::cout << "OpenGL Version 2.1 is NOT supported.\n";
00109         }
00110         if (GLEW_VERSION_3_0)   {
00111             std::cout << "OpenGL Version 3.0 is supported.\n";
00112             m_GL_Version[0] = 3;  m_GL_Version[1] = 0;  m_GL_Version[2] = 0;
00113         }
00114         else    {
00115             std::cout << "OpenGL Version 3.0 is NOT supported.\n";
00116         }
00117         if (GLEW_VERSION_3_1)   {
00118             std::cout << "OpenGL Version 3.1 is supported.\n";
00119             m_GL_Version[0] = 3;  m_GL_Version[1] = 1;  m_GL_Version[2] = 0;
00120         }
00121         else {
00122             std::cout << "OpenGL Version 3.1 is NOT supported.\n";
00123         }
00124         if (GLEW_VERSION_3_2)   {
00125             std::cout << "OpenGL Version 3.2 is supported.\n";
00126             m_GL_Version[0] = 3;  m_GL_Version[1] = 2;  m_GL_Version[2] = 0;
00127         }
00128         else {
00129             std::cout << "OpenGL Version 3.2 is NOT supported.\n";
00130         }
00131 
00132     #else //TAPs_USE_GLSL
00133         const GLubyte * glVersion = glGetString(GL_VERSION);
00134         if ( glVersion != NULL ) {
00135             std::string version( "" );
00136             int i = 0;
00137             while ( glVersion[i] != NULL ) {
00138                 version += glVersion[i++];
00139             }
00140             int dot1 = version.find_first_of( "." );
00141             int dot2 = version.find_last_of( "." );
00142             m_GL_Version[0] = atoi( version.substr(0, dot1).c_str() );
00143             m_GL_Version[1] = atoi( version.substr(dot1+1, dot2-dot1-1).c_str() );
00144             m_GL_Version[2] = atoi( version.substr(dot2+1, version.length()-dot2-1).c_str() );
00145             std::cout << "OpenGL Version: " << static_cast<int>(m_GL_Version[0]) << "." 
00146                                             << static_cast<int>(m_GL_Version[1]) << "." 
00147                                             << static_cast<int>(m_GL_Version[2]) << "\n";
00148         }
00149         else {
00150             std::cout << "OpenGL is not supported!\n";
00151         }
00152     #endif//TAPs_USE_GLSL
00153 
00154     if ( m_GL_Version[0] > 0 )  return true;
00155     else                        return false;
00156 }
00157 //=============================================================================
00158 // Set the path to TAPs resource
00159 //-----------------------------------------------------------------------------
00160 void OpenGLObj::SetTAPsResourcePath ()
00161 {
00162     // Look for the path in the environment variable named "TAPsResource".
00163     char * TAPsResourcePath = getenv( "TAPsResource" );
00164     if ( TAPsResourcePath ) {
00165         m_GL_TAPsResourcePath += TAPsResourcePath + std::string("/");
00166     }
00167     else {  // If "TAPsResource" doesn't exist, then use this path.
00168         m_GL_TAPsResourcePath = "TAPsResource/";
00169     }
00170     //std::cout << "The path to TAPs resource: " << m_GL_TAPsResourcePath << "\n";
00171 }
00172 //-----------------------------------------------------------------------------
00173 
00174 
00175 //=============================================================================
00176 #ifdef  TAPs_USE_GLSL
00177 //-----------------------------------------------------------------------------
00178 // Initialize shaders
00179 bool OpenGLObj::InitShaders (
00180     std::string const & shaderFileName,     
00181     bool useVertexShader,                   
00182     bool useGeometryShader,                 
00183     bool useFragmentShader                  
00184 )
00185 {
00186     // Create a GLSL manager
00187     if ( !glslManager.CreateGLSLProgramObject( glslProgObj ) ) {
00188         std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a glsl program object!" << std::endl;
00189         return false;
00190     }
00191 
00192     std::string filename( m_GL_TAPsResourcePath + std::string("GLSL/Shaders/") + shaderFileName );
00193 
00194     // Create and compile shaders from files
00195     if ( useVertexShader ) {
00196         std::string vertFile = filename + std::string(".vert");
00197         if ( !glslManager.AttachShaderFromFile( glslProgObj, vertFile, GL_VERTEX_SHADER ) ) {
00198             std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a vertex shader from file ("<<vertFile<<")!" << std::endl;
00199             return false;
00200         }
00201     }
00202     if ( useGeometryShader ) {
00203         std::string geomFile = filename + std::string(".geom");
00204         if ( !glslManager.AttachShaderFromFile( glslProgObj, geomFile, GL_GEOMETRY_SHADER_EXT ) ) {
00205             std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a geometry shader from file ("<<geomFile<<")!" << std::endl;
00206             return false;
00207         }
00208     }
00209     if ( useFragmentShader ) {
00210         std::string fragFile = filename + std::string(".frag");
00211         if ( !glslManager.AttachShaderFromFile( glslProgObj, fragFile, GL_FRAGMENT_SHADER ) ) {
00212             std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a fragment shader from file ("<<fragFile<<")!" << std::endl;
00213             return false;
00214         }
00215     }
00216 
00217     // Link the glsl program object
00218     if ( !glslManager.Link( glslProgObj ) ) {
00219         std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create link shaders ("<<shaderFileName<<")!" << std::endl;
00220         return false;
00221     }
00222 
00223     return true;
00224 }
00225 
00226 
00227 // Clear shaders
00228 void OpenGLObj::ClearShaders ()
00229 {
00230 }
00231 //-----------------------------------------------------------------------------
00232 #endif//TAPs_USE_GLSL
00233 //=============================================================================
00234 
00235 
00236 
00237 //=============================================================================
00238 #ifdef  TAPs_USE_WXWIDGETS
00239 //-----------------------------------------------------------------------------
00240 // Initialize an 1D texture
00241 //bool OpenGLObj::InitTexture1D ( std::string imageFile )
00242 //{
00243 //  return false;
00244 //}
00245 
00246 // Initialize a 2D texture
00247 bool OpenGLObj::InitTexture2D ( std::string & imageFile )
00248 {
00249     wxImage imgObj( imageFile );
00250     if ( !imgObj.IsOk() ) {
00251         std::cout << "Image from \""<<imageFile<<"\" cannont be initialized!\n";
00252         return false;
00253     }
00254 
00255     // Get texels from the image object
00256     int width   = imgObj.GetWidth();
00257     int height  = imgObj.GetHeight();
00258     std::cout << "\""<<imageFile<<"\" size: [w:" << width << ", h:" << height << "]" << "\n";
00259     int numOfComponents = imgObj.HasAlpha() ? 4 : 3;
00260     GLubyte * texels = new GLubyte[width * height * numOfComponents];
00261     unsigned char * imgData = imgObj.GetData();
00262     unsigned char * imgAlpha = imgObj.GetAlpha();
00263     long count = 0;
00264     int alphaIdx = 0;
00265     for ( int n = 0; n < width*height*3; n+=3 ) {
00266         texels[count++] = /*255 -*/ imgData[n+0];
00267         texels[count++] = /*255 -*/ imgData[n+1];
00268         texels[count++] = /*255 -*/ imgData[n+2];
00269         if ( imgAlpha ) {
00270             texels[count++] = /*255 -*/ imgAlpha[alphaIdx++];
00271         }
00272     }
00273 
00274     // Create texture
00275     GLint texIntFormat = GL_RGB;
00276     GLenum pixelFormat = GL_RGB;
00277     if ( numOfComponents == 4 ) {
00278         texIntFormat = GL_RGBA;
00279         pixelFormat = GL_RGBA;
00280     }
00281     Texture.ChangeTexture( GL_TEXTURE_2D, 0, texIntFormat, width, height, 0, pixelFormat, GL_UNSIGNED_BYTE, texels );
00282 
00283     delete [] texels;
00284 
00285     return true;
00286 }
00287 
00288 // Initialize a 3D texture
00289 //bool OpenGLObj::InitTexture3D ( std::string imageFile )
00290 //{
00291 //  return false;
00292 //}
00293 
00294 // Clear textures
00295 void OpenGLObj::ClearTextures ()
00296 {}
00297 //-----------------------------------------------------------------------------
00298 #endif//TAPs_USE_WXWIDGETS
00299 //=============================================================================
00300 
00301 //=============================================================================
00302 END_NAMESPACE_TAPs__OpenGL
00303 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00304 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines