Vis 2 Demo  1.0
Technical illustration type real-time rendering of geometry
 All Classes Namespaces Files Functions Variables Typedefs Macros
ShaderController.cpp
Go to the documentation of this file.
1 #include "ShaderController.h"
2 
3 using namespace vis2;
4 
5 ShaderController::ShaderController(Shader* _shader_program, unsigned int _shader_type)
6  : shader_Ptr (_shader_program), shader_type (_shader_type),
7  shader_InVars_setup_done(false), shader_Uniforms_update_done(false),
8  vec_diffuse_color(glm::vec3(0.0f, 0.0f, 0.0f)),
9  vec_light_pos_0(glm::vec3(0.0f, 0.0f, 0.0f)), vec_light_col_0(glm::vec3(0.0f, 0.0f, 0.0f)),
10  vec_light_pos_1(glm::vec3(0.0f, 0.0f, 0.0f)), vec_light_col_1(glm::vec3(0.0f, 0.0f, 0.0f)),
11  vec_viewer_pos(glm::vec3(0.0f, 0.0f, 0.0f)), vec_viewer_lookAt(glm::vec3(0.0f, 0.0f, 1.0f)),
12  near_plane(1.0f), far_plane(10.0f),
13  use_gauss(1), gauss_kernel_size(3), blur_distance(0),
14  masking_color(glm::vec3(0.0f, 0.0f, 0.0f)), exculde_color(glm::vec3(0.0f, 0.0f, 0.0f)),
15  masking_tolerance(0.0f), use_prev_pass(0),
16  window_w(800.0f), window_h(600.0f),
17  depth_peel_bcgrd_col(glm::vec3(0.0f, 0.0f, 0.0f)),
18  blend_all_neighb_color(glm::vec3(1.0f, 1.0f, 1.0f))
19 {
20  // initialize the texture loading flags
21  for (unsigned int i = 0; i < VIS2_NR_TEXTURE_SLOTS; i++)
22  {
23  texture_loaded[i] = false;
24  }
25  // initialize the blending ceofficients for the depth peel (default alpha 0.5)
26  depth_peel_blend[0] = 0.5000f;
27  depth_peel_blend[1] = 0.2500f;
28  depth_peel_blend[2] = 0.1250f;
29  depth_peel_blend[3] = 0.1250f;
30 }
31 
33 {
34  // the textures here are destroyed from the caller -> nothing to do
35 }
36 
37 int ShaderController::setupShaderInVars( GLuint _position_buffer_handle,
38  GLuint _normal_buffer_handle,
39  GLuint _uv_buffer_handle,
40  GLuint _index_buffer_handle)
41 {
42  if (!this->shader_Ptr)
44 
45  try
46  {
47  // ------------------------- BIND VBOs to VAO ----------------------------------- //
48  // create and bind vao
49  glGenVertexArrays(1,&vao_handle);
50  glBindVertexArray(vao_handle);
51 
52  // bind positions
53  glBindBuffer(GL_ARRAY_BUFFER,_position_buffer_handle);
54  GLint shader_in_position_handle = glGetAttribLocation(shader_Ptr->program_handle,"position");
55  glEnableVertexAttribArray(shader_in_position_handle);
56  glVertexAttribPointer(shader_in_position_handle, 3, GL_FLOAT, GL_FALSE, 0, 0);
57 
58  // bind normals
63  {
64  glBindBuffer(GL_ARRAY_BUFFER,_normal_buffer_handle);
65  GLint shader_in_normal_handle = glGetAttribLocation(shader_Ptr->program_handle,"normal");
66  glEnableVertexAttribArray(shader_in_normal_handle);
67  glVertexAttribPointer(shader_in_normal_handle, 3, GL_FLOAT, GL_FALSE, 0, 0);
68  }
69 
70  // bind texture coordinates
72  {
73  glBindBuffer(GL_ARRAY_BUFFER,_uv_buffer_handle);
74  GLint shader_in_uv_handle = glGetAttribLocation(shader_Ptr->program_handle,"uv");
75  glEnableVertexAttribArray(shader_in_uv_handle);
76  glVertexAttribPointer(shader_in_uv_handle, 2, GL_FLOAT, GL_FALSE, 0, 0);
77  }
78 
79  // bind indices
80  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _index_buffer_handle);
81 
82  // ------------------------- DEACTIVATE ALL BUFFERS ----------------------------- //
83  glBindVertexArray(0); // deactivate vao
84  glBindBuffer(GL_ARRAY_BUFFER, 0); // deactivate vbo
85  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // deactivate vbo
86 
87  // ---------------------------------- END --------------------------------------- //
88  this->shader_InVars_setup_done = true;
90  }
91  catch( std::exception & e)
92  {
93  std::cout << "Error during setup of VAO for shader type " << shader_type << ": " << e.what() << std::endl;
95  }
96 
97 
98 }
99 
100 void ShaderController::assignTexture(Texture* _texture, const unsigned int _slot)
101 {
102  if (!_texture)
103  return;
104 
105  if (_slot >= 0 && _slot < VIS2_NR_TEXTURE_SLOTS)
106  {
107  this->texture_slots[_slot] = _texture;
108  this->texture_loaded[_slot] = true;
109  }
110 }
111 
112 void ShaderController::setLightSource(const glm::vec3 _light_pos, const glm::vec3 _light_col, const unsigned int _slot)
113 {
114  switch(_slot)
115  {
116  case VIS2_LIGHT_0:
117  {
118  this->vec_light_pos_0 = _light_pos;
119  this->vec_light_col_0 = _light_col;
120  }
121  break;
122  case VIS2_LIGHT_1:
123  {
124  this->vec_light_pos_1 = _light_pos;
125  this->vec_light_col_1 = _light_col;
126  }
127  break;
128  default:
129  break;
130  }
131 }
132 
133 void ShaderController::setViewData(const glm::vec3 _viewer_pos, const glm::vec3 _viewer_lookAt)
134 {
135  this->vec_viewer_pos = _viewer_pos;
136  this->vec_viewer_lookAt = _viewer_lookAt;
137 }
138 
139 void ShaderController::setNearFar(float _near, float _far)
140 {
141  this->near_plane = _near;
142  this->far_plane = _far;
143 }
144 
145 
146 void ShaderController::setBlurParams( const int _use_gaussian,
147  const int _gauss_kernel_size,
148  const int _blur_dist)
149 {
150  this->use_gauss = _use_gaussian;
151  if (_gauss_kernel_size == 3 || _gauss_kernel_size == 5 || _gauss_kernel_size == 7 || _gauss_kernel_size == 9)
152  this->gauss_kernel_size = _gauss_kernel_size;
153  this->blur_distance = _blur_dist;
154 }
155 
156 void ShaderController::setMaskingParams( const glm::vec3 _masking_color,
157  const glm::vec3 _exclude_color,
158  const float _masking_tolerance,
159  const int _use_prev_pass)
160 {
161  this->masking_tolerance = _masking_tolerance;
162  this->exculde_color = _exclude_color;
163  this->masking_color = _masking_color;
164  this->use_prev_pass = _use_prev_pass;
165 }
166 
167 void ShaderController::setPeelingParams( float _window_w, float _window_h)
168 {
169  this->window_w = _window_w;
170  this->window_h = _window_h;
171 }
172 
173 void ShaderController::setPeelingBlendParams(const float _alpha, const glm::vec3 _bcgrd_col)
174 {
175  if (_alpha >= 0.0f && _alpha <= 1.0f)
176  {
177  // blending coefficients = ( A, (1-A)A, (1-A)(1-A)A, (1-A)(1-A)(1-A) )
178 
179  this->depth_peel_blend[0] = _alpha;
180  this->depth_peel_blend[1] = (1.0f - _alpha);
181  this->depth_peel_blend[2] = (1.0f - _alpha) * this->depth_peel_blend[1];
182  this->depth_peel_blend[3] = (1.0f - _alpha) * this->depth_peel_blend[2];
183 
184  this->depth_peel_blend[1] *= _alpha;
185  this->depth_peel_blend[2] *= _alpha;
186  }
187  this->depth_peel_bcgrd_col = _bcgrd_col;
188 }
189 
190 void ShaderController::setBlendAllParams( const glm::vec3 & _neighb_shading_col)
191 {
192  this->blend_all_neighb_color = _neighb_shading_col;
193 }
194 
195 int ShaderController::updateShaderUniforms( glm::mat4 & _projectionMatrix,
196  glm::mat4 & _modelMatrix,
197  const glm::vec3 & _obj_color,
198  const glm::vec3 & _contour_color)
199 {
200 
201  // check if shader was activated first!
202  if (!shader_Ptr || !shader_Ptr->shaderInUse())
204 
205  try
206  {
207  // ------------------------- PASS UNIFORMS TO SHADER ---------------------------- //
208 
209  // MATRICES: geometry-related
210  glUniformMatrix4fv( glGetUniformLocation(shader_Ptr->program_handle,"projectionMatrix"),
211  1,
212  GL_FALSE,
213  glm::value_ptr(_projectionMatrix));
214  glUniformMatrix4fv( glGetUniformLocation(this->shader_Ptr->program_handle, "modelMatrix"),
215  1,
216  GL_FALSE,
217  glm::value_ptr(_modelMatrix));
218 
219  // VECTORS: environment-related
221  {
222  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "lightPos_0"),
223  this->vec_light_pos_0.x, this->vec_light_pos_0.y, this->vec_light_pos_0.z);
224 
225  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "lightPos_1"),
226  this->vec_light_pos_1.x, this->vec_light_pos_1.y, this->vec_light_pos_1.z);
227 
228  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "viewerPos"),
229  this->vec_viewer_pos.x, this->vec_viewer_pos.y, this->vec_viewer_pos.z);
230 
231  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "viewerLookAt"),
232  this->vec_viewer_lookAt.x, this->vec_viewer_lookAt.y, this->vec_viewer_lookAt.z);
233 
234  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "lightCol_0"),
235  this->vec_light_col_0.x, this->vec_light_col_0.y, this->vec_light_col_0.z);
236 
237  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "lightCol_1"),
238  this->vec_light_col_1.x, this->vec_light_col_1.y, this->vec_light_col_1.z);
239 
240  glUniform1f(glGetUniformLocation(this->shader_Ptr->program_handle, "near"), this->near_plane);
241  glUniform1f(glGetUniformLocation(this->shader_Ptr->program_handle, "far"), this->far_plane);
242 
243  }
244 
245  // OBJECT ATTRIBUTES:
247  {
248  // objectColor
249  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "objectColor"),
250  _obj_color.x, _obj_color.y, _obj_color.z);
251 
252  }
253 
254  // DIFFUSE TEXTURES (for shaders needing at least one):
256  {
259 
260  int tex_unit = VIS2_TEXTURE_DIFFUSE_SLOT;
262  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"diffColorTexture"), tex_unit);
263  }
264 
265  // TEXTURE FILTERS:
267  {
268  glm::vec2 pixel_size = glm::vec2(1.0 / this->texture_slots[VIS2_TEXTURE_DIFFUSE_SLOT]->getTextureWidth(),
269  1.0 / this->texture_slots[VIS2_TEXTURE_DIFFUSE_SLOT]->getTextureHeight());
270  glUniform2f(glGetUniformLocation(this->shader_Ptr->program_handle, "pixelSize"), pixel_size.x, pixel_size.y);
271  }
272 
273  // EDGE DETECTION:
275  {
276  // HORIZONTAL + VERTICAL PASS
277  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "edgeColor"),
278  _contour_color.x, _contour_color.y, _contour_color.z);
279 
280  // VERTICAL PASS
282  {
285 
286  texture_slots[VIS2_TEXTURE_1]->textureBind(VIS2_TEXTURE_1);
287  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"edgeHrzTexture"), VIS2_TEXTURE_1);
288 
289  }
290  }
291 
292  // GAUSSIAN BLUR KERNEL SIZES:
294  {
295  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"useGauss"), this->use_gauss);
296  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"kernelSize"), this->gauss_kernel_size);
297  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"blurDist"), this->blur_distance);
298  }
299 
300  // MASKING PARAMETERS:
302  {
303  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "maskingColor"),
304  this->masking_color.x, this->masking_color.y, this->masking_color.z);
305  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "excludedColor"),
306  this->exculde_color.x, this->exculde_color.y, this->exculde_color.z);
307  glUniform1f(glGetUniformLocation(this->shader_Ptr->program_handle, "toleranceMasking"), this->masking_tolerance);
308  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"usePrevPass"), this->use_prev_pass);
309 
310  if (this->use_prev_pass == 1 && !texture_loaded[VIS2_TEXTURE_1])
312 
313  texture_slots[VIS2_TEXTURE_1]->textureBind(VIS2_TEXTURE_1);
314  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"maskPrevPass"), VIS2_TEXTURE_1);
315  }
316 
317  // DEPTH PEELING PARAMETERS:
319  {
320  // fragment coordinates are not normalized !!!
321  glUniform2f(glGetUniformLocation(this->shader_Ptr->program_handle,"windowSize"), this->window_w, this->window_h);
322 
325 
326  texture_slots[VIS2_TEXTURE_1]->textureBind(VIS2_TEXTURE_1);
327  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"depthPrevPassTexture"), VIS2_TEXTURE_1);
328  }
329 
330  // DEPTH PEELING BLEND PARAMETERS:
332  {
335 
337  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"diffLayer1"), VIS2_TEXTURE_1);
338  texture_slots[VIS2_TEXTURE_2]->textureBind(VIS2_TEXTURE_2);
339  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"diffLayer2"), VIS2_TEXTURE_2);
340 
341  glUniform4f(glGetUniformLocation(this->shader_Ptr->program_handle, "blendCoeff"),
342  this->depth_peel_blend[0], this->depth_peel_blend[1], this->depth_peel_blend[2], this->depth_peel_blend[3]);
343 
344  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "backgroundColor"),
345  this->depth_peel_bcgrd_col.r, this->depth_peel_bcgrd_col.g, this->depth_peel_bcgrd_col.b);
346  }
347 
348  // BLENDING PARAMETERS:
350  {
354 
356  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"diffContentTexture"), VIS2_TEXTURE_1);
358  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"maskSelectedTexture"), VIS2_TEXTURE_2);
360  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"maskNeighborsTexture"), VIS2_TEXTURE_3);
361  texture_slots[VIS2_TEXTURE_4]->textureBind(VIS2_TEXTURE_4);
362  glUniform1i(glGetUniformLocation(this->shader_Ptr->program_handle,"edgesContentTexture"), VIS2_TEXTURE_4);
363 
364  glUniform3f(glGetUniformLocation(this->shader_Ptr->program_handle, "neighborColor"),
365  this->blend_all_neighb_color.r, this->blend_all_neighb_color.g, this->blend_all_neighb_color.b);
366  }
367 
368 
369  // ---------------------------------- END --------------------------------------- //
370  this->shader_Uniforms_update_done = true;
372  }
373  catch( std::exception & e)
374  {
375  std::cout << "Error during setup of uniforms for shader type " << shader_type << ": " << e.what() << std::endl;
377  }
378 
379 }