SimpleGL  1.1.0
A framework for platform independent rendering
shader_objects.cpp
Go to the documentation of this file.
1 
9 
10 #include <vector>
11 
12 #include <glm/gtc/type_ptr.hpp>
13 
15 
16 using namespace std;
17 using namespace glm;
18 
19 namespace sgl {
20 
21  Shader::Shader(ShaderType type) {
22  handle = glCreateShader(to_GLenum(type));
23  }
24 
25  Shader::~Shader(void) {
26  glDeleteShader(handle);
27  }
28 
29  GLuint Shader::getHandle() {
30  return handle;
31  }
32 
33  void Shader::source(std::string source) {
34  const char* c_str = source.c_str();
35  glShaderSource(handle, 1, &c_str, nullptr);
36  }
37 
38  void Shader::compile() {
39  glCompileShader(handle);
40  }
41 
42  bool Shader::getCompileStatus() {
43  GLint status;
44  glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
45  return status == GL_TRUE;
46  }
47 
48  std::string Shader::getInfoLog() {
49  GLint logSize;
50  glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logSize);
51  vector<char> message(logSize);
52  glGetShaderInfoLog(handle, logSize, NULL, message.data());
53  return string(message.data());
54  }
55 
56  ShaderProgram* ShaderProgram::current = nullptr;
57 
58  ShaderProgram::ShaderProgram(void) {
59  handle = glCreateProgram();
60  }
61 
62  ShaderProgram::~ShaderProgram(void) {
63  glDeleteProgram(handle);
64 
65  /* Set current to nullptr if this was the used shader program */
66  if (ShaderProgram::current == this) {
67  ShaderProgram::current = nullptr;
68  }
69  }
70 
71  GLuint ShaderProgram::getHandle() {
72  return handle;
73  }
74 
75  void ShaderProgram::attach(Shader* shader) {
76  glAttachShader(handle, shader->getHandle());
77  }
78 
79  void ShaderProgram::link() {
80  glLinkProgram(handle);
81  }
82 
83  void ShaderProgram::use() {
84  /* If already used return immediately */
85  if (ShaderProgram::current == this) {
86  return;
87  }
88 
89  /* Use shader program */
90  glUseProgram(handle);
91  ShaderProgram::current = this;
92  }
93 
94  bool ShaderProgram::getLinkStatus() {
95  GLint status;
96  glGetProgramiv(handle, GL_LINK_STATUS, &status);
97  return status == GL_TRUE;
98  }
99 
100  std::string ShaderProgram::getInfoLog() {
101  GLint logSize;
102  glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &logSize);
103  vector<char> message(logSize);
104  glGetProgramInfoLog(handle, logSize, NULL, message.data());
105  return string(message.data());
106  }
107 
108  void ShaderProgram::bindFragmentDataLocation(unsigned int colorNumber, std::string name) {
109  glBindFragDataLocation(handle, colorNumber, name.c_str());
110  }
111 
112  int ShaderProgram::getAttributeLocation(std::string name) {
113  return glGetAttribLocation(handle, name.c_str());
114  }
115 
116  int ShaderProgram::getUniformLocation(std::string name) {
117  return glGetUniformLocation(handle, name.c_str());
118  }
119 
120  void ShaderProgram::setUniform(int location, int value) {
121  glUniform1i(location, value);
122  }
123 
124  void ShaderProgram::setUniform(int location, float value) {
125  glUniform1f(location, value);
126  }
127 
128  void ShaderProgram::setUniform(int location, glm::vec2 value) {
129  glUniform2fv(location, 1, value_ptr(value));
130  }
131 
132  void ShaderProgram::setUniform(int location, glm::vec3 value) {
133  glUniform3fv(location, 1, value_ptr(value));
134  }
135 
136  void ShaderProgram::setUniform(int location, glm::vec4 value) {
137  glUniform4fv(location, 1, value_ptr(value));
138  }
139 
140  void ShaderProgram::setUniform(int location, glm::mat2 value) {
141  glUniformMatrix2fv(location, 1, GL_FALSE, value_ptr(value));
142  }
143 
144  void ShaderProgram::setUniform(int location, glm::mat3 value) {
145  glUniformMatrix3fv(location, 1, GL_FALSE, value_ptr(value));
146  }
147 
148  void ShaderProgram::setUniform(int location, glm::mat4 value) {
149  glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(value));
150  }
151 
152  void ShaderProgram::setUniform(std::string name, int value) {
153  int location = getUniformLocation(name);
154  setUniform(location, value);
155  }
156 
157  void ShaderProgram::setUniform(std::string name, float value) {
158  int location = getUniformLocation(name);
159  setUniform(location, value);
160  }
161 
162  void ShaderProgram::setUniform(std::string name, glm::vec2 value) {
163  int location = getUniformLocation(name);
164  setUniform(location, value);
165  }
166 
167  void ShaderProgram::setUniform(std::string name, glm::vec3 value) {
168  int location = getUniformLocation(name);
169  setUniform(location, value);
170  }
171 
172  void ShaderProgram::setUniform(std::string name, glm::vec4 value) {
173  int location = getUniformLocation(name);
174  setUniform(location, value);
175  }
176 
177  void ShaderProgram::setUniform(std::string name, glm::mat2 value) {
178  int location = getUniformLocation(name);
179  setUniform(location, value);
180  }
181 
182  void ShaderProgram::setUniform(std::string name, glm::mat3 value) {
183  int location = getUniformLocation(name);
184  setUniform(location, value);
185  }
186 
187  void ShaderProgram::setUniform(std::string name, glm::mat4 value) {
188  int location = getUniformLocation(name);
189  setUniform(location, value);
190  }
191 }
This class wraps an OpenGL shader object.
ShaderType
This enum wraps the shader types.
GLenum to_GLenum(E e)
Gets the GLenum value from an enum class.
Definition: backend_gl.tcc:22
This class wraps an OpenGL shader program.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
GLuint getHandle()
Returns the handle of the shader object.