Assignments Assignments

GLSLProgram.h
Go to the documentation of this file.
1//Code from : OpenGL 4 Shading Language Cookbook Second Edition, by David Wolff
2// [MK] Modified to only support simple vertex/fragment shaders
3#ifndef GLSLPROGRAM_H
4#define GLSLPROGRAM_H
5
6#include <GL/glew.h>
7#include <string>
8#include <stdexcept>
9#include <Util/exceptions.h>
10
11#pragma warning( disable : 4290 )
12
13 namespace GLSLShaderInfo
14{
15 class shader_file_extension
16 {
17 public:
18 std::string ext;
19 GLenum type;
20 };
21
22 const shader_file_extension extensions[] =
23 {
24 { ".vs" , GL_VERTEX_SHADER },
25 { ".vert" , GL_VERTEX_SHADER },
26 { ".fs" , GL_FRAGMENT_SHADER },
27 { ".frag" , GL_FRAGMENT_SHADER },
28 };
29}
30
32 class GLSLProgram
33{
34 template< unsigned int Dim > static void glUniformiv ( GLint location , GLsizei count , const GLint* value );
35
36 template< unsigned int Dim > static void glUniformfv ( GLint location , GLsizei count , const GLfloat* value );
37
38 template< unsigned int Dim > static void glUniformMatrixfv( GLint location , GLsizei count , GLboolean transpose , const GLfloat* value );
39
40 // Make these private in order to make the object non-copyable
41 GLSLProgram( const GLSLProgram & other ) { }
42
43 GLSLProgram & operator=( const GLSLProgram &other ){ return *this; }
44
45 GLuint _handle;
46
47 bool _linked;
48
49 std::string _vertexSource;
50
51 std::string _fragmentSource;
52 public:
53 static bool FileExists( const std::string & fileName );
54
55 static std::string GetExtension( const std::string & fileName );
56
57 static GLenum GetShaderType( const std::string &fileName );
58
59 static std::string GetShaderTypeString( GLenum type );
60
61 GLSLProgram( const std::string &vs_filename , const std::string &fs_filename );
62
63 ~GLSLProgram( void );
64
66 void compileShader( const std::string & source , GLenum type );
67
69 void link( void );
70
72 void validate( void );
73
75 void use( void );
76
78 GLuint handle( void ) const { return _handle; }
79
81 bool linked( void ) const { return _linked; }
82
84 void setUniform( const std::string &name , int val , bool showWarning=true );
85
87 void setUniform( const std::string &name , float val , bool showWarning=true );
88
90 void setUniform( const std::string &name , double val , bool showWarning=true );
91
93 template< unsigned int > void setUniform( const std::string &name , const int* v , bool showWarning=true );
94
96 template< unsigned int > void setUniform( const std::string &name , const float* v , bool showWarning=true );
97
99 template< unsigned int > void setUniform( const std::string &name , const double* v , bool showWarning=true );
100
102 template< unsigned int > void setUniformMatrix( const std::string &name , const float* m , bool showWarning=true );
103
105 template< unsigned int > void setUniformMatrix( const std::string &name , const double* m , bool showWarning=true );
106
108 void init( void );
109};
110
111 template< unsigned int Dim >
112 void GLSLProgram::setUniform( const std::string &name , const int* v , bool showWarning )
113{
114 GLint loc = glGetUniformLocation( _handle , name.c_str() );
115 if( loc>=0 ) glUniformiv< Dim >( loc , 1 , v );
116 else if( showWarning ) WARN( "Non-existant uniform: %s" , name.c_str() );
117}
118
119 template< unsigned int Dim >
120 void GLSLProgram::setUniform( const std::string &name , const float* v , bool showWarning )
121{
122 GLint loc = glGetUniformLocation( _handle , name.c_str() );
123 if( loc>=0 ) glUniformfv< Dim >( loc , 1 , v );
124 else if( showWarning ) WARN( "Non-existant uniform: %s" , name.c_str() );
125}
126
127 template< unsigned int Dim >
128 void GLSLProgram::setUniform( const std::string &name , const double* v , bool showWarning )
129{
130 float _v[Dim];
131 for( int i=0 ; i<Dim ; i++ ) _v[i] = (float)v[i];
132 setUniform< Dim >( name , _v , showWarning );
133}
134
135 template< unsigned int Dim >
136 void GLSLProgram::setUniformMatrix( const std::string &name , const float* m , bool showWarning )
137{
138 GLint loc = glGetUniformLocation( _handle , name.c_str() );
139 if( loc>=0 ) glUniformMatrixfv< Dim >( loc , 1 , GL_FALSE , m );
140 else if( showWarning ) WARN( "Non-existant uniform: %s" , name.c_str() );
141};
142
143 template< unsigned int Dim >
144 void GLSLProgram::setUniformMatrix( const std::string &name , const double* m , bool showWarning )
145{
146 float _m[Dim*Dim];
147 for( int i=0 ; i<Dim*Dim ; i++ ) _m[i] = (float)m[i];
148 setUniformMatrix< Dim >( name , _m , showWarning );
149}
150#endif // GLSLPROGRAM_H
GLSLProgram
Definition GLSLProgram.h:33
GLSLProgram::init
void init(void)
Definition GLSLProgram.cpp:250
GLSLProgram::linked
bool linked(void) const
Definition GLSLProgram.h:81
GLSLProgram::GLSLProgram
GLSLProgram(const GLSLProgram &other)
Definition GLSLProgram.h:41
GLSLProgram::_handle
GLuint _handle
Definition GLSLProgram.h:45
GLSLProgram::setUniform
void setUniform(const std::string &name, int val, bool showWarning=true)
Definition GLSLProgram.cpp:192
GLSLProgram::_linked
bool _linked
Definition GLSLProgram.h:47
GLSLProgram::handle
GLuint handle(void) const
Definition GLSLProgram.h:78
GLSLProgram::glUniformiv
static void glUniformiv(GLint location, GLsizei count, const GLint *value)
GLSLProgram::FileExists
static bool FileExists(const std::string &fileName)
Definition GLSLProgram.cpp:241
GLSLProgram::use
void use(void)
Definition GLSLProgram.cpp:186
GLSLProgram::GetShaderType
static GLenum GetShaderType(const std::string &fileName)
Definition GLSLProgram.cpp:100
GLSLProgram::validate
void validate(void)
Definition GLSLProgram.cpp:213
GLSLProgram::~GLSLProgram
~GLSLProgram(void)
Definition GLSLProgram.cpp:79
GLSLProgram::_fragmentSource
std::string _fragmentSource
Definition GLSLProgram.h:51
GLSLProgram::link
void link(void)
Definition GLSLProgram.cpp:156
GLSLProgram::glUniformfv
static void glUniformfv(GLint location, GLsizei count, const GLfloat *value)
GLSLProgram::_vertexSource
std::string _vertexSource
Definition GLSLProgram.h:49
GLSLProgram::GetExtension
static std::string GetExtension(const std::string &fileName)
Definition GLSLProgram.cpp:111
GLSLProgram::operator=
GLSLProgram & operator=(const GLSLProgram &other)
Definition GLSLProgram.h:43
GLSLProgram::GetShaderTypeString
static std::string GetShaderTypeString(GLenum type)
Definition GLSLProgram.cpp:266
GLSLProgram::compileShader
void compileShader(const std::string &source, GLenum type)
Definition GLSLProgram.cpp:118
GLSLProgram::glUniformMatrixfv
static void glUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
GLSLProgram::setUniformMatrix
void setUniformMatrix(const std::string &name, const float *m, bool showWarning=true)
Definition GLSLProgram.h:136
GLSLShaderInfo::shader_file_extension
Definition GLSLProgram.h:16
GLSLShaderInfo::shader_file_extension::ext
std::string ext
Definition GLSLProgram.h:18
GLSLShaderInfo::shader_file_extension::type
GLenum type
Definition GLSLProgram.h:19
exceptions.h
WARN
#define WARN(...)
Definition exceptions.h:145
glew.h
glGetUniformLocation
#define glGetUniformLocation
Definition glew.h:1931
GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER
Definition glew.h:1768
GLfloat
float GLfloat
Definition glew.h:289
GLint
int GLint
Definition glew.h:281
GLenum
unsigned int GLenum
Definition glew.h:278
GLsizei
int GLsizei
Definition glew.h:282
GLboolean
unsigned char GLboolean
Definition glew.h:283
GLuint
unsigned int GLuint
Definition glew.h:280
GL_FALSE
#define GL_FALSE
Definition glew.h:314
GL_VERTEX_SHADER
#define GL_VERTEX_SHADER
Definition glew.h:1769
val
int val
Definition jpeglib.h:956
GLSLShaderInfo
Definition GLSLProgram.h:14
GLSLShaderInfo::extensions
const shader_file_extension extensions[]
Definition GLSLProgram.h:22