00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #pragma once
00024
00025 #include <string>
00026 #include <sstream>
00027 #include <iostream>
00028 #include <fstream>
00029 #include <exception>
00030 #include <map>
00031
00032 #include "cinder/gl/gl.h"
00033 #include "cinder/Vector.h"
00034 #include "cinder/Color.h"
00035 #include "cinder/Matrix.h"
00036 #include "cinder/DataSource.h"
00037
00038 namespace cinder { namespace gl {
00039
00040 class GlslProg;
00041 typedef std::shared_ptr<GlslProg> GlslProgRef;
00042
00044 class GlslProg {
00045 public:
00046 GlslProg() {}
00047 GlslProg( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef(),
00048 GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 );
00049
00050 GlslProg( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0, GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 );
00051
00052 static GlslProgRef create( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef(),
00053 GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 )
00054 { return std::shared_ptr<GlslProg>( new GlslProg( vertexShader, fragmentShader, geometryShader, geometryInputType, geometryOutputType, geometryOutputVertices ) ); }
00055 static GlslProgRef create( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0,
00056 GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 )
00057 { return std::shared_ptr<GlslProg>( new GlslProg( vertexShader, fragmentShader, geometryShader, geometryInputType, geometryOutputType, geometryOutputVertices ) ); }
00058
00059
00060 void bind() const;
00061 static void unbind();
00062
00063 GLuint getHandle() const { return mObj->mHandle; }
00064
00065 void uniform( const std::string &name, int data );
00066 void uniform( const std::string &name, const Vec2i &data );
00067 void uniform( const std::string &name, const int *data, int count );
00068 void uniform( const std::string &name, const Vec2i *data, int count );
00069 void uniform( const std::string &name, float data );
00070 void uniform( const std::string &name, const Vec2f &data );
00071 void uniform( const std::string &name, const Vec3f &data );
00072 void uniform( const std::string &name, const Vec4f &data );
00073 void uniform( const std::string &name, const Color &data );
00074 void uniform( const std::string &name, const ColorA &data );
00075 void uniform( const std::string &name, const Matrix22f &data, bool transpose = false );
00076 void uniform( const std::string &name, const Matrix33f &data, bool transpose = false );
00077 void uniform( const std::string &name, const Matrix44f &data, bool transpose = false );
00078 void uniform( const std::string &name, const float *data, int count );
00079 void uniform( const std::string &name, const Vec2f *data, int count );
00080 void uniform( const std::string &name, const Vec3f *data, int count );
00081 void uniform( const std::string &name, const Vec4f *data, int count );
00082 void uniform( const std::string &name, const Matrix22f *data, int count, bool transpose = false );
00083 void uniform( const std::string &name, const Matrix33f *data, int count, bool transpose = false );
00084 void uniform( const std::string &name, const Matrix44f *data, int count, bool transpose = false );
00085
00086 GLint getUniformLocation( const std::string &name );
00087 GLint getAttribLocation( const std::string &name );
00088
00089 std::string getShaderLog( GLuint handle ) const;
00090
00091 protected:
00092 void loadShader( Buffer shaderSourceBuffer, GLint shaderType );
00093 void loadShader( const char *shaderSource, GLint shaderType );
00094 void attachShaders();
00095 void link();
00096
00097 struct Obj {
00098 Obj() : mHandle( 0 ) {}
00099 ~Obj();
00100
00101 GLuint mHandle;
00102 std::map<std::string,int> mUniformLocs;
00103 };
00104
00105 std::shared_ptr<Obj> mObj;
00106
00107 public:
00109
00110 typedef std::shared_ptr<Obj> GlslProg::*unspecified_bool_type;
00111 operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &GlslProg::mObj; }
00112 void reset() { mObj.reset(); }
00114 };
00115
00116 class GlslProgCompileExc : public std::exception {
00117 public:
00118 GlslProgCompileExc( const std::string &log, GLint aShaderType ) throw();
00119 virtual const char* what() const throw()
00120 {
00121 return mMessage;
00122 }
00123
00124 private:
00125 char mMessage[16001];
00126 GLint mShaderType;
00127 };
00128
00129 class GlslNullProgramExc : public std::exception {
00130 public:
00131 virtual const char* what() const throw()
00132 {
00133 return "Glsl: Attempt to use null shader";
00134 }
00135
00136 };
00137
00138 } }