Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GlslProg.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include <string>
26 #include <sstream>
27 #include <iostream>
28 #include <fstream>
29 #include <exception>
30 #include <map>
31 
32 #include "cinder/gl/gl.h"
33 #include "cinder/Vector.h"
34 #include "cinder/Color.h"
35 #include "cinder/Matrix.h"
36 #include "cinder/DataSource.h"
37 
38 namespace cinder { namespace gl {
39 
40 class GlslProg;
41 typedef std::shared_ptr<GlslProg> GlslProgRef;
42 
44 class GlslProg {
45  public:
46  GlslProg() {}
47  GlslProg( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef(),
48  GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 );
49 
50  GlslProg( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0, GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 );
51 
52  static GlslProgRef create( DataSourceRef vertexShader, DataSourceRef fragmentShader = DataSourceRef(), DataSourceRef geometryShader = DataSourceRef(),
53  GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 )
54  { return std::shared_ptr<GlslProg>( new GlslProg( vertexShader, fragmentShader, geometryShader, geometryInputType, geometryOutputType, geometryOutputVertices ) ); }
55  static GlslProgRef create( const char *vertexShader, const char *fragmentShader = 0, const char *geometryShader = 0,
56  GLint geometryInputType = GL_POINTS, GLint geometryOutputType = GL_TRIANGLES, GLint geometryOutputVertices = 0 )
57  { return std::shared_ptr<GlslProg>( new GlslProg( vertexShader, fragmentShader, geometryShader, geometryInputType, geometryOutputType, geometryOutputVertices ) ); }
58 
59 
60  void bind() const;
61  static void unbind();
62 
63  GLuint getHandle() const { return mObj->mHandle; }
64 
65  void uniform( const std::string &name, int data );
66  void uniform( const std::string &name, const Vec2i &data );
67  void uniform( const std::string &name, const int *data, int count );
68  void uniform( const std::string &name, const Vec2i *data, int count );
69  void uniform( const std::string &name, float data );
70  void uniform( const std::string &name, const Vec2f &data );
71  void uniform( const std::string &name, const Vec3f &data );
72  void uniform( const std::string &name, const Vec4f &data );
73  void uniform( const std::string &name, const Color &data );
74  void uniform( const std::string &name, const ColorA &data );
75  void uniform( const std::string &name, const Matrix22f &data, bool transpose = false );
76  void uniform( const std::string &name, const Matrix33f &data, bool transpose = false );
77  void uniform( const std::string &name, const Matrix44f &data, bool transpose = false );
78  void uniform( const std::string &name, const float *data, int count );
79  void uniform( const std::string &name, const Vec2f *data, int count );
80  void uniform( const std::string &name, const Vec3f *data, int count );
81  void uniform( const std::string &name, const Vec4f *data, int count );
82  void uniform( const std::string &name, const Matrix22f *data, int count, bool transpose = false );
83  void uniform( const std::string &name, const Matrix33f *data, int count, bool transpose = false );
84  void uniform( const std::string &name, const Matrix44f *data, int count, bool transpose = false );
85 
88 
89  std::string getShaderLog( GLuint handle ) const;
90 
91  protected:
92  void loadShader( Buffer shaderSourceBuffer, GLint shaderType );
93  void loadShader( const char *shaderSource, GLint shaderType );
94  void attachShaders();
95  void link();
96 
97  struct Obj {
98  Obj() : mHandle( 0 ) {}
99  ~Obj();
100 
102  std::map<std::string,int> mUniformLocs;
103  };
104 
105  std::shared_ptr<Obj> mObj;
106 
107  public:
109  typedef std::shared_ptr<Obj> GlslProg::*unspecified_bool_type;
111  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &GlslProg::mObj; }
112  void reset() { mObj.reset(); }
114 };
115 
116 class GlslProgCompileExc : public std::exception {
117  public:
118  GlslProgCompileExc( const std::string &log, GLint aShaderType ) throw();
119  virtual const char* what() const throw()
120  {
121  return mMessage;
122  }
123 
124  private:
125  char mMessage[16001];
126  GLint mShaderType;
127 };
128 
129 class GlslNullProgramExc : public std::exception {
130  public:
131  virtual const char* what() const throw()
132  {
133  return "Glsl: Attempt to use null shader";
134  }
135 
136 };
137 
138 } } // namespace cinder::gl
void bind() const
Definition: GlslProg.cpp:112
GLsizei const GLchar ** string
Definition: GLee.h:2427
GLuint getHandle() const
Definition: GlslProg.h:63
~Obj()
Definition: GlslProg.cpp:30
std::map< std::string, int > mUniformLocs
Definition: GlslProg.h:102
void loadShader(Buffer shaderSourceBuffer, GLint shaderType)
Definition: GlslProg.cpp:82
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:1011
Obj()
Definition: GlslProg.h:98
Definition: GlslProg.h:116
static void unbind()
Definition: GlslProg.cpp:117
Represents an OpenGL GLSL program. Implicitly shared object.
Definition: GlslProg.h:44
virtual const char * what() const
Definition: GlslProg.h:131
#define GL_TRIANGLES
Definition: gldx.h:112
std::string getShaderLog(GLuint handle) const
Definition: GlslProg.cpp:122
unsigned int GLuint
Definition: gldx.h:55
static GlslProgRef create(const char *vertexShader, const char *fragmentShader=0, const char *geometryShader=0, GLint geometryInputType=GL_POINTS, GLint geometryOutputType=GL_TRIANGLES, GLint geometryOutputVertices=0)
Definition: GlslProg.h:55
Definition: Buffer.h:31
GLint getUniformLocation(const std::string &name)
Definition: GlslProg.cpp:260
Definition: GlslProg.h:129
static GlslProgRef create(DataSourceRef vertexShader, DataSourceRef fragmentShader=DataSourceRef(), DataSourceRef geometryShader=DataSourceRef(), GLint geometryInputType=GL_POINTS, GLint geometryOutputType=GL_TRIANGLES, GLint geometryOutputVertices=0)
Definition: GlslProg.h:52
GLuint GLuint GLsizei count
Definition: GLee.h:963
GlslProg()
Definition: GlslProg.h:46
#define GL_POINTS
Definition: gldx.h:108
GLsizei GLboolean transpose
Definition: GLee.h:2535
void link()
Definition: GlslProg.cpp:107
void reset()
Emulates shared_ptr-like behavior.
Definition: GlslProg.h:112
GLuint mHandle
Definition: GlslProg.h:101
GLuint const GLchar * name
Definition: GLee.h:2259
Definition: Matrix22.h:37
int GLint
Definition: gldx.h:51
std::shared_ptr< Obj > GlslProg::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: GlslProg.h:110
Definition: GlslProg.h:97
virtual const char * what() const
Definition: GlslProg.h:119
void uniform(const std::string &name, int data)
Definition: GlslProg.cpp:140
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
std::shared_ptr< GlslProg > GlslProgRef
Definition: GlslProg.h:40
GlslProgCompileExc(const std::string &log, GLint aShaderType)
Definition: GlslProg.cpp:279
Definition: Matrix33.h:37
GLint getAttribLocation(const std::string &name)
Definition: GlslProg.cpp:272
std::shared_ptr< Obj > mObj
Definition: GlslProg.h:105