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
14{
16 {
17 public:
18 std::string ext;
19 GLenum type;
20 };
21
23 {
24 { ".vs" , GL_VERTEX_SHADER },
25 { ".vert" , GL_VERTEX_SHADER },
26 { ".fs" , GL_FRAGMENT_SHADER },
27 { ".frag" , GL_FRAGMENT_SHADER },
28 };
29}
30
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;
52public:
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
111template< unsigned int Dim >
112void 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
119template< unsigned int Dim >
120void 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
127template< unsigned int Dim >
128void 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
135template< unsigned int Dim >
136void 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
143template< unsigned int Dim >
144void 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
Definition: GLSLProgram.h:33
void init(void)
Definition: GLSLProgram.cpp:250
bool linked(void) const
Definition: GLSLProgram.h:81
GLSLProgram(const GLSLProgram &other)
Definition: GLSLProgram.h:41
GLuint _handle
Definition: GLSLProgram.h:45
void setUniform(const std::string &name, int val, bool showWarning=true)
Definition: GLSLProgram.cpp:192
bool _linked
Definition: GLSLProgram.h:47
GLuint handle(void) const
Definition: GLSLProgram.h:78
static void glUniformiv(GLint location, GLsizei count, const GLint *value)
static bool FileExists(const std::string &fileName)
Definition: GLSLProgram.cpp:241
void use(void)
Definition: GLSLProgram.cpp:186
static GLenum GetShaderType(const std::string &fileName)
Definition: GLSLProgram.cpp:100
void validate(void)
Definition: GLSLProgram.cpp:213
~GLSLProgram(void)
Definition: GLSLProgram.cpp:79
std::string _fragmentSource
Definition: GLSLProgram.h:51
void link(void)
Definition: GLSLProgram.cpp:156
static void glUniformfv(GLint location, GLsizei count, const GLfloat *value)
std::string _vertexSource
Definition: GLSLProgram.h:49
static std::string GetExtension(const std::string &fileName)
Definition: GLSLProgram.cpp:111
GLSLProgram & operator=(const GLSLProgram &other)
Definition: GLSLProgram.h:43
static std::string GetShaderTypeString(GLenum type)
Definition: GLSLProgram.cpp:266
void compileShader(const std::string &source, GLenum type)
Definition: GLSLProgram.cpp:118
static void glUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
void setUniformMatrix(const std::string &name, const float *m, bool showWarning=true)
Definition: GLSLProgram.h:136
Definition: GLSLProgram.h:16
std::string ext
Definition: GLSLProgram.h:18
GLenum type
Definition: GLSLProgram.h:19
#define WARN(...)
Definition: exceptions.h:145
Definition: GLSLProgram.h:14
const shader_file_extension extensions[]
Definition: GLSLProgram.h:22