/
shader_program.h
154 lines (128 loc) · 3.53 KB
/
shader_program.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* shader_program.h
*
* @author Zsolt Horváth
* @date 2. 4. 2011
*
*/
#ifndef _SHADER_PROGRAM_H_
#define _SHADER_PROGRAM_H_
#include <string>
#include <fstream>
#include <iterator>
#include <exception>
#include <stdexcept>
#include <iostream>
#include <SDL/SDL.h>
#include <GL/glew.h>
using namespace std;
/**
* Shader exceptions
*/
struct ShaderException : public runtime_error
{
ShaderException(const GLenum error = glGetError()) throw()
: std::runtime_error(std::string("OpenGL : ") + (const char*) gluErrorString(error)) {}
ShaderException(const char * text, const GLenum error = glGetError()) throw()
: std::runtime_error(std::string("OpenGL : ") + text + " - " + (const char*) gluErrorString(error)) {}
};
/**
* OpenGL shader program
*/
class ShaderProgram {
public:
/**
* Construct
*
* @param string vertexSource - path to vertex shader
* @param string fragmentSource - path to fragment shader
*/
ShaderProgram (string vertexSource, string fragmentSource);
/**
* Desctuctor
*/
~ShaderProgram ();
/**
* Use shader program
*/
void use();
/**
* Add shader attribute
*
* @see glVertexAttribPointer
*/
void addAttribute (string attribute, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * pointer);
/**
* Get attribute location
*
* @param string attribute - attribute name
* @return pointer to attribute
*/
GLuint getAttributeLocation (string attribute);
/**
* Get uniform location
*
* @param string uniform - uniform name
* @return pointer to uniform
*/
GLuint getUniformLocation (string uniform);
GLuint getReference();
protected:
/**
* Add shader
*
* @param const GLenum type - type of shader
* @param const char *soruce - path to shader
*/
void add (const GLenum type, const char *source);
/**
* Load shader form file
*
* @param const char *filename - path to shader
* @return content of loaded file
*/
string load (const char *filename);
/**
* Get info log of shader
*
* @param const GLuint shader - shader pointer
* @return info string
*/
string getShaderInfoLog (const GLuint shader);
/**
* Get info log of program
*
* @param const GLuint program - program pointer
* @return info string
*/
string getProgramInfoLog (const GLuint program);
/**
* Compile shader
*
* @param const GLenum type - type of shader
* @param const char * source - shader source code
* @return pointer to shader
*/
GLuint compile (const GLenum type, const char * source);
/**
* Link shader into program
*
* @param size_t count - count of shaders to compile
* @param ... - shader pointers
* @return pointer to program
*/
GLuint link (size_t count, ...);
/**
* Vertex shader pointer
*/
GLuint vertexShader;
/**
* Fragment shader pointer
*/
GLuint fragmentShader;
/**
* Program pointer
*/
GLuint program;
};
#endif /* _SHADER_PROGRAM_H_ */