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/Matrix.h"
00035 #include "cinder/DataSource.h"
00036
00037 namespace cinder { namespace gl {
00038
00039 class GlslProg {
00040 public:
00041 GlslProg() {}
00042 GlslProg( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef() );
00043 GlslProg( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0 );
00044
00045 void bind() const;
00046 static void unbind();
00047
00048 GLuint getHandle() const { return mObj->mHandle; }
00049
00050 void uniform( const std::string &name, int data );
00051 void uniform( const std::string &name, float data );
00052 void uniform( const std::string &name, const Vec2f &data );
00053 void uniform( const std::string &name, const Vec3f &data );
00054 void uniform( const std::string &name, const Matrix44f &data, bool transpose = false );
00055
00056 GLint getUniformLocation( const std::string &name );
00057 GLint getAttribLocation( const std::string &name );
00058
00059 std::string getShaderLog( GLuint handle ) const;
00060
00061 protected:
00062 void loadShader( Buffer shaderSourceBuffer, GLint shaderType );
00063 void loadShader( const char *shaderSource, GLint shaderType );
00064 void attachShaders();
00065 void link();
00066
00067 struct Obj {
00068 Obj() : mHandle( 0 ) {}
00069 ~Obj();
00070
00071 GLuint mHandle;
00072 std::map<std::string,int> mUniformLocs;
00073 };
00074
00075 shared_ptr<Obj> mObj;
00076
00077 public:
00079
00080 typedef shared_ptr<Obj> GlslProg::*unspecified_bool_type;
00081 operator unspecified_bool_type() { return ( mObj.get() == 0 ) ? 0 : &GlslProg::mObj; }
00082 void reset() { mObj.reset(); }
00084 };
00085
00086 class GlslProgCompileExc : public std::exception {
00087 public:
00088 GlslProgCompileExc( const std::string &log, GLint aShaderType ) throw();
00089 virtual const char* what() const throw()
00090 {
00091 return mMessage;
00092 }
00093
00094 private:
00095 char mMessage[16001];
00096 GLint mShaderType;
00097 };
00098
00099 class GlslNullProgramExc : public std::exception {
00100 public:
00101 virtual const char* what() const throw()
00102 {
00103 return "Glsl: Attempt to use null shader";
00104 }
00105
00106 };
00107
00108 } }