TAPs 0.7.7.3
STAPsGLSLSupport.hpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 STAPsGLSLSupport.hpp
00003 
00004 Based on 3DLabs Inc. Ltd. and adapted from aGLSL.h by Martin Christen.
00005 This class relies on using glew to initialize OpenGL.
00006 Since Windows XP uses OpenGL 1.1, so glewInit() is needed to initialize 
00007 the OpenGL to the highest version that the graphics card can support.
00008 
00009 SUKITTI PUNAK   (09/09/2010)
00010 UPDATE          (09/10/2010)
00011 ******************************************************************************/
00029 #ifndef STAPs_GLSL_SUPPORT_HPP
00030 #define STAPs_GLSL_SUPPORT_HPP
00031 
00032 //-----------------------------------------------------------------------------
00033 // Operating System Check
00034 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
00035     #define TAPs_GLSL_WINDOWS
00036     #include <windows.h>
00037 #elif defined(__MACOSX__)
00038     #define TAPs_GLSL_MACOSX
00039     #error "MacOSX is not supported!"
00040 #elif defined(linux) || defined(__linux) || defined(__linux__)
00041     #define TAPs_GLSL_LINUX
00042     #error "LINUX is not supported!"
00043 #else
00044     #error "This OS is not supported!"
00045 #endif
00046 //-----------------------------------------------------------------------------
00047 //#include "TAPs_GL_Ext.hpp"
00048 
00049 //#include "../Core/TAPsLib.hpp"
00050 //#define GLEW_MX
00051 
00052 #define GLEW_STATIC 1
00053 #include <GL/glew.h>
00054 #include <GL/wglew.h>
00055 #include <GL/gl.h>
00056 //#include <GL/glut.h>
00057 //#include <GL/glu.h>
00058 //#include <GL/glext.h>
00059 //#include <GL/wglext.h>
00060 
00061 #include "../Core/TAPsLib.hpp"
00062 #include "../OpenGL/STAPsGLFns.hpp"
00063 //#include "../OpenGL/TAPsOpenGLFns.hpp"
00064 
00065 #include <iostream>
00066 #include <fstream>
00067 #include <vector>
00068 
00069 
00070 #ifdef WIN32 /*[*/
00071 #include <io.h>
00072 #endif /*]*/
00073 
00074 #include <stdlib.h>
00075 #include <stdio.h>
00076 #include <string.h>
00077 #include <fcntl.h>
00078 
00079 // For wxWidgets
00080 #ifdef TAPs_USE_WXWIDGETS
00081 #include <wx/wx.h>
00082 #endif
00083 
00084 //-----------------------------------------------------------------------------
00085 BEGIN_NAMESPACE_TAPs__OpenGL
00086 //=============================================================================
00087 class GLSLShader
00088 {
00089     //----------------------------------------------------------------
00090     friend class GLSLProgramObject; // i.e. GLSL Program
00091     //----------------------------------------------------------------
00092 public:
00093     //----------------------------------------------------------------
00094     GLSLShader ();
00095     ~GLSLShader ();
00096     int Load ( const char *fileName );  // read file returns
00097                                         //   0 if everything is ok,
00098                                         //  -1 if file not found,
00099                                         //  -2 if file is empty
00100                                         //  -3 if no memory
00101     // Load shader from char array, make sure shader is 0 terminated!
00102     void LoadFromMemory ( const char *shader );
00103     bool Compile ();            // compile shader
00104     char * GetCompilerLog ();   // get compiler log messages
00105     GLint GetAttribLoc ( char * cpAttribName ); // get localtion of the attribute
00106 
00107     void SetName ( std::string const & name )   { m_name = name; }
00108     std::string const & GetName ()  { return m_name; }
00109     //----------------------------------------------------------------
00110 protected:
00111     //----------------------------------------------------------------
00112     // Data Members
00113     int         m_iShaderType;      
00114     GLuint      m_uiShaderObject;   
00115     GLchar *    m_cpShaderSource;   
00116     bool        m_bCompileStatus;   
00117     GLchar *    m_cpCompilerLog;    
00118     bool        m_bMemAlloc;        
00119     std::string m_name;             
00120     //----------------------------------------------------------------
00121 };
00122 //=============================================================================
00123 class GLSLVertexShader : public GLSLShader
00124 {
00125 public:
00126     GLSLVertexShader ();
00127     ~GLSLVertexShader ();
00128 };
00129 //=============================================================================
00130 class GLSLGeometryShader : public GLSLShader
00131 {
00132 public:
00133     GLSLGeometryShader ();
00134     ~GLSLGeometryShader ();
00135 };
00136 //=============================================================================
00137 class GLSLFragmentShader : public GLSLShader
00138 {
00139 public:
00140     GLSLFragmentShader ();
00141     ~GLSLFragmentShader ();
00142 };
00143 //=============================================================================
00144 // GLSL Program
00145 class GLSLProgramObject
00146 {
00147 public:
00148     //----------------------------------------------------------------
00149     GLSLProgramObject ();
00150     ~GLSLProgramObject ();
00151     //----------------------------------------------------------------
00152     // Add a vertex or fragment shader
00153     void AddShader ( GLSLShader * glslShader );
00154     //----------------------------------------------------------------
00155     bool Link ();               // link all shaders
00156     char * GetLinkerLog ();     // get linker messages
00157     //----------------------------------------------------------------
00158     // Begin this shader.  OpenGL calls will go through shader
00159     void BeginGLSL ();
00160     // Stop using this shader. OpenGL calls will go through regular pipeline.
00161     void EndGLSL ();
00162     //----------------------------------------------------------------
00163     bool IsGLSLEnabled ();  // returns true if GLSL is enabled
00164     //----------------------------------------------------------------
00165     GLuint GetProgramObject () {
00166         return m_uiProgramObject;
00167     }
00168     //----------------------------------------------------------------
00169     GLuint const GetProgramObject () const {
00170         return m_uiProgramObject;
00171     }
00172     //----------------------------------------------------------------
00173     // To set (uniform) Textures to GLSL Shader
00174     //   only glUniform1i and glUniform1iv can be used to load texture(s)
00175     //---------------------------------------------------------------
00176     // Set Uniform Variables to GLSL Shader
00177     //---------------------------------------------------------------
00178     // Float
00179     bool SetUniform1f   ( char * var, GLfloat x );
00180     bool SetUniform2f   ( char * var, GLfloat x, GLfloat y );
00181     bool SetUniform3f   ( char * var, GLfloat x, GLfloat y, GLfloat z );
00182     bool SetUniform4f   ( char * var, GLfloat x, GLfloat y, GLfloat z, GLfloat w );
00183     //-----------------------
00184     // Float Vector
00185     bool SetUniform1fv  ( char * var, GLsizei count, GLfloat * val );
00186     bool SetUniform2fv  ( char * var, GLsizei count, GLfloat * val );
00187     bool SetUniform3fv  ( char * var, GLsizei count, GLfloat * val );
00188     bool SetUniform4fv  ( char * var, GLsizei count, GLfloat * val );
00189     //-----------------------
00190     // Integer
00191     bool SetUniform1i   ( char * var, GLint x );
00192     bool SetUniform2i   ( char * var, GLint x, GLint y );
00193     bool SetUniform3i   ( char * var, GLint x, GLint y, GLint z );
00194     bool SetUniform4i   ( char * var, GLint x, GLint y, GLint z, GLint w );
00195     //-----------------------
00196     // Integer Vector
00197     bool SetUniform1iv  ( char * var, GLsizei count, GLint * val );
00198     bool SetUniform2iv  ( char * var, GLsizei count, GLint * val );
00199     bool SetUniform3iv  ( char * var, GLsizei count, GLint * val );
00200     bool SetUniform4iv  ( char * var, GLsizei count, GLint * val );
00201     //-----------------------
00202     // Unsigned Integer
00203     bool SetUniform1ui  ( char * var, GLuint x );
00204     bool SetUniform2ui  ( char * var, GLuint x, GLuint y );
00205     bool SetUniform3ui  ( char * var, GLuint x, GLuint y, GLuint z );
00206     bool SetUniform4ui  ( char * var, GLuint x, GLuint y, GLuint z, GLuint w );
00207     //-----------------------
00208     // Unsigned Integer Vector
00209 //  bool SetUniform1uiv ( char * var, GLsizei count, GLuint * val );
00210 //  bool SetUniform2uiv ( char * var, GLsizei count, GLuint * val );
00211 //  bool SetUniform3uiv ( char * var, GLsizei count, GLuint * val );
00212 //  bool SetUniform4uiv ( char * var, GLsizei count, GLuint * val );
00213     //-----------------------
00214     bool SetUniformMatrix2fv    ( char * var, GLsizei count, GLboolean transpose, GLfloat * val );
00215     bool SetUniformMatrix3fv    ( char * var, GLsizei count, GLboolean transpose, GLfloat * val );
00216     bool SetUniformMatrix4fv    ( char * var, GLsizei count, GLboolean transpose, GLfloat * val );
00217     //----------------------------------------------------------------
00218     // Get Uniform Variables from GLSL Shader
00219     void GetUniformfv   ( char * var, GLfloat * val );  // float
00220     void GetUniformiv   ( char * var, GLint *   val );  // int
00221     void GetUniformuiv  ( char * var, GLuint *  val );  // unsigned int
00222     //----------------------------------------------------------------
00223     // Set Vertex Attributes to GLSL Shader
00224     bool SetVertexAttrib1f ( GLuint index, GLfloat x );
00225     bool SetVertexAttrib2f ( GLuint index, GLfloat x, GLfloat y );
00226     bool SetVertexAttrib3f ( GLuint index, GLfloat x, GLfloat y, GLfloat z );
00227     bool SetVertexAttrib4f ( GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w );
00228     //----------------------------------------------------------------
00229     void MemageMemory () { m_bManageMemory = true; }
00230     //----------------------------------------------------------------
00231     // Turn On/Off All Shaders
00232     static void UseShader ( bool b ) { m_stcbShaderActive = b; }
00233 
00234     void SetName ( std::string const & name )   { m_name = name; }
00235     std::string const & GetName ()  { return m_name; }
00236     //----------------------------------------------------------------
00237 private:
00238     //----------------------------------------------------------------
00239     // Member Functions
00240     GLint   GetUniLoc( const GLchar *name );    // get location of the uniform variable
00241     //----------------------------------------------------------------
00242     // Data Members
00243     GLuint      m_uiProgramObject;  
00244     bool        m_bLinkStatus;      
00245     GLchar *    m_cpLinkerLog;      
00246     std::vector< GLSLShader * > m_vShaderList;  // list of all shaders
00247     bool        m_bManageMemory;    
00248     static bool m_stcbShaderActive; 
00249     std::string m_name;             
00250     //----------------------------------------------------------------
00251 };
00252 //=============================================================================
00303 class GLSLShaderManager
00304 {
00305 public:
00306     GLSLShaderManager ();
00307     ~GLSLShaderManager ();
00308     //---------------------------------------------------------------
00309     bool CreateGLSLProgramObject ( GLSLProgramObject * (&progObj) );
00310     bool AttachShaderFromFile ( GLSLProgramObject * progObj, char const * shaderFile, GLuint shaderType );
00311     inline bool AttachShaderFromFile ( GLSLProgramObject * progObj, std::string & shaderFile, GLuint shaderType )
00312     { return AttachShaderFromFile ( progObj, shaderFile.c_str(), shaderType ); }
00313     bool Link ( GLSLProgramObject * progObj );
00314     //---------------------------------------------------------------
00315     // Load vertex/geometry/fragment shader(s) from file(s)
00320     GLSLProgramObject * LoadVertexGeometryAndFragmentShadersFromFile ( 
00321         char * vertFile, char * geomFile, char * fragFile );
00322     GLSLProgramObject * LoadVertexAndFragmentShadersFromFile ( 
00323         char * vertFile, char * fragFile )
00324     {   return LoadFromFile( vertFile, fragFile );  }
00325     //GLSLProgramObject * LoadVertexAndGeometryFragmentShadersFromFile ( 
00326     //  char * vertFile, char * geomFile );
00327     //GLSLProgramObject * LoadGeometryAndFragmentShadersFromFile ( 
00328     //  char * geomFile, char * fragFile );
00329     GLSLProgramObject * LoadFromFile ( char * vertFile, char * fragFile );
00330     GLSLProgramObject * LoadOnlyAVertexShaderFromFile ( char * vertFile );
00331     GLSLProgramObject * LoadOnlyAGeometryShaderFromFile ( char * geomFile );
00332     GLSLProgramObject * LoadOnlyAFragmentShaderFromFile ( char * fragFile );
00333     //---------------------------------------------------------------
00334     // Load vertex/fragment shader from memory
00335     GLSLProgramObject * LoadFromMemory ( const char * vertMem, const char * fragMem );
00336     //---------------------------------------------------------------
00337     // Delete a program object
00338     bool Delete ( GLSLProgramObject * obj );
00339     //----------------------------------------------------------------
00340     std::vector< GLSLProgramObject * > & GetProgramObjectList () {
00341         return m_vProgramObjectList;
00342     }
00343     //----------------------------------------------------------------
00344     std::vector< GLSLProgramObject * > const & GetProgramObjectList () const {
00345         return m_vProgramObjectList;
00346     }
00347     //----------------------------------------------------------------
00348 protected:
00349     bool LoadVertexShaderFromFile   ( GLSLVertexShader *   vertShader, const char * vertFile );
00350     bool LoadGeometryShaderFromFile ( GLSLGeometryShader * geomShader, const char * geomFile );
00351     bool LoadFragmentShaderFromFile ( GLSLFragmentShader * fragShader, const char * fragFile );
00352     bool CompileVertexShaderFromFile    ( GLSLVertexShader *   vertShader, const char * vertFile );
00353     bool CompileGeometryShaderFromFile  ( GLSLGeometryShader * geomShader, const char * vertFile );
00354     bool CompileFragmentShaderFromFile  ( GLSLFragmentShader * fragShader, const char * vertFile );
00355     //----------------------------------------------------------------
00356 private:
00357     std::vector< GLSLProgramObject * >  m_vProgramObjectList;
00358     //===============================================================
00359     //---------------------------------------------------------------
00360     // For Attributes / Uniform Variables
00361     //===========================================
00362     // Uniform Variables
00363     //-------------------------------------------
00364     // Scalars
00365     std::vector< float >            m_f1Uniform;
00366     std::vector< int >              m_i1Uniform;
00367     std::vector< bool >             m_b1Uniform;
00368     //-------------------------------------------
00369     // Vectors
00370     std::vector< Vector2<float> >   m_f2Uniform;
00371     std::vector< Vector2<int> >     m_i2Uniform;
00372     std::vector< Vector2<bool> >    m_b2Uniform;
00373     std::vector< Vector3<float> >   m_f3Uniform;
00374     std::vector< Vector3<int> >     m_i3Uniform;
00375     std::vector< Vector3<bool> >    m_b3Uniform;
00376     std::vector< Vector4<float> >   m_f4Uniform;
00377     std::vector< Vector4<int> >     m_i4Uniform;
00378     std::vector< Vector4<bool> >    m_b4Uniform;
00379     //-------------------------------------------
00380     // Matrices
00381     std::vector< ColMatrix2x2<float> >  m_m2x2Uniform;
00382     std::vector< ColMatrix3x3<float> >  m_m3x3Uniform;
00383     std::vector< ColMatrix4x4<float> >  m_m4x4Uniform;
00384     //-------------------------------------------
00385     // Samplers
00386     // --------
00387     // sampler1D        Access a 1D texture
00388     // sampler2D        Access a 2D texture
00389     // sampler3D        Access a 3D texture
00390     // samplerCube      Access a cube-map texture
00391     // sampler1DShadow  Access a 1D depth texture with comparison
00392     // sampler2DShadow  Access a 2D depth texture with comparison
00393     //-------------------------------------------
00394     //===========================================
00395     // Attributes
00396     //-------------------------------------------
00397     // Scalars
00398     std::vector< short >            m_s1VertexAttrib;
00399     std::vector< float >            m_f1VertexAttrib;
00400     std::vector< double >           m_d1VertexAttrib;
00401     //-------------------------------------------
00402     // 2D Vectors
00403     std::vector< Vector2<short> >   m_s2VertexAttrib;
00404     std::vector< Vector2<float> >   m_f2VertexAttrib;
00405     std::vector< Vector2<double> >  m_d2VertexAttrib;
00406     // 3D Vectors
00407     std::vector< Vector3<short> >   m_s3VertexAttrib;
00408     std::vector< Vector3<float> >   m_f3VertexAttrib;
00409     std::vector< Vector3<double> >  m_d3VertexAttrib;
00410     // 4D Vectors
00411     std::vector< Vector4<short> >   m_s4VertexAttrib;
00412     std::vector< Vector4<float> >   m_f4VertexAttrib;
00413     std::vector< Vector4<double> >  m_d4VertexAttrib;
00414     std::vector< Vector4<char> >    m_b4VertexAttrib;
00415     std::vector< Vector4<int> >     m_i4VertexAttrib;
00416     std::vector< Vector4<unsigned char> >   m_ub4VertexAttrib;
00417     std::vector< Vector4<unsigned short> >  m_us4VertexAttrib;
00418     std::vector< Vector4<unsigned int> >    m_ui4VertexAttrib;
00419     //---------------------------------------------------------------
00420     //===============================================================
00421     //-------------------------------------------------------------------------
00422 };  // End Class GLSLShaderManager
00423 //=============================================================================
00425 // * \class GLSLSupport STAPsGLSLSupport.hpp "OpenGL/STAPsGLSLSupport.hpp"
00426 // * \brief To simplify the process of loading/compiling/linking shaders, this
00427 // */
00428 //class GLSLSupport
00429 //{
00430 //public:
00431 //  // Data Members
00432 //  GLuint VAO;             //!< the vertex array object
00433 //  GLunit VBO_Positions;   //!< the vertex buffer object for positions
00434 //  GLunit VBO_Indices;     //!< the vertex buffer object for indices
00435 //};
00436 //=============================================================================
00437 // Classless functions to initialize OpenGL Extensions and check for GLSL and OpenGL
00438 bool GFnInitGLSL    ();
00439 bool GFnCheckGLSL   ();
00440 //=============================================================================
00441 END_NAMESPACE_TAPs__OpenGL
00442 //-----------------------------------------------------------------------------
00443 // Include definition if TAPs_USE_EXPORT is not defined
00444 #if !defined( TAPs_USE_EXPORT )
00445     #include "STAPsGLSLSupport.cpp"
00446 #endif
00447 //-----------------------------------------------------------------------------
00448 #endif
00449 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00450 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines