00001
00002 #ifndef _GLSHADER_OBJECT_H
00003 #define _GLSHADER_OBJECT_H
00004
00005 #include <vector>
00006 #include <string>
00007
00008 #include "GLPlatform.h"
00009 #include "GLResource.h"
00010 #include "TextFile.h"
00011 #include "TextFileIO.h"
00012
00013
00014 namespace gk {
00015
00016 class GLShaderProgram;
00017 class GLContext;
00018
00019
00020 class GLShaderObject : public GLResource
00021 {
00022 protected:
00023 TextFile *m_text;
00024
00025 GLenum m_type;
00026 GLuint m_name;
00027 bool m_errors;
00028
00029 GLShaderObject( const GLShaderObject& );
00030 GLShaderObject& operator= ( const GLShaderObject& );
00031
00032 public:
00033 friend class GLShaderProgram;
00034 friend class GLContext;
00035
00036
00037 GLShaderObject( const GLenum type )
00038 :
00039 m_text(NULL),
00040 m_type(type),
00041 m_name(0),
00042 m_errors(false)
00043 {}
00044
00045
00046 virtual ~GLShaderObject( )
00047 {
00048 delete m_text;
00049 }
00050
00051
00052 int pushDefinition( const std::string& definition, const std::string& value= "" )
00053 {
00054 if(m_text == NULL)
00055 return -1;
00056 return m_text->define(definition, value);
00057 }
00058
00059
00060 int pushSource( const TextFile *text )
00061 {
00062 if(text == NULL)
00063 return 0;
00064 if(m_text == NULL)
00065 m_text= new TextFile(text->name());
00066 if(m_text == NULL)
00067 return -1;
00068
00069 return m_text->include(text);
00070 }
00071
00072
00073 void printSource( ) const
00074 {
00075 if(m_text == NULL)
00076 return;
00077 m_text->print();
00078 }
00079
00080
00081 void printErrors( );
00082
00083
00084 int createGLResource( );
00085
00086 int releaseGLResource( );
00087
00088
00089 bool errors( ) const
00090 {
00091 return m_errors;
00092 }
00093
00094
00095 GLenum type( ) const
00096 {
00097 return m_type;
00098 }
00099
00100
00101 GLuint name( ) const
00102 {
00103 return m_name;
00104 }
00105 };
00106
00107
00108
00109 class GLVertexShader : public GLShaderObject
00110 {
00111 public:
00112
00113 GLVertexShader( )
00114 :
00115 GLShaderObject(GL_VERTEX_SHADER)
00116 {}
00117
00118
00119 GLVertexShader( const TextFile *file )
00120 :
00121 GLShaderObject(GL_VERTEX_SHADER)
00122 {
00123 pushSource(file);
00124 }
00125
00126
00127 GLVertexShader( const std::string& filename )
00128 :
00129 GLShaderObject(GL_VERTEX_SHADER)
00130 {
00131 #ifdef VERBOSE
00132 printf("loading vertex shader '%s'...\n", filename.c_str());
00133 #endif
00134 TextFile *source= TextFileIO::read(filename, "vertex");
00135 if(filename.empty() == false && source == NULL)
00136 {
00137 printf("GLVertexShader( ): error reading source '%s'.\n",
00138 filename.c_str());
00139 m_errors= true;
00140 }
00141
00142 pushSource(source);
00143 }
00144
00145
00146 ~GLVertexShader( ) {}
00147 };
00148
00149
00150 class GLFragmentShader: public GLShaderObject
00151 {
00152 public:
00153
00154 GLFragmentShader( )
00155 :
00156 GLShaderObject(GL_FRAGMENT_SHADER)
00157 {}
00158
00159
00160 GLFragmentShader( const TextFile *file )
00161 :
00162 GLShaderObject(GL_FRAGMENT_SHADER)
00163 {
00164 pushSource(file);
00165 }
00166
00167
00168 GLFragmentShader( const std::string& filename )
00169 :
00170 GLShaderObject(GL_FRAGMENT_SHADER)
00171 {
00172 #ifdef VERBOSE
00173 printf("loading fragment shader '%s'...\n", filename.c_str());
00174 #endif
00175 TextFile *source= TextFileIO::read(filename, "fragment");
00176 if(filename.empty() == false && source == NULL)
00177 {
00178 printf("GLFragmentShader( ): error reading source '%s'.\n",
00179 filename.c_str());
00180 m_errors= true;
00181 }
00182
00183 pushSource(source);
00184 }
00185
00186
00187 ~GLFragmentShader( ) {}
00188 };
00189
00190
00191 #if defined GK_OPENGL3 || defined GK_OPENGL4
00192
00193 class GLGeometryShader : public GLShaderObject
00194 {
00195 public:
00196 GLGeometryShader( )
00197 :
00198 GLShaderObject(GL_GEOMETRY_SHADER)
00199 {}
00200
00201 GLGeometryShader( const TextFile *file )
00202 :
00203 GLShaderObject(GL_GEOMETRY_SHADER)
00204 {
00205 pushSource(file);
00206 }
00207
00208 GLGeometryShader( const std::string& filename )
00209 :
00210 GLShaderObject(GL_GEOMETRY_SHADER)
00211 {
00212 #ifdef VERBOSE
00213 printf("loading geometry shader '%s'...\n", filename.c_str());
00214 #endif
00215 TextFile *source= TextFileIO::read(filename, "geometry");
00216 if(filename.empty() == false && source == NULL)
00217 {
00218 printf("GLGeometryShader( ): error reading source '%s'.\n",
00219 filename.c_str());
00220 m_errors= true;
00221 }
00222
00223 pushSource(source);
00224 }
00225 };
00226 #endif
00227
00228 #ifdef GK_OPENGL4
00229
00230 class GLControlShader : public GLShaderObject
00231 {
00232 public:
00233 GLControlShader( )
00234 :
00235 GLShaderObject(GL_TESS_CONTROL_SHADER)
00236 {}
00237
00238 GLControlShader( const TextFile *file )
00239 :
00240 GLShaderObject(GL_TESS_CONTROL_SHADER)
00241 {
00242 pushSource(file);
00243 }
00244
00245 GLControlShader( const std::string& filename )
00246 :
00247 GLShaderObject(GL_TESS_CONTROL_SHADER)
00248 {
00249 #ifdef VERBOSE
00250 printf("loading control shader '%s'...\n", filename.c_str());
00251 #endif
00252 TextFile *source= TextFileIO::read(filename, "control");
00253 if(filename.empty() == false && source == NULL)
00254 {
00255 printf("GLControlShader( ): error reading source '%s'.\n",
00256 filename.c_str());
00257 m_errors= true;
00258 }
00259
00260 pushSource(source);
00261 }
00262 };
00263
00264
00265 class GLEvaluationShader : public GLShaderObject
00266 {
00267 public:
00268 GLEvaluationShader( )
00269 :
00270 GLShaderObject(GL_TESS_EVALUATION_SHADER)
00271 {}
00272
00273 GLEvaluationShader( const TextFile *file )
00274 :
00275 GLShaderObject(GL_TESS_EVALUATION_SHADER)
00276 {
00277 pushSource(file);
00278 }
00279
00280 GLEvaluationShader( const std::string& filename )
00281 :
00282 GLShaderObject(GL_TESS_EVALUATION_SHADER)
00283 {
00284 #ifdef VERBOSE
00285 printf("loading evaluation shader '%s'...\n", filename.c_str());
00286 #endif
00287 TextFile *source= TextFileIO::read(filename, "evaluation");
00288 if(filename.empty() == false && source == NULL)
00289 {
00290 printf("GLEvaluationShader( ): error reading source '%s'.\n",
00291 filename.c_str());
00292 m_errors= true;
00293 }
00294
00295 pushSource(source);
00296 }
00297 };
00298
00299 #endif
00300
00301 }
00302 #endif