![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsGLObj.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (09/10/2010) 00009 UPDATE (12/20/2010) 00010 ******************************************************************************/ 00011 #include "STAPsGLObj.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs__OpenGL 00017 //============================================================================= 00018 char GLObj::m_GL_Version[3] = {-1,-1,-1}; 00019 std::string GLObj::m_GL_TAPsResourcePath = ""; 00020 std::string GLObj::m_GL_TAPsGLSLShadersResourcePath = ""; 00021 //============================================================================= 00022 // Constructors 00023 //----------------------------------------------------------------------------- 00024 GLObj::GLObj () 00025 { 00026 InitOnce(); 00027 } 00028 //----------------------------------------------------------------------------- 00029 //GLObj::GLObj ( GLObj const &orig ) 00030 //{ 00031 //Material = orig.Material; 00032 //} 00033 //----------------------------------------------------------------------------- 00034 GLObj::~GLObj () 00035 { 00036 //ClearShaders(); 00037 00038 #ifdef TAPs_USE_WXWIDGETS 00039 //ClearTextures(); 00040 #endif//TAPs_USE_WXWIDGETS 00041 } 00042 //----------------------------------------------------------------------------- 00043 std::string GLObj::StrInfo () const 00044 { 00045 std::ostringstream ss; 00046 ss << "-----<<<<< GLObj >>>>>-----"; 00047 //ss << "\n---<<< Material >>>---\n" << Material; 00048 //ss << "\n---<<< Texture >>>---\n" << Texture; 00049 ss << "\n"; 00050 return ss.str(); 00051 } 00052 //----------------------------------------------------------------------------- 00053 //============================================================================= 00054 // Assignment Operator 00055 //----------------------------------------------------------------------------- 00056 //GLObj & GLObj::operator= ( GLObj const &orig ) 00057 //{ 00058 // //Material = orig.Material; 00059 // return *this; 00060 //} 00061 //----------------------------------------------------------------------------- 00062 //============================================================================= 00063 // Draw 00064 //----------------------------------------------------------------------------- 00065 //void GLObj::Draw () const 00066 //{ 00067 //??? 00068 //} 00069 //----------------------------------------------------------------------------- 00070 //============================================================================= 00071 // Check OpenGL version 00072 //----------------------------------------------------------------------------- 00073 bool GLObj::InitOnce () 00074 { 00075 if ( m_GL_Version[0] > 0 ) return true; 00076 else if ( m_GL_Version[0] == 0 ) return false; 00077 00078 SetTAPsResourcePath(); 00079 00080 m_GL_Version[0] = 0; m_GL_Version[1] = 0; m_GL_Version[2] = 0; 00081 00082 GLenum err = glewInit(); 00083 if ( GLEW_OK != err ) 00084 { 00085 // Problem: glewInit failed 00086 std::cerr << "Error (FILE:"<<__FILE__<<"; LINE:"<<__LINE__<<"): OpenGL is not supported (" << glewGetErrorString(err) << ")!" << std::endl; 00087 return false; 00088 } 00089 std::cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << "\n"; 00090 if (GLEW_VERSION_1_5) { 00091 std::cout << "OpenGL Version 1.5 is supported.\n"; 00092 m_GL_Version[0] = 1; m_GL_Version[1] = 5; m_GL_Version[2] = 0; 00093 } 00094 else { 00095 std::cout << "OpenGL Version 1.5 is NOT supported.\n"; 00096 } 00097 if (GLEW_VERSION_2_0) { 00098 std::cout << "OpenGL Version 2.0 is supported.\n"; 00099 m_GL_Version[0] = 2; m_GL_Version[1] = 0; m_GL_Version[2] = 0; 00100 } 00101 else { 00102 std::cout << "OpenGL Version 2.0 is NOT supported.\n"; 00103 } 00104 if (GLEW_VERSION_2_1) { 00105 std::cout << "OpenGL Version 2.1 is supported.\n"; 00106 m_GL_Version[0] = 2; m_GL_Version[1] = 1; m_GL_Version[2] = 0; 00107 } 00108 else { 00109 std::cout << "OpenGL Version 2.1 is NOT supported.\n"; 00110 } 00111 if (GLEW_VERSION_3_0) { 00112 std::cout << "OpenGL Version 3.0 is supported.\n"; 00113 m_GL_Version[0] = 3; m_GL_Version[1] = 0; m_GL_Version[2] = 0; 00114 } 00115 else { 00116 std::cout << "OpenGL Version 3.0 is NOT supported.\n"; 00117 } 00118 if (GLEW_VERSION_3_1) { 00119 std::cout << "OpenGL Version 3.1 is supported.\n"; 00120 m_GL_Version[0] = 3; m_GL_Version[1] = 1; m_GL_Version[2] = 0; 00121 } 00122 else { 00123 std::cout << "OpenGL Version 3.1 is NOT supported.\n"; 00124 } 00125 if (GLEW_VERSION_3_2) { 00126 std::cout << "OpenGL Version 3.2 is supported.\n"; 00127 m_GL_Version[0] = 3; m_GL_Version[1] = 2; m_GL_Version[2] = 0; 00128 } 00129 else { 00130 std::cout << "OpenGL Version 3.2 is NOT supported.\n"; 00131 } 00132 if (GLEW_VERSION_3_3) { 00133 std::cout << "OpenGL Version 3.3 is supported.\n"; 00134 m_GL_Version[0] = 3; m_GL_Version[1] = 3; m_GL_Version[2] = 0; 00135 } 00136 else { 00137 std::cout << "OpenGL Version 3.3 is NOT supported.\n"; 00138 } 00139 if (GLEW_VERSION_4_0) { 00140 std::cout << "OpenGL Version 4.0 is supported.\n"; 00141 m_GL_Version[0] = 4; m_GL_Version[1] = 0; m_GL_Version[2] = 0; 00142 } 00143 else { 00144 std::cout << "OpenGL Version 4.0 is NOT supported.\n"; 00145 } 00146 if (GLEW_VERSION_4_1) { 00147 std::cout << "OpenGL Version 4.1 is supported.\n"; 00148 m_GL_Version[0] = 4; m_GL_Version[1] = 1; m_GL_Version[2] = 0; 00149 } 00150 else { 00151 std::cout << "OpenGL Version 4.1 is NOT supported.\n"; 00152 } 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 GLObj::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 // Initialize shaders 00176 bool GLObj::InitShaders ( 00177 std::string const & shaderFileName, 00178 bool useVertexShader, 00179 bool useGeometryShader, 00180 bool useFragmentShader 00181 ) 00182 { 00183 // Create a GLSL manager 00184 if ( !glslManager.CreateGLSLProgramObject( glslProgObj ) ) { 00185 std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a glsl program object!" << std::endl; 00186 return false; 00187 } 00188 00189 std::stringstream ss; 00190 ss << m_GL_TAPsResourcePath << "GLSL/Shaders/" << "v" 00191 << GetVersionMajorNumber() << "_" 00192 << GetVersionMinorNumber() << "_" 00193 << GetVersionMinorMinorNumber() << "/"; 00194 m_GL_TAPsGLSLShadersResourcePath = ss.str(); 00195 std::string filename( m_GL_TAPsGLSLShadersResourcePath + shaderFileName ); 00196 00197 // Create and compile shaders from files 00198 if ( useVertexShader ) { 00199 std::string vertFile = filename + std::string(".vert"); 00200 if ( !glslManager.AttachShaderFromFile( glslProgObj, vertFile, GL_VERTEX_SHADER ) ) { 00201 std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a vertex shader from file ("<<vertFile<<")!" << std::endl; 00202 return false; 00203 } 00204 } 00205 if ( useGeometryShader ) { 00206 std::string geomFile = filename + std::string(".geom"); 00207 if ( !glslManager.AttachShaderFromFile( glslProgObj, geomFile, GL_GEOMETRY_SHADER_EXT ) ) { 00208 std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a geometry shader from file ("<<geomFile<<")!" << std::endl; 00209 return false; 00210 } 00211 } 00212 if ( useFragmentShader ) { 00213 std::string fragFile = filename + std::string(".frag"); 00214 if ( !glslManager.AttachShaderFromFile( glslProgObj, fragFile, GL_FRAGMENT_SHADER ) ) { 00215 std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create a fragment shader from file ("<<fragFile<<")!" << std::endl; 00216 return false; 00217 } 00218 } 00219 00220 // Link the glsl program object 00221 if ( !glslManager.Link( glslProgObj ) ) { 00222 std::cout << "Error ("<<__FILE__<<";"<<__LINE__<<"): GLSL couldn't create link shaders ("<<shaderFileName<<")!" << std::endl; 00223 return false; 00224 } 00225 00226 return true; 00227 } 00228 00229 00230 // Clear shaders 00231 //void GLObj::ClearShaders () 00232 //{ 00233 //} 00234 00235 00236 00237 //============================================================================= 00238 #ifdef TAPs_USE_WXWIDGETS 00239 //----------------------------------------------------------------------------- 00240 // Initialize an 1D texture 00241 //bool GLObj::InitTexture1D ( std::string imageFile ) 00242 //{ 00243 // return false; 00244 //} 00245 00246 // Initialize a 2D texture 00247 //bool GLObj::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 GLObj::InitTexture3D ( std::string imageFile ) 00290 //{ 00291 // return false; 00292 //} 00293 00294 // Clear textures 00295 //void GLObj::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----+----