gKitGL
GLShaderObject.h
00001 
00002 #ifndef _GLSHADER_OBJECT_H
00003 #define _GLSHADER_OBJECT_H
00004 
00005 #include <vector>
00006 #include <string>
00007 
00008 #include "GL/GLPlatform.h"
00009 #include "GLResource.h"
00010 #include "TextFile.h"
00011 
00012 
00013 namespace gk {
00014 
00015 //! utilisation interne. abstraction d'un shader object, ressource openGL.
00016 class GLShaderObject : public GLResource
00017 {
00018 private:
00019     // non copyable
00020     GLShaderObject( );
00021     GLShaderObject( const GLShaderObject& );
00022     GLShaderObject& operator= ( const GLShaderObject& );
00023 
00024 protected:
00025     TextFile *m_text;
00026 
00027     unsigned int m_type;
00028     bool m_errors;
00029 
00030 public:
00031     //! types de shaders.
00032     enum
00033     {
00034         VERTEX= 0,      //!< type d'un vertex shader,
00035         FRAGMENT,       //!< type d'un fragment shader,
00036         GEOMETRY,       //!< type d'un geometry shader,
00037         CONTROL,        //!< type d'un control shader,
00038         EVALUATION,     //!< type d'un evaluation shader.
00039         SHADERTYPE_LAST
00040     };
00041     
00042     static
00043     GLenum ShaderType[];
00044     
00045     static
00046     const char *ShaderTypeString[];
00047     
00048     //! constructeur, 
00049     //! \param type correspond a une contante gk::GLShaderObject::VERTEX, etc.
00050     GLShaderObject( const unsigned int type );
00051     
00052     //! destructeur.
00053     virtual ~GLShaderObject( )
00054     {
00055         delete m_text;
00056     }
00057 
00058     //! ajoute une definition au source. "#define 'definition' 'value'".
00059     int pushDefinition( const std::string& definition, const std::string& value= "" )
00060     {
00061         if(m_text == NULL)
00062             return -1;
00063         return m_text->define(definition, value);
00064     }
00065 
00066     //! remplace l'ensemble de definitions.
00067     int setDefinitions( const std::vector<std::string>& definitions )
00068     {
00069         if(m_text == NULL)
00070             return -1;
00071         return m_text->setDefinitions(definitions);
00072     }
00073     
00074     //! renvoie les definitions.
00075     const std::vector<std::string> *definitions( )
00076     {
00077         if(m_text == NULL)
00078             return NULL;
00079         return m_text->definitions();
00080     }
00081     
00082     //! ajoute le contenu d'un fichier source.
00083     int pushSource( const TextFile *text )
00084     {
00085         if(text == NULL)
00086             return 0;
00087         
00088         if(m_text == NULL)
00089         {
00090             //~ printf("ShaderObject: source '%s'...\n", text->name().c_str());
00091             m_text= new TextFile(text->name());
00092             if(m_text == NULL)
00093                 return -1;
00094         }
00095         
00096         return m_text->include(text);
00097     }
00098 
00099     //! renvoie le source du shader.
00100     TextFile *source( ) const
00101     {
00102         return m_text;
00103     }
00104     
00105     //! affiche les sources du shader.
00106     void printSource( ) const
00107     {
00108         if(m_text == NULL)
00109             return;
00110         m_text->print();
00111     }
00112 
00113     //! affiche les erreurs de compilation du shader.
00114     void printErrors( );
00115 
00116     //! cree l'objet openGL.
00117     int createGLResource( );
00118     //! detruit l'objet openGL.
00119     int releaseGLResource( );
00120 
00121     //! reinitialise l'etat du shader.
00122     int clear( )
00123     {
00124         delete m_text;
00125         m_text= NULL;
00126         m_errors= false;
00127         return 0;
00128     }
00129     
00130     //! renvoie vrai ou faux, si erreurs de lecture du source.
00131     bool errors( ) const
00132     {
00133         return m_errors;
00134     }
00135 
00136     //! renvoie le type du shader.
00137     unsigned int type( ) const
00138     {
00139         return m_type;
00140     }
00141 };
00142 
00143 
00144 //! representation d'un vertex shader object, objet openGL.
00145 class GLVertexShader : public GLShaderObject
00146 {
00147 public:
00148     //! constructeur par defaut.
00149     GLVertexShader( )
00150         :
00151         GLShaderObject(GLShaderObject::VERTEX)
00152     {}
00153 
00154     //! destructeur.
00155     ~GLVertexShader( ) {}
00156 };
00157 
00158 //! representation d'un fragment shader object, objet openGL.
00159 class GLFragmentShader: public GLShaderObject
00160 {
00161 public:
00162     //! constructeur par defaut.
00163     GLFragmentShader( )
00164         :
00165         GLShaderObject(GLShaderObject::FRAGMENT)
00166     {}
00167 
00168     //! destructeur.
00169     ~GLFragmentShader( ) {}
00170 };
00171 
00172 
00173 #if defined GK_OPENGL3 || defined GK_OPENGL4
00174 //! representation d'un geometry shader object. objet openGL.
00175 class GLGeometryShader : public GLShaderObject
00176 {
00177 public:
00178     GLGeometryShader( )
00179         :
00180         GLShaderObject(GLShaderObject::GEOMETRY)
00181     {}
00182 
00183     ~GLGeometryShader( ) {}
00184 };
00185 #endif
00186 
00187 #ifdef GK_OPENGL4
00188 //! representation d'un tesselation control shader object. objet openGL.
00189 class GLControlShader : public GLShaderObject
00190 {
00191 public:
00192     GLControlShader( )
00193         :
00194         GLShaderObject(GLShaderObject::CONTROL)
00195     {}
00196 
00197     ~GLControlShader( ) {}
00198 };
00199 
00200 //! representation d'un tesselation evaluation shader object. objet openGL.
00201 class GLEvaluationShader : public GLShaderObject
00202 {
00203 public:
00204     GLEvaluationShader( )
00205         :
00206         GLShaderObject(GLShaderObject::EVALUATION)
00207     {}
00208 
00209     ~GLEvaluationShader( ) {}
00210 };
00211 #endif
00212 
00213 }       // namespace
00214 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerator Friends