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