MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
shader_program.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef OGL_SHADER_PROGRAM_HEADER
11#define OGL_SHADER_PROGRAM_HEADER
12
13#include <string>
14#include <memory>
15
16#include "math/vector.h"
17#include "math/matrix.h"
18#include "ogl/defines.h"
19#include "ogl/opengl.h"
20#include "ogl/check_gl_error.h"
21
22#define OGL_ATTRIB_POSITION "pos"
23#define OGL_ATTRIB_NORMAL "normal"
24#define OGL_ATTRIB_COLOR "color"
25#define OGL_ATTRIB_TEXCOORD "texuv"
26
28
33{
34public:
35 typedef std::shared_ptr<ShaderProgram> Ptr;
36 typedef std::shared_ptr<ShaderProgram const> ConstPtr;
37
38public:
39 ~ShaderProgram (void);
40 static Ptr create (void);
41
46 bool try_load_all (std::string const& basename);
47
49 void load_vert_file (std::string const& filename);
51 void load_geom_file (std::string const& filename);
53 void load_frag_file (std::string const& filename);
54
56 void load_vert_code (std::string const& code);
58 void load_geom_code (std::string const& code);
60 void load_frag_code (std::string const& code);
61
63 void unload_vert (void);
65 void unload_geom (void);
67 void unload_frag (void);
68
74 GLint get_attrib_location (char const* name);
75
81 GLint get_uniform_location (char const* name);
82
84 void send_uniform (char const* name, math::Vec3f const& v);
86 void send_uniform (char const* name, math::Vec4f const& v);
88 void send_uniform (char const* name, math::Matrix4f const& m);
90 void send_uniform (char const* name, GLint val);
92 void send_uniform (char const* name, GLfloat val);
93
95 void bind (void);
96
98 void unbind (void) const;
99
100private:
101 ShaderProgram (void);
102
103 void load_shader_file (GLuint& shader_id, GLuint shader_type,
104 std::string const& filename);
105 void load_shader_code (GLuint& shader_id, GLuint shader_type,
106 std::string const& code);
107 void unload_shader (GLuint& shader_id);
108 void compile_shader (GLuint shader_id, std::string const& code);
109
110 GLint get_program_property (int pname);
111 GLint get_shader_property (GLuint shader_id, int pname);
112
113 void ensure_linked (void);
114
115private:
116 GLuint prog_id;
117 GLuint vert_id;
118 GLuint geom_id;
119 GLuint frag_id;
120
121 bool need_to_link;
122};
123
124/* ---------------------------------------------------------------- */
125
126inline
127ShaderProgram::ShaderProgram (void)
128{
129 this->vert_id = 0;
130 this->geom_id = 0;
131 this->frag_id = 0;
132 this->prog_id = glCreateProgram();
134 this->need_to_link = false;
135}
136
137inline
138ShaderProgram::~ShaderProgram (void)
139{
140 glDeleteProgram(this->prog_id);
142 glDeleteShader(this->vert_id);
144 glDeleteShader(this->geom_id);
146 glDeleteShader(this->frag_id);
148}
149
151ShaderProgram::create (void)
152{
153 return Ptr(new ShaderProgram);
154}
155
156inline void
157ShaderProgram::load_vert_file (std::string const& filename)
158{
159 this->load_shader_file(this->vert_id, GL_VERTEX_SHADER, filename);
160}
161
162inline void
163ShaderProgram::load_geom_file (std::string const& filename)
164{
165 this->load_shader_file(this->geom_id, GL_GEOMETRY_SHADER, filename);
166}
167
168inline void
169ShaderProgram::load_frag_file (std::string const& filename)
170{
171 this->load_shader_file(this->frag_id, GL_FRAGMENT_SHADER, filename);
172}
173
174inline void
175ShaderProgram::load_vert_code (std::string const& code)
176{
177 this->load_shader_code(this->vert_id, GL_VERTEX_SHADER, code);
178}
179
180inline void
181ShaderProgram::load_geom_code (std::string const& code)
182{
183 this->load_shader_code(this->geom_id, GL_GEOMETRY_SHADER, code);
184}
185
186inline void
187ShaderProgram::load_frag_code (std::string const& code)
188{
189 this->load_shader_code(this->frag_id, GL_FRAGMENT_SHADER, code);
190}
191
192inline void
193ShaderProgram::unload_vert (void)
194{
195 glDetachShader(this->prog_id, this->vert_id);
197
198 glDeleteShader(this->vert_id);
200
201 this->vert_id = 0;
202}
203
204inline void
205ShaderProgram::unload_geom (void)
206{
207 glDetachShader(this->prog_id, this->geom_id);
209
210 glDeleteShader(this->geom_id);
212
213 this->geom_id = 0;
214}
215
216inline void
217ShaderProgram::unload_frag (void)
218{
219 glDetachShader(this->prog_id, this->frag_id);
221
222 glDeleteShader(this->frag_id);
224
225 this->frag_id = 0;
226}
227
228inline GLint
229ShaderProgram::get_attrib_location (char const* name)
230{
231 this->ensure_linked();
232 return glGetAttribLocation(this->prog_id, name);
233}
234
235inline GLint
236ShaderProgram::get_uniform_location (char const* name)
237{
238 this->ensure_linked();
239 GLint loc = glGetUniformLocation(this->prog_id, name);
240 return loc;
241}
242
243inline void
244ShaderProgram::send_uniform (char const* name, math::Vec3f const& v)
245{
246 GLint loc = this->get_uniform_location(name);
247 if (loc < 0)
248 return;
249 glUniform3fv(loc, 1, *v);
250}
251
252inline void
253ShaderProgram::send_uniform (char const* name, math::Vec4f const& v)
254{
255 GLint loc = this->get_uniform_location(name);
256 if (loc < 0)
257 return;
258 glUniform4fv(loc, 1, *v);
259}
260
261inline void
262ShaderProgram::send_uniform (const char* name, math::Matrix4f const& m)
263{
264 GLint loc = this->get_uniform_location(name);
265 if (loc < 0)
266 return;
267 glUniformMatrix4fv(loc, 1, true, *m);
268}
269
270inline void
271ShaderProgram::send_uniform (const char* name, GLint val)
272{
273 GLint loc = this->get_uniform_location(name);
274 if (loc < 0)
275 return;
276 glUniform1i(loc, val);
277}
278
279inline void
280ShaderProgram::send_uniform (const char* name, GLfloat val)
281{
282 GLint loc = this->get_uniform_location(name);
283 if (loc < 0)
284 return;
285 glUniform1f(loc, val);
286}
287
288inline void
289ShaderProgram::bind (void)
290{
291 this->ensure_linked();
292 glUseProgram(this->prog_id);
294}
295
296inline void
297ShaderProgram::unbind (void) const
298{
299 glUseProgram(0);
301}
302
303inline GLint
304ShaderProgram::get_program_property (int pname)
305{
306 GLint ret;
307 glGetProgramiv(this->prog_id, pname, &ret);
309 return ret;
310}
311
312inline GLint
313ShaderProgram::get_shader_property (GLuint shader_id, int pname)
314{
315 GLint ret;
316 glGetShaderiv(shader_id, pname, &ret);
317 check_gl_error();
318 return ret;
319}
320
322
323#endif /* OGL_SHADER_PROGRAM_HEADER */
Matrix class for arbitrary dimensions and types.
Definition matrix.h:54
Vector class for arbitrary dimensions and types.
Definition vector.h:87
Abstraction for OpenGL Shader Programs.
std::shared_ptr< ShaderProgram > Ptr
std::shared_ptr< ShaderProgram const > ConstPtr
void check_gl_error()
#define OGL_NAMESPACE_END
Definition defines.h:14
#define OGL_NAMESPACE_BEGIN
Definition defines.h:13