00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 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, const Vec2i &data ); 00052 void uniform( const std::string &name, const int *data, int count ); 00053 void uniform( const std::string &name, const Vec2i *data, int count ); 00054 void uniform( const std::string &name, float data ); 00055 void uniform( const std::string &name, const Vec2f &data ); 00056 void uniform( const std::string &name, const Vec3f &data ); 00057 void uniform( const std::string &name, const Vec4f &data ); 00058 void uniform( const std::string &name, const Matrix44f &data, bool transpose = false ); 00059 void uniform( const std::string &name, const float *data, int count ); 00060 void uniform( const std::string &name, const Vec2f *data, int count ); 00061 void uniform( const std::string &name, const Vec3f *data, int count ); 00062 void uniform( const std::string &name, const Vec4f *data, int count ); 00063 00064 GLint getUniformLocation( const std::string &name ); 00065 GLint getAttribLocation( const std::string &name ); 00066 00067 std::string getShaderLog( GLuint handle ) const; 00068 00069 protected: 00070 void loadShader( Buffer shaderSourceBuffer, GLint shaderType ); 00071 void loadShader( const char *shaderSource, GLint shaderType ); 00072 void attachShaders(); 00073 void link(); 00074 00075 struct Obj { 00076 Obj() : mHandle( 0 ) {} 00077 ~Obj(); 00078 00079 GLuint mHandle; 00080 std::map<std::string,int> mUniformLocs; 00081 }; 00082 00083 shared_ptr<Obj> mObj; 00084 00085 public: 00087 00088 typedef shared_ptr<Obj> GlslProg::*unspecified_bool_type; 00089 operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &GlslProg::mObj; } 00090 void reset() { mObj.reset(); } 00092 }; 00093 00094 class GlslProgCompileExc : public std::exception { 00095 public: 00096 GlslProgCompileExc( const std::string &log, GLint aShaderType ) throw(); 00097 virtual const char* what() const throw() 00098 { 00099 return mMessage; 00100 } 00101 00102 private: 00103 char mMessage[16001]; 00104 GLint mShaderType; 00105 }; 00106 00107 class GlslNullProgramExc : public std::exception { 00108 public: 00109 virtual const char* what() const throw() 00110 { 00111 return "Glsl: Attempt to use null shader"; 00112 } 00113 00114 }; 00115 00116 } } // namespace cinder::gl