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