Go to the documentation of this file.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
00041 class GlslProg {
00042 public:
00043 GlslProg() {}
00044 GlslProg( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef(),
00045 GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0);
00046
00047 GlslProg( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0, GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0);
00048
00049 void bind() const;
00050 static void unbind();
00051
00052 GLuint getHandle() const { return mObj->mHandle; }
00053
00054 void uniform( const std::string &name, int data );
00055 void uniform( const std::string &name, const Vec2i &data );
00056 void uniform( const std::string &name, const int *data, int count );
00057 void uniform( const std::string &name, const Vec2i *data, int count );
00058 void uniform( const std::string &name, float data );
00059 void uniform( const std::string &name, const Vec2f &data );
00060 void uniform( const std::string &name, const Vec3f &data );
00061 void uniform( const std::string &name, const Vec4f &data );
00062 void uniform( const std::string &name, const Color &data );
00063 void uniform( const std::string &name, const ColorA &data );
00064 void uniform( const std::string &name, const Matrix33f &data, bool transpose = false );
00065 void uniform( const std::string &name, const Matrix44f &data, bool transpose = false );
00066 void uniform( const std::string &name, const float *data, int count );
00067 void uniform( const std::string &name, const Vec2f *data, int count );
00068 void uniform( const std::string &name, const Vec3f *data, int count );
00069 void uniform( const std::string &name, const Vec4f *data, int count );
00070
00071 GLint getUniformLocation( const std::string &name );
00072 GLint getAttribLocation( const std::string &name );
00073
00074 std::string getShaderLog( GLuint handle ) const;
00075
00076 protected:
00077 void loadShader( Buffer shaderSourceBuffer, GLint shaderType );
00078 void loadShader( const char *shaderSource, GLint shaderType );
00079 void attachShaders();
00080 void link();
00081
00082 struct Obj {
00083 Obj() : mHandle( 0 ) {}
00084 ~Obj();
00085
00086 GLuint mHandle;
00087 std::map<std::string,int> mUniformLocs;
00088 };
00089
00090 std::shared_ptr<Obj> mObj;
00091
00092 public:
00094
00095 typedef std::shared_ptr<Obj> GlslProg::*unspecified_bool_type;
00096 operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &GlslProg::mObj; }
00097 void reset() { mObj.reset(); }
00099 };
00100
00101 class GlslProgCompileExc : public std::exception {
00102 public:
00103 GlslProgCompileExc( const std::string &log, GLint aShaderType ) throw();
00104 virtual const char* what() const throw()
00105 {
00106 return mMessage;
00107 }
00108
00109 private:
00110 char mMessage[16001];
00111 GLint mShaderType;
00112 };
00113
00114 class GlslNullProgramExc : public std::exception {
00115 public:
00116 virtual const char* what() const throw()
00117 {
00118 return "Glsl: Attempt to use null shader";
00119 }
00120
00121 };
00122
00123 } }