Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
shader.hpp
Go to the documentation of this file.
1 
23 #ifndef __TUCANOSHADER__
24 #define __TUCANOSHADER__
25 
26 #include "utils/misc.hpp"
27 
28 #include <fstream>
29 #include <vector>
30 #include <Eigen/Dense>
31 #include <memory>
32 
33 using namespace std;
34 
35 namespace Tucano
36 {
37 
45 class Shader {
46 
47 private:
48 
50  string shaderName;
51 
54 
57 
60 
63 
66 
69 
71  string vertex_code;
72 
74  string fragment_code;
75 
77  string geometry_code;
78 
81 
84 
87 
89  GLuint computeShader = 0;
90 
92  GLuint vertexShader = 0;
93 
95  GLuint tessellationControlShader = 0;
96 
98  GLuint tessellationEvaluationShader = 0;
99 
101  GLuint geometryShader = 0;
102 
104  GLuint fragmentShader = 0;
105 
107  GLuint shaderProgram = 0;
108 
110  int debug_level = 1;
111 
113  std::shared_ptr < GLuint > programID_sptr = 0;
114  std::shared_ptr < GLuint > vertexID_sptr = 0;
115  std::shared_ptr < GLuint > fragID_sptr = 0;
116  std::shared_ptr < GLuint > geomID_sptr = 0;
117  std::shared_ptr < GLuint > tessEvalID_sptr = 0;
118  std::shared_ptr < GLuint > tessContID_sptr = 0;
119  std::shared_ptr < GLuint > computeID_sptr = 0;
120 
121 public:
122 
127  /*Shader (const Shader& other)
128  {
129  shaderName = other.shaderName;
130  vertexShaderPath = other.vertexShaderPath;
131  fragmentShaderPath = other.fragmentShaderPath;
132  geometryShaderPath = other.geometryShaderPath;
133  tessellationControlShaderPath = other.tessellationControlShaderPath;
134  tessellationEvaluationShaderPath = other.tessellationEvaluationShaderPath;
135  computeShaderPaths = other.computeShaderPaths;
136 
137  vertex_code = other.vertex_code;
138  fragment_code = other.fragment_code;
139  geometry_code = other.geometry_code;
140  tessellation_evaluation_code = other.tessellation_evaluation_code;
141  tessellation_control_code = other.tessellation_control_code;
142 
143  debug_level = other.debug_level;
144 
145  initializeFromStrings(vertex_code, fragment_code, geometry_code, tessellation_evaluation_code, tessellation_control_code);
146 
147  #ifdef TUCANODEBUG
148  Misc::errorCheckFunc(__FILE__, __LINE__);
149  #endif
150  }*/
151 
155  void createProgramID (void)
156  {
157 
158  shaderProgram = glCreateProgram();
159  programID_sptr = std::shared_ptr < GLuint > (
160  new GLuint (shaderProgram),
161  [] (GLuint *p) {
162  glDeleteProgram(*p);
163  delete p;
164  }
165  );
166 
167  }
168 
172  void createShaders (void)
173  {
174 
175  if (!(fragmentShaderPath.empty() && fragment_code.empty()) && fragID_sptr == 0)
176  {
177  fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
178  fragID_sptr = std::shared_ptr < GLuint > (
179  new GLuint (fragmentShader),
180  [] (GLuint *p) {
181  glDeleteShader(*p);
182  delete p;
183  }
184  );
185  }
186 
187  if (!(vertexShaderPath.empty() && vertex_code.empty()) && vertexID_sptr == 0)
188  {
189  vertexShader = glCreateShader(GL_VERTEX_SHADER);
190  vertexID_sptr = std::shared_ptr < GLuint > (
191  new GLuint (vertexShader),
192  [] (GLuint *p) {
193  glDeleteShader(*p);
194  delete p;
195  }
196  );
197  }
198 
199  if (!(geometryShaderPath.empty() && geometry_code.empty()) && geomID_sptr == 0)
200  {
201  geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
202  geomID_sptr = std::shared_ptr < GLuint > (
203  new GLuint (geometryShader),
204  [] (GLuint *p) {
205  glDeleteShader(*p);
206  delete p;
207  }
208  );
209  }
210 
211 
212  if (!(tessellationControlShaderPath.empty() && tessellation_control_code.empty()) && tessContID_sptr == 0)
213  {
214  tessellationControlShader = glCreateShader(GL_TESS_CONTROL_SHADER);
215  tessContID_sptr = std::shared_ptr < GLuint > (
216  new GLuint (tessellationControlShader),
217  [] (GLuint *p) {
218  glDeleteShader(*p);
219  delete p;
220  }
221  );
222  }
223 
224  if (!(tessellationEvaluationShaderPath.empty() && tessellation_evaluation_code.empty()) && tessEvalID_sptr == 0)
225  {
226  tessellationEvaluationShader = glCreateShader(GL_TESS_EVALUATION_SHADER);
227  tessEvalID_sptr = std::shared_ptr < GLuint > (
228  new GLuint (tessellationEvaluationShader),
229  [] (GLuint *p) {
230  glDeleteShader(*p);
231  delete p;
232  }
233  );
234  }
235 
236 
237  if (!(computeShaderPath.empty() && compute_shader_code.empty()) && computeID_sptr == 0)
238  {
239  computeShader = glCreateShader(GL_COMPUTE_SHADER);
240  computeID_sptr = std::shared_ptr < GLuint > (
241  new GLuint (computeShader),
242  [] (GLuint *p) {
243  glDeleteShader(*p);
244  delete p;
245  }
246  );
247  }
248 
249 
250  #ifdef TUCANODEBUG
251  Tucano::Misc::errorCheckFunc(__FILE__, __LINE__, "mesh constructor");
252  #endif
253 
254  }
255 
267  Shader (string name, string vertex_shader_path, string fragment_shader_path, string geometry_shader_path = "", string tessellation_evaluation_shader_path = "", string tessellation_control_shader_path = "")
268  {
269  shaderName = name;
270  vertexShaderPath = vertex_shader_path;
271  tessellationControlShaderPath = tessellation_control_shader_path;
272  tessellationEvaluationShaderPath = tessellation_evaluation_shader_path;
273  geometryShaderPath = geometry_shader_path;
274  fragmentShaderPath = fragment_shader_path;
275 
276  createProgramID();
277  }
278 
286  Shader (string name, string shader_dir)
287  {
288  load (name, shader_dir);
289  createProgramID();
290  }
291 
292 
297  {
298  createProgramID();
299  }
300 
306  /*Shader (string name, string compute_shader_path)
307  {
308  shaderName = name;
309  computeShaderPath = compute_shader_path;
310  createProgramID();
311  }*/
312 
316  /* ~Shader (void)
317  {
318  deleteShaders();
319 
320  #ifdef TUCANODEBUG
321  Misc::errorCheckFunc(__FILE__, __LINE__, "Shader destructor");
322  #endif
323  }*/
324 
329  void setShaderName (string name)
330  {
331  shaderName = name;
332  }
333 
340  string getShaderName (void)
341  {
342  return shaderName;
343  }
344 
349  GLuint getShaderProgram (void)
350  {
351  if (programID_sptr)
352  return *programID_sptr;
353  return 0;
354  }
355 
360  GLuint getFragmentShader (void)
361  {
362  if (fragID_sptr)
363  return *fragID_sptr;
364  return 0;
365  }
366 
371  GLuint getVertexShader (void)
372  {
373  if (vertexID_sptr)
374  return *vertexID_sptr;
375  return 0;
376  }
377 
382  GLuint getGeometryShader (void)
383  {
384  if (geomID_sptr)
385  return *geomID_sptr;
386  return 0;
387  }
388 
394  {
395  if (tessContID_sptr)
396  return *tessContID_sptr;
397  return 0;
398  }
399 
400 
406  {
407  if (tessEvalID_sptr)
408  return *tessEvalID_sptr;
409  return 0;
410  }
411 
412 
413 
417  GLuint getComputeShader (void)
418  {
419  if (computeID_sptr)
420  return *computeID_sptr;
421  return 0;
422  }
423 
424 
425 public:
426 
427 
436  void load (string name, string shader_dir = "")
437  {
438  shaderName = name;
439 
440  bool found = false;
441 
442  //Vertex:
443  string vs_name = shader_dir + name + ".vert";
444  ifstream vertex_file(vs_name.c_str());
445  if (vertex_file.good())
446  {
447  vertexShaderPath = vs_name;
448  found = true;
449  }
450 
451  //Tessellation Control:
452  string tesc_name = shader_dir + name + ".tesc";
453  ifstream tesc_file(tesc_name.c_str());
454  if (tesc_file.good())
455  {
456  tessellationControlShaderPath = tesc_name;
457  found = true;
458  }
459 
460  //Tessellation Evaluation:
461  string tese_name = shader_dir + name + ".tese";
462  ifstream tese_file(tese_name.c_str());
463  if (tese_file.good())
464  {
465  tessellationEvaluationShaderPath = tese_name;
466  found = true;
467  }
468 
469  //Geometry:
470  string gs_name = shader_dir + name + ".geom";
471  ifstream geom_file(gs_name.c_str());
472  if (geom_file.good())
473  {
474  geometryShaderPath = gs_name;
475  found = true;
476  }
477 
478  //Fragment:
479  string fs_name = shader_dir + name + ".frag";
480  ifstream fragment_file(fs_name.c_str());
481  if (fragment_file.good())
482  {
483  fragmentShaderPath = fs_name;
484  found = true;
485  }
486 
487  //Compute:
488  string cs_name = shader_dir + name + ".comp";
489  ifstream comp_file(cs_name.c_str());
490  if (comp_file.good())
491  {
492  computeShaderPath = cs_name;
493  found = true;
494  }
495 
496  // if no shader was found, emit an warning
497  if (!found)
498  {
499  std::cerr << "Warning: no shader " << name.c_str() << " file found in directory : " << shader_dir.c_str() << std::endl;
500  }
501 
502  }
503 
504 
508  void linkProgram (void)
509  {
510 
511  glLinkProgram(*programID_sptr);
512 
513  GLint result = GL_FALSE;
514  glGetProgramiv(*programID_sptr, GL_LINK_STATUS, &result);
515  if (result != GL_TRUE)
516  {
517  std::cerr << "Error linking program : " << shaderName << std::endl;
518  GLchar errorLog[1024] = {0};
519  glGetProgramInfoLog(*programID_sptr, 1024, NULL, errorLog);
520  fprintf(stdout, "%s", &errorLog[0]);
521  std::cerr << std::endl;
522  }
523  #ifdef TUCANODEBUG
524  else
525  {
526  std::cout << " Successfully linked : " << shaderName << std::endl << std::endl;
527  }
528  #endif
529  }
530 
531 
541  void initializeTF (int size, const char** varlist, GLenum buffer_mode = GL_INTERLEAVED_ATTRIBS)
542  {
543  createShaders();
544  if(!vertexShaderPath.empty())
545  {
546  readVertexCode();
547  // tessellation control shader needs a vertex shader
548  if (!tessellationControlShaderPath.empty())
549  {
550  readTessellationControlCode();
551  }
552  // tessellation evaluation shader needs a vertex shader
553  if (!tessellationEvaluationShaderPath.empty())
554  {
555  readTessellationEvaluationCode();
556  }
557  // geom shader needs a vertex shader
558  if(!geometryShaderPath.empty())
559  {
560  readGeometryCode();
561  }
562  }
563  if(!fragmentShaderPath.empty())
564  {
565  readFragmentCode();
566  }
567  if(!computeShaderPath.empty())
568  {
569  readComputeShaderCode();
570  }
571 
572  glTransformFeedbackVaryings(*programID_sptr, size, varlist, buffer_mode);
573 
574  linkProgram();
575 
576  #ifdef TUCANODEBUG
577  Misc::errorCheckFunc(__FILE__, __LINE__);
578  #endif
579  }
580 
589  void initializeFromStrings (string in_vertex_code, string in_fragment_code, string in_geometry_code = "", string in_tessellation_evaluation_code = "", string in_tessellation_control_code = "")
590  {
591  vertex_code = in_vertex_code;
592  fragment_code = in_fragment_code;
593  geometry_code = in_geometry_code;
594  tessellation_evaluation_code = in_tessellation_evaluation_code;
595  tessellation_control_code = in_tessellation_control_code;
596 
597  createShaders();
598 
599  if (vertex_code.empty())
600  {
601  std::cerr << "warning: " << shaderName.c_str() << " : empty vertex string code!" << std::endl;
602  }
603  else
604  {
605  setVertexShader(vertex_code);
606  // tessellation control shader is optional, but needs needs a vertex shader
607  if (!tessellationControlShaderPath.empty())
608  {
609  setTessellationControlShader(tessellation_control_code);
610  }
611  // tessellation evaluation shader is optional, but needs a vertex shader
612  if (!tessellationEvaluationShaderPath.empty())
613  {
614  setTessellationEvaluationShader(tessellation_evaluation_code);
615  }
616  // geometry shader is optional, but need a vertex shader if is set
617  if (!geometry_code.empty())
618  {
619  setGeometryShader(geometry_code);
620  }
621  }
622  if (fragment_code.empty())
623  {
624  std::cerr << "warning: " << shaderName.c_str() << " : empty fragment string code!" << std::endl;
625  }
626  else
627  {
628  setFragmentShader(fragment_code);
629  }
630 
631  linkProgram();
632 
633  #ifdef TUCANODEBUG
634  Misc::errorCheckFunc(__FILE__, __LINE__);
635  #endif
636  }
637 
641  void initialize (void)
642  {
643  createShaders();
644  if(!vertexShaderPath.empty())
645  {
646  readVertexCode();
647  // tessellation control shader needs a vertex shader
648  if (!tessellationControlShaderPath.empty())
649  {
650  readTessellationControlCode();
651  }
652  // tessellation evaluation shader needs a vertex shader
653  if (!tessellationEvaluationShaderPath.empty())
654  {
655  readTessellationEvaluationCode();
656  }
657  // geom shader needs a vertex shader
658  if(!geometryShaderPath.empty()) {
659  readGeometryCode();
660  }
661  }
662  if(!fragmentShaderPath.empty())
663  {
664  readFragmentCode();
665  }
666  if(!computeShaderPath.empty())
667  {
668  readComputeShaderCode();
669  }
670 
671  linkProgram();
672 
673  #ifdef TUCANODEBUG
674  Misc::errorCheckFunc(__FILE__, __LINE__);
675  #endif
676  }
677 
682  void setVertexShader (string &vertexShaderCode)
683  {
684  GLint result = GL_FALSE;
685  int infoLogLength;
686 
687  char const * vertexSourcePointer = vertexShaderCode.c_str();
688  glShaderSource(*vertexID_sptr, 1, &vertexSourcePointer , NULL);
689  glCompileShader(*vertexID_sptr);
690 
691  // Check Vertex Shader
692  glGetShaderiv(*vertexID_sptr, GL_COMPILE_STATUS, &result);
693  if (result != GL_TRUE)
694  {
695  // if an error is found, print the log (even if not in debug mode)
696  std::cerr << "Erro compiling vertex shader: " << vertexShaderPath << std::endl;
697  glGetShaderiv(*vertexID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
698  char * vertexShaderErrorMessage = new char[infoLogLength];
699  glGetShaderInfoLog(*vertexID_sptr, infoLogLength, NULL, &vertexShaderErrorMessage[0]);
700  fprintf(stdout, "\n%s", &vertexShaderErrorMessage[0]);
701  delete [] vertexShaderErrorMessage;
702  }
703  #ifdef TUCANODEBUG
704  else
705  {
706  if (vertexShaderPath.empty())
707  {
708  std::cout << "Compiled vertex shader from string without errors : " << shaderName.c_str() << std::endl;
709  }
710  else
711  {
712  std::cout << "Compiled vertex shader without errors : " << vertexShaderPath << std::endl;
713  }
714  }
715  #endif
716 
717  glAttachShader(*programID_sptr, *vertexID_sptr);
718 
719  #ifdef TUCANODEBUG
720  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading vertex shader code");
721  #endif
722 
723  }
724 
728  void readVertexCode (void)
729  {
730  // Read the Vertex Shader code from the file
731  string vertexShaderCode;
732 
733  ifstream vertexShaderStream(vertexShaderPath.c_str(), std::ios::in);
734 
735  if(vertexShaderStream.is_open())
736  {
737  string line = "";
738  while(getline(vertexShaderStream, line))
739  {
740  vertexShaderCode += "\n" + line;
741  }
742  vertexShaderStream.close();
743  }
744  else
745  {
746  std::cout << "warning: no vertex shader file found : " << vertexShaderPath << std::endl;
747  }
748 
749  setVertexShader(vertexShaderCode);
750  vertex_code = vertexShaderCode;
751  }
752 
757  void setTessellationControlShader(string &tessellationControlCode)
758  {
759  GLint result = GL_FALSE;
760  int infoLogLength;
761 
762  char const * tessellationControlSourcePointer = tessellationControlCode.c_str();
763  glShaderSource(*tessContID_sptr, 1, &tessellationControlSourcePointer , NULL);
764  glCompileShader(*tessContID_sptr);
765 
766  // Check Vertex Shader
767  glGetShaderiv(*tessContID_sptr, GL_COMPILE_STATUS, &result);
768  if (result != GL_TRUE)
769  {
770  // if an error is found, print the log (even if not in debug mode)
771  std::cerr << "Erro compiling tessellation evaluation shader: " << tessellationControlShaderPath << std::endl;
772  glGetShaderiv(*tessContID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
773  char * tessellationControlShaderErrorMessage = new char[infoLogLength];
774  glGetShaderInfoLog(*tessContID_sptr, infoLogLength, NULL, &tessellationControlShaderErrorMessage[0]);
775  fprintf(stdout, "\n%s", &tessellationControlShaderErrorMessage[0]);
776  delete [] tessellationControlShaderErrorMessage;
777  }
778  #ifdef TUCANODEBUG
779  else
780  {
781  if (tessellationControlShaderPath.empty())
782  {
783  std::cout << "Compiled tessellation control shader from string without errors : " << shaderName.c_str() << std::endl;
784  }
785  else
786  {
787  std::cout << "Compiled tessellation control shader without errors : " << tessellationControlShaderPath << std::endl;
788  }
789  }
790  #endif
791 
792  glAttachShader(*programID_sptr, *tessContID_sptr);
793 
794  #ifdef TUCANODEBUG
795  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading tessellation control shader code");
796  #endif
797  }
798 
803  {
804  // Read the tessellation control Shader code from the file
805  string tessellationControlShaderCode;
806 
807  ifstream tessellationControlShaderStream(tessellationControlShaderPath.c_str(), std::ios::in);
808  if(tessellationControlShaderStream.is_open())
809  {
810  string line = "";
811  while(getline(tessellationControlShaderStream, line))
812  {
813  tessellationControlShaderCode += "\n" + line;
814  }
815  tessellationControlShaderStream.close();
816  }
817  else
818  {
819  std::cout << "warning: no tessellation control shader file found : " << tessellationControlShaderPath << std::endl;
820  }
821 
822  setTessellationControlShader(tessellationControlShaderCode);
823  tessellation_control_code = tessellationControlShaderCode;
824  }
825 
830  void setTessellationEvaluationShader(string &tessellationEvaluationCode)
831  {
832  GLint result = GL_FALSE;
833  int infoLogLength;
834 
835  char const * tessellationEvaluationSourcePointer = tessellationEvaluationCode.c_str();
836  glShaderSource(*tessEvalID_sptr, 1, &tessellationEvaluationSourcePointer , NULL);
837  glCompileShader(*tessEvalID_sptr);
838 
839  // Check Vertex Shader
840  glGetShaderiv(*tessEvalID_sptr, GL_COMPILE_STATUS, &result);
841  if (result != GL_TRUE)
842  {
843  // if an error is found, print the log (even if not in debug mode)
844  std::cerr << "Erro compiling tessellation evaluation shader: " << tessellationEvaluationShaderPath << std::endl;
845  glGetShaderiv(*tessEvalID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
846  char * tessellationEvaluationShaderErrorMessage = new char[infoLogLength];
847  glGetShaderInfoLog(*tessEvalID_sptr, infoLogLength, NULL, &tessellationEvaluationShaderErrorMessage[0]);
848  fprintf(stdout, "\n%s", &tessellationEvaluationShaderErrorMessage[0]);
849  delete [] tessellationEvaluationShaderErrorMessage;
850  }
851  #ifdef TUCANODEBUG
852  else
853  {
854  if (tessellationEvaluationShaderPath.empty())
855  {
856  std::cout << "Compiled tessellation evaluation shader from string without errors : " << shaderName.c_str() << std::endl;
857  }
858  else
859  {
860  std::cout << "Compiled tessellation evaluation shader without errors : " << tessellationEvaluationShaderPath << std::endl;
861  }
862  }
863  #endif
864 
865  glAttachShader(*programID_sptr, *tessEvalID_sptr);
866 
867  #ifdef TUCANODEBUG
868  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading tessellation evaluation shader code");
869  #endif
870  }
871 
876  {
877  // Read the Vertex Shader code from the file
878  string tessellationEvaluationShaderCode;
879 
880  ifstream tessellationEvaluationShaderStream(tessellationEvaluationShaderPath.c_str(), std::ios::in);
881 
882  if(tessellationEvaluationShaderStream.is_open())
883  {
884  string line = "";
885  while(getline(tessellationEvaluationShaderStream, line))
886  {
887  tessellationEvaluationShaderCode += "\n" + line;
888  }
889  tessellationEvaluationShaderStream.close();
890  }
891  else
892  {
893  std::cout << "warning: no tessellation evaluation shader file found : " << tessellationEvaluationShaderPath << std::endl;
894  }
895 
896  setTessellationEvaluationShader(tessellationEvaluationShaderCode);
897  tessellation_evaluation_code = tessellationEvaluationShaderCode;
898  }
899 
904  void setGeometryShader(string &geometryShaderCode)
905  {
906  GLint result = GL_FALSE;
907  int infoLogLength;
908 
909  char const * geometrySourcePointer = geometryShaderCode.c_str();
910  glShaderSource(*geomID_sptr, 1, &geometrySourcePointer , NULL);
911  glCompileShader(geometryShader);
912 
913  // Check Geometry Shader
914  glGetShaderiv(*geomID_sptr, GL_COMPILE_STATUS, &result);
915  if (result != GL_TRUE)
916  {
917  // if an error is found, print the log (even if not in debug mode)
918  std::cerr << "Erro compiling geometry shader: " << geometryShaderPath << std::endl;
919  glGetShaderiv(*geomID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
920  char * geometryShaderErrorMessage = new char[infoLogLength];
921  glGetShaderInfoLog(*geomID_sptr, infoLogLength, NULL, &geometryShaderErrorMessage[0]);
922  fprintf(stdout, "\n%s", &geometryShaderErrorMessage[0]);
923  delete [] geometryShaderErrorMessage;
924  }
925  #ifdef TUCANODEBUG
926  else
927  {
928  if (geometryShaderPath.empty())
929  {
930  std::cout << "Compiled geometry shader from string without errors : " << shaderName.c_str() << std::endl;
931  }
932  else
933  {
934  std::cout << "Compiled geometry shader without errors : " << geometryShaderPath << std::endl;
935  }
936  }
937  #endif
938 
939  glAttachShader(*programID_sptr, *geomID_sptr);
940 
941  #ifdef TUCANODEBUG
942  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading geometry shader code");
943  #endif
944 
945  }
946 
950  void readGeometryCode (void)
951  {
952  // Read the Geometry Shader code from the file
953  string geometryShaderCode;
954 
955  ifstream geometryShaderStream(geometryShaderPath.c_str(), std::ios::in);
956 
957  if(geometryShaderStream.is_open())
958  {
959  string line = "";
960  while(getline(geometryShaderStream, line))
961  {
962  geometryShaderCode += "\n" + line;
963  }
964  geometryShaderStream.close();
965  }
966  else
967  {
968  std::cerr << "warning: no geom shader found : " << geometryShaderPath << std::endl;
969  }
970 
971  setGeometryShader(geometryShaderCode);
972  geometry_code = geometryShaderCode;
973  }
974 
979  void setFragmentShader (string& fragmentShaderCode)
980  {
981  GLint result = GL_FALSE;
982  int infoLogLength;
983 
984  char const * fragmentSourcePointer = fragmentShaderCode.c_str();
985  glShaderSource(*fragID_sptr, 1, &fragmentSourcePointer , NULL);
986  glCompileShader(fragmentShader);
987 
988  // Check Fragment Shader
989  glGetShaderiv(*fragID_sptr, GL_COMPILE_STATUS, &result);
990  if (result != GL_TRUE)
991  {
992  std::cerr << "Erro compiling fragment shader: " << fragmentShaderPath << std::endl;
993  glGetShaderiv(*fragID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
994  char * fragmentShaderErrorMessage = new char[infoLogLength];
995  glGetShaderInfoLog(*fragID_sptr, infoLogLength, NULL, &fragmentShaderErrorMessage[0]);
996  fprintf(stdout, "\n%s", &fragmentShaderErrorMessage[0]);
997  delete [] fragmentShaderErrorMessage;
998  }
999  #ifdef TUCANODEBUG
1000  else
1001  {
1002  if (fragmentShaderPath.empty())
1003  {
1004  std::cout << "Compiled fragment shader from string without errors : " << shaderName.c_str() << std::endl;
1005  }
1006  else
1007  {
1008  std::cout << "Compiled fragment shader without errors : " << fragmentShaderPath << std::endl;
1009  }
1010  }
1011  #endif
1012 
1013  glAttachShader(*programID_sptr, *fragID_sptr);
1014 
1015  #ifdef TUCANODEBUG
1016  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading fragment shader code");
1017  #endif
1018 
1019  }
1020 
1024  void readFragmentCode (void)
1025  {
1026  // Read the Fragment Shader code from the file
1027  string fragmentShaderCode;
1028  ifstream fragmentShaderStream(fragmentShaderPath.c_str(), std::ios::in);
1029  if(fragmentShaderStream.is_open())
1030  {
1031  string line = "";
1032  while(getline(fragmentShaderStream, line))
1033  {
1034  fragmentShaderCode += "\n" + line;
1035  }
1036  fragmentShaderStream.close();
1037  }
1038  else
1039  {
1040  std::cerr << "warning: no fragment shader found : " << fragmentShaderPath << std::endl;
1041  }
1042 
1043  setFragmentShader(fragmentShaderCode);
1044  fragment_code = fragmentShaderCode;
1045  }
1046 
1047 
1052  void setComputeShader (string& computeShaderCode)
1053  {
1054  GLint result = GL_FALSE;
1055  int infoLogLength;
1056 
1057  char const * computeSourcePointer = computeShaderCode.c_str();
1058  glShaderSource(*computeID_sptr, 1, &computeSourcePointer , NULL);
1059  glCompileShader(*computeID_sptr);
1060 
1061  // Check Compute Shader
1062  glGetShaderiv(*computeID_sptr, GL_COMPILE_STATUS, &result);
1063  if (result != GL_TRUE)
1064  {
1065  std::cerr << "Erro compiling compute shader: " << computeShaderPath << std::endl;
1066  glGetShaderiv(*computeID_sptr, GL_INFO_LOG_LENGTH, &infoLogLength);
1067  char * computeShaderErrorMessage = new char[infoLogLength];
1068  glGetShaderInfoLog(*computeID_sptr, infoLogLength, NULL, &computeShaderErrorMessage[0]);
1069  fprintf(stdout, "\n%s", &computeShaderErrorMessage[0]);
1070  delete [] computeShaderErrorMessage;
1071  }
1072  #ifdef TUCANODEBUG
1073  else
1074  {
1075  if (computeShaderPath.empty())
1076  {
1077  std::cout << "Compiled compute shader from string without errors : " << shaderName.c_str() << std::endl;
1078  }
1079  else
1080  {
1081  std::cout << "Compiled compute shader without errors : " << computeShaderPath << std::endl;
1082  }
1083  }
1084  #endif
1085 
1086  glAttachShader(*programID_sptr, *computeID_sptr);
1087 
1088  #ifdef TUCANODEBUG
1089  Misc::errorCheckFunc(__FILE__, __LINE__, "error loading compute shader code");
1090  #endif
1091  }
1092 
1097  {
1098  string computeShaderCode;
1099  ifstream computeShaderStream(computeShaderPath.c_str(), std::ios::in);
1100  if(computeShaderStream.is_open())
1101  {
1102  string line = "";
1103  while(getline(computeShaderStream, line))
1104  {
1105  computeShaderCode += "\n" + line;
1106  }
1107  computeShaderStream.close();
1108  }
1109  else
1110  {
1111  std::cerr << "warning: no compute shader found : " << computeShaderPath << std::endl;
1112  }
1113 
1114  setComputeShader(computeShaderCode);
1115  compute_shader_code = computeShaderCode;
1116 
1117  }
1118 
1119 
1126  void reloadShaders (void)
1127  {
1128 
1129  #ifdef TUCANODEBUG
1130  std::cout << "reloading shaders" << std::endl;
1131  #endif
1132 
1133  if(vertexID_sptr)
1134  {
1135  glDetachShader(*programID_sptr, *vertexID_sptr);
1136  readVertexCode();
1137  }
1138  if (tessContID_sptr)
1139  {
1140  glDetachShader(*programID_sptr, *tessContID_sptr);
1141  readTessellationControlCode();
1142  }
1143  if (tessEvalID_sptr)
1144  {
1145  glDetachShader(*programID_sptr, *tessEvalID_sptr);
1146  readTessellationEvaluationCode();
1147  }
1148  if(geomID_sptr)
1149  {
1150  glDetachShader(*programID_sptr, *geomID_sptr);
1151  readGeometryCode();
1152  }
1153  if(fragID_sptr)
1154  {
1155  glDetachShader(*programID_sptr, *fragID_sptr);
1156  readFragmentCode();
1157  }
1158  if(computeID_sptr)
1159  {
1160  glDetachShader(*programID_sptr, *computeID_sptr);
1161  readComputeShaderCode();
1162  }
1163 
1164  linkProgram();
1165 
1166  #ifdef TUCANODEBUG
1167  Misc::errorCheckFunc(__FILE__, __LINE__);
1168  #endif
1169  }
1170 
1176  void bind (void)
1177  {
1178  glUseProgram(*programID_sptr);
1179  }
1180 
1184  void unbind (void)
1185  {
1186  glUseProgram(0);
1187  }
1188 
1192  /*void deleteShaders (void)
1193  {
1194  glDetachShader(*programID_sptr, fragmentShader);
1195  glDetachShader(*programID_sptr, vertexShader);
1196  glDeleteShader(fragmentShader);
1197  glDeleteShader(vertexShader);
1198  glDeleteProgram(*programID_sptr);
1199  }*/
1200 
1205  void getActiveAttributes( vector< string > &attribs )
1206  {
1207  int maxlength = 0;
1208  int numattribs = 0;
1209 
1210  glGetProgramiv (*programID_sptr, GL_ACTIVE_ATTRIBUTES, &numattribs);
1211  glGetProgramiv (*programID_sptr, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxlength);
1212 
1213  int length = 0;
1214  int size = 0;
1215  GLuint type = 0;
1216  char* name = new char[maxlength];
1217  for (int i = 0; i < numattribs; ++i)
1218  {
1219  glGetActiveAttrib(*programID_sptr, i, maxlength, &length, &size, &type, name);
1220  attribs.push_back(name);
1221  }
1222  delete [] name;
1223  }
1224 
1230  GLint getUniformLocation (const GLchar* name) const
1231  {
1232  return glGetUniformLocation(*programID_sptr, name);
1233  }
1234 
1240  GLint getAttributeLocation (const GLchar* name) const
1241  {
1242  return glGetAttribLocation(*programID_sptr, name);
1243  }
1244 
1245  //============================Uniforms Setters==========================================================
1246 
1247 
1248  //============================ Integer ==========================================================
1249 
1258  void setUniform (GLint location, GLint a, GLint b, GLint c, GLint d)
1259  {
1260  glUniform4i(location, a, b, c, d);
1261  }
1262 
1270  void setUniform (GLint location, GLint a, GLint b, GLint c)
1271  {
1272  glUniform3i(location, a, b, c);
1273  }
1274 
1281  void setUniform (GLint location, GLint a, GLint b)
1282  {
1283  glUniform2i(location, a, b);
1284  }
1285 
1291  void setUniform (GLint location, GLint a)
1292  {
1293  glUniform1i(location, a);
1294  }
1295 
1304  void setUniform (const GLchar* name, GLint a, GLint b, GLint c, GLint d)
1305  {
1306  GLint location = getUniformLocation(name);
1307  setUniform(location, a, b, c, d);
1308  }
1309 
1317  void setUniform (const GLchar* name, GLint a, GLint b, GLint c)
1318  {
1319  GLint location = getUniformLocation(name);
1320  setUniform(location, a, b, c);
1321  }
1322 
1329  void setUniform (const GLchar* name, GLint a, GLint b)
1330  {
1331  GLint location = getUniformLocation(name);
1332  setUniform(location, a, b);
1333  }
1334 
1340  void setUniform (const GLchar* name, GLint a)
1341  {
1342  GLint location = getUniformLocation(name);
1343  setUniform(location, a);
1344  }
1345 
1351  void setUniform (GLint location, const Eigen::Vector4i &vec)
1352  {
1353  glUniform4i(location, vec[0], vec[1], vec[2], vec[3]);
1354  }
1355 
1361  void setUniform (GLint location, const Eigen::Vector3i &vec)
1362  {
1363  glUniform3i(location, vec[0], vec[1], vec[2]);
1364  }
1365 
1371  void setUniform (GLint location, const Eigen::Vector2i &vec)
1372  {
1373  glUniform2i(location, vec[0], vec[1]);
1374  }
1375 
1381  void setUniform (const GLchar* name, const Eigen::Vector4i &vec)
1382  {
1383  GLint location = getUniformLocation(name);
1384  setUniform(location, vec);
1385  }
1386 
1392  void setUniform (const GLchar* name, const Eigen::Vector3i &vec)
1393  {
1394  GLint location = getUniformLocation(name);
1395  setUniform(location, vec);
1396  }
1397 
1403  void setUniform (const GLchar* name, const Eigen::Vector2i &vec)
1404  {
1405  GLint location = getUniformLocation(name);
1406  setUniform(location, vec);
1407  }
1408 
1409  //============================ Float ==========================================================
1410 
1411 
1420  void setUniform (GLint location, GLfloat a, GLfloat b, GLfloat c, GLfloat d)
1421  {
1422  glUniform4f(location, a, b, c, d);
1423  }
1424 
1432  void setUniform (GLint location, GLfloat a, GLfloat b, GLfloat c)
1433  {
1434  glUniform3f(location, a, b, c);
1435  }
1436 
1443  void setUniform (GLint location, GLfloat a, GLfloat b)
1444  {
1445  glUniform2f(location, a, b);
1446  }
1447 
1453  void setUniform (GLint location, GLfloat a)
1454  {
1455  glUniform1f(location, a);
1456  }
1457 
1466  void setUniform (const GLchar* name, GLfloat a, GLfloat b, GLfloat c, GLfloat d)
1467  {
1468  GLint location = getUniformLocation(name);
1469  setUniform(location, a, b, c, d);
1470  }
1471 
1479  void setUniform (const GLchar* name, GLfloat a, GLfloat b, GLfloat c)
1480  {
1481  GLint location = getUniformLocation(name);
1482  setUniform(location, a, b, c);
1483  }
1484 
1491  void setUniform (const GLchar* name, GLfloat a, GLfloat b)
1492  {
1493  GLint location = getUniformLocation(name);
1494  setUniform(location, a, b);
1495  }
1496 
1502  void setUniform (const GLchar* name, GLfloat a)
1503  {
1504  GLint location = getUniformLocation(name);
1505  setUniform(location, a);
1506  }
1507 
1513  void setUniform (GLint location, const Eigen::Vector4f &vec)
1514  {
1515  glUniform4f(location, vec[0], vec[1], vec[2], vec[3]);
1516  }
1517 
1523  void setUniform (GLint location, const Eigen::Vector3f &vec)
1524  {
1525  glUniform3f(location, vec[0], vec[1], vec[2]);
1526  }
1527 
1533  void setUniform (GLint location, const Eigen::Vector2f &vec)
1534  {
1535  glUniform2f(location, vec[0], vec[1]);
1536  }
1537 
1543  void setUniform (const GLchar* name, const Eigen::Vector4f &vec)
1544  {
1545  GLint location = getUniformLocation(name);
1546  setUniform(location, vec);
1547  }
1548 
1554  void setUniform (const GLchar* name, const Eigen::Vector3f &vec)
1555  {
1556  GLint location = getUniformLocation(name);
1557  setUniform(location, vec);
1558  }
1559 
1565  void setUniform (const GLchar* name, const Eigen::Vector2f &vec)
1566  {
1567  GLint location = getUniformLocation(name);
1568  setUniform(location, vec);
1569  }
1570 
1571  //============================ Double ==========================================================
1572 
1582  void setUniform (GLint location, GLdouble a, GLdouble b, GLdouble c, GLdouble d)
1583  {
1584  glUniform4f(location, (GLfloat)a, (GLfloat)b, (GLfloat)c, (GLfloat)d);
1585  }
1586 
1595  void setUniform (GLint location, GLdouble a, GLdouble b, GLdouble c)
1596  {
1597  glUniform3f(location, (GLfloat)a, (GLfloat)b, (GLfloat)c);
1598  }
1599 
1607  void setUniform (GLint location, GLdouble a, GLdouble b)
1608  {
1609  glUniform2f(location, (GLfloat)a, (GLfloat)b);
1610  }
1611 
1617  void setUniform (GLint location, GLdouble a)
1618  {
1619  glUniform1f(location, (GLfloat)a);
1620  }
1621 
1632  void setUniform (const GLchar* name, GLdouble a, GLdouble b, GLdouble c, GLdouble d)
1633  {
1634  GLint location = getUniformLocation(name);
1635  setUniform(location, a, b, c, d);
1636  }
1637 
1647  void setUniform (const GLchar* name, GLdouble a, GLdouble b, GLdouble c)
1648  {
1649  GLint location = getUniformLocation(name);
1650  setUniform(location, a, b, c);
1651  }
1652 
1661  void setUniform (const GLchar* name, GLdouble a, GLdouble b)
1662  {
1663  GLint location = getUniformLocation(name);
1664  setUniform(location, a, b);
1665  }
1671  void setUniform (const GLchar* name, GLdouble a)
1672  {
1673  GLint location = getUniformLocation(name);
1674  setUniform(location, a);
1675  }
1676 
1677 
1683  void setUniform (GLint location, const Eigen::Vector4d vec)
1684  {
1685  glUniform4f(location, (GLfloat)vec[0], (GLfloat)vec[1], (GLfloat)vec[2], (GLfloat)vec[3]);
1686  }
1687 
1693  void setUniform (GLint location, const Eigen::Vector3d vec)
1694  {
1695  glUniform3f(location, (GLfloat)vec[0], (GLfloat)vec[1], (GLfloat)vec[2]);
1696  }
1697 
1703  void setUniform (GLint location, const Eigen::Vector2d vec)
1704  {
1705  glUniform2f(location, (GLfloat)vec[0], (GLfloat)vec[1]);
1706  }
1707 
1713  void setUniform (const GLchar* name, const Eigen::Vector4d vec)
1714  {
1715  GLint location = getUniformLocation(name);
1716  setUniform(location, vec);
1717  }
1718 
1724  void setUniform (const GLchar* name, const Eigen::Vector3d vec)
1725  {
1726  GLint location = getUniformLocation(name);
1727  setUniform(location, vec);
1728  }
1729 
1735  void setUniform (const GLchar* name, const Eigen::Vector2d vec)
1736  {
1737  GLint location = getUniformLocation(name);
1738  setUniform(location, vec);
1739  }
1740 
1741 
1742  //============================ Vector from Array ==========================================================
1743 
1753  void setUniform (GLint location, const GLint* v, GLuint nvalues, GLsizei count = 1)
1754  {
1755  switch (nvalues)
1756  {
1757  case 1: glUniform1iv(location, count, v); break;
1758  case 2: glUniform2iv(location, count, v); break;
1759  case 3: glUniform3iv(location, count, v); break;
1760  case 4: glUniform4iv(location, count, v); break;
1761  }
1762  }
1773  void setUniform (GLint location, const GLfloat* v, GLuint nvalues, GLsizei count = 1)
1774  {
1775  switch (nvalues)
1776  {
1777  case 1: glUniform1fv(location, count, v); break;
1778  case 2: glUniform2fv(location, count, v); break;
1779  case 3: glUniform3fv(location, count, v); break;
1780  case 4: glUniform4fv(location, count, v); break;
1781  }
1782  }
1783 
1793  void setUniform (const GLchar* name, const GLint* v, GLuint nvalues, GLsizei count = 1)
1794  {
1795  GLint location = getUniformLocation(name);
1796  setUniform(location,v,nvalues,count);
1797  }
1798 
1809  void setUniform (const GLchar* name, const GLfloat* v, GLuint nvalues, GLsizei count = 1)
1810  {
1811  GLint location = getUniformLocation(name);
1812  setUniform(location, v, nvalues, count);
1813  }
1814 
1815 
1816  //============================ Matrix ==========================================================
1817 
1830  void setUniform (GLint location, const GLfloat* m, GLuint dim, GLboolean transpose = GL_FALSE, GLsizei count = 1)
1831  {
1832  switch(dim)
1833  {
1834  case 2: glUniformMatrix2fv(location, count, transpose, m); break;
1835  case 3: glUniformMatrix3fv(location, count, transpose, m); break;
1836  case 4: glUniformMatrix4fv(location, count, transpose, m); break;
1837  }
1838  }
1839 
1852  void setUniform (const GLchar* name, const GLfloat* m, GLuint dim, GLboolean transpose = GL_FALSE, GLsizei count = 1)
1853  {
1854  GLint location = getUniformLocation(name);
1855  setUniform(location, m, dim, transpose, count);
1856  }
1857 
1863  void setUniform (GLint location, const Eigen::Matrix4f &matrix)
1864  {
1865  glUniformMatrix4fv(location, 1, GL_FALSE, matrix.data());
1866  }
1867 
1873  void setUniform (GLint location, const Eigen::Matrix3f &matrix)
1874  {
1875  glUniformMatrix3fv(location, 1, GL_FALSE, matrix.data());
1876  }
1877 
1883  void setUniform (GLint location, const Eigen::Matrix2f &matrix)
1884  {
1885  glUniformMatrix2fv(location, 1, GL_FALSE, matrix.data());
1886  }
1887 
1893  void setUniform (const GLchar* name, const Eigen::Matrix4f &matrix)
1894  {
1895  GLint location = getUniformLocation(name);
1896  setUniform(location, matrix);
1897  }
1898 
1904  void setUniform (const GLchar* name, const Eigen::Matrix3f &matrix)
1905  {
1906  GLint location = getUniformLocation(name);
1907  setUniform(location, matrix);
1908  }
1909 
1915  void setUniform (const GLchar* name, const Eigen::Matrix2f &matrix)
1916  {
1917  GLint location = getUniformLocation(name);
1918  setUniform(location, matrix);
1919  }
1920 
1926  void setUniform (GLint location, const Eigen::Affine3f &affine_matrix)
1927  {
1928  glUniformMatrix4fv(location, 1, GL_FALSE, affine_matrix.matrix().data());
1929  }
1930 
1936  void setUniform (GLint location, const Eigen::Affine2f &affine_matrix)
1937  {
1938  glUniformMatrix3fv(location, 1, GL_FALSE, affine_matrix.matrix().data());
1939  }
1940 
1946  void setUniform (const GLchar* name, const Eigen::Affine3f &affine_matrix)
1947  {
1948  GLint location = getUniformLocation(name);
1949  setUniform(location, affine_matrix);
1950  }
1951 
1957  void setUniform (const GLchar* name, const Eigen::Affine2f &affine_matrix)
1958  {
1959  GLint location = getUniformLocation(name);
1960  setUniform(location, affine_matrix);
1961  }
1962 
1963 
1964 };
1965 
1966 }
1967 #endif
string tessellationEvaluationShaderPath
Stores the path to the tessellation evaluation shader file.
Definition: shader.hpp:59
void setUniform(const GLchar *name, const Eigen::Affine2f &affine_matrix)
Sets a uniform 3x3 float matrix value given its name in shader and eigen 2x2 affine matrix...
Definition: shader.hpp:1957
GLuint getVertexShader(void)
Returns a handle to the vertex shader.
Definition: shader.hpp:371
Shader()
Empty constructor.
Definition: shader.hpp:296
void setUniform(GLint location, const Eigen::Vector2f &vec)
Sets an uniform float 2D vector (vec2) given a location and the vector with values.
Definition: shader.hpp:1533
GLuint getGeometryShader(void)
Returns a handle to the geometry shader.
Definition: shader.hpp:382
void setFragmentShader(string &fragmentShaderCode)
Loads fragment code into shader program.
Definition: shader.hpp:979
string fragmentShaderPath
Stores the path to the fragment shader file.
Definition: shader.hpp:65
string shaderName
Stores an user mainteined identification for the shader. If the shader is created with the autoloader...
Definition: shader.hpp:50
void setUniform(GLint location, const Eigen::Matrix2f &matrix)
Sets a uniform 2x2 float matrix value given its location and eigen 2x2 matrix.
Definition: shader.hpp:1883
GLuint getFragmentShader(void)
Returns a handle to the fragment shader.
Definition: shader.hpp:360
string compute_shader_code
Compute shader code.
Definition: shader.hpp:86
void setUniform(const GLchar *name, const Eigen::Matrix3f &matrix)
Sets a uniform 3x3 float matrix value given its name in shader and eigen 3x3 matrix.
Definition: shader.hpp:1904
Shader(string name, string shader_dir)
Constructors that searches a given directory for shaders with given name.
Definition: shader.hpp:286
GLuint getTessellationEvaluationShader(void)
Returns a handle to the tessellation evaluation shader.
Definition: shader.hpp:405
GLint getAttributeLocation(const GLchar *name) const
Definition: shader.hpp:1240
void setUniform(const GLchar *name, GLfloat a, GLfloat b, GLfloat c, GLfloat d)
Sets an uniform float 4D vector (vec4) given its name in the shader and the vector values...
Definition: shader.hpp:1466
void setUniform(GLint location, GLdouble a, GLdouble b, GLdouble c, GLdouble d)
Sets an uniform float 4D vector (vec4) given a location and the vector values as Double. The double values are converted to float.
Definition: shader.hpp:1582
string geometryShaderPath
Stores the path to the geometry shader file.
Definition: shader.hpp:62
void setUniform(GLint location, GLint a, GLint b, GLint c)
Sets an uniform integer 3D vector (ivec3) given a location and the vector values. ...
Definition: shader.hpp:1270
Definition: bufferobject.hpp:34
void setUniform(const GLchar *name, const Eigen::Vector4f &vec)
Sets an uniform float 4D vector (vec4) given its name in the shader and the vector with values...
Definition: shader.hpp:1543
void initialize(void)
Calls all the functions related to the shader initialization, i.e., creates, loads the shaders from t...
Definition: shader.hpp:641
void setShaderName(string name)
Sets the shader name, very useful for debugging.
Definition: shader.hpp:329
void setGeometryShader(string &geometryShaderCode)
Loads geometry code into shader program.
Definition: shader.hpp:904
void setUniform(GLint location, const Eigen::Vector4d vec)
Sets an uniform float 4D vector (vec4) given a location and the double vector that is converted to fl...
Definition: shader.hpp:1683
void readVertexCode(void)
Reads the external file containing the vertex shader and loads it into the shader program...
Definition: shader.hpp:728
void createShaders(void)
Create the necessary shaders.
Definition: shader.hpp:172
void setUniform(GLint location, const Eigen::Vector4f &vec)
Sets an uniform float 4D vector (vec4) given a location and the vector with values.
Definition: shader.hpp:1513
void setUniform(GLint location, GLint a)
Sets an uniform integer given a location and the integer value.
Definition: shader.hpp:1291
void setUniform(GLint location, GLdouble a, GLdouble b, GLdouble c)
Sets an uniform float 3D vector (vec3) given a location and the vector values as Double. The double values are converted to float.
Definition: shader.hpp:1595
string getShaderName(void)
Returns a string with the shader name.
Definition: shader.hpp:340
void setUniform(const GLchar *name, GLint a, GLint b, GLint c, GLint d)
Sets an uniform integer 4D vector (ivec4) given its name in the shader and the vector values...
Definition: shader.hpp:1304
A Shader object represents one GLSL program.
Definition: shader.hpp:45
string tessellationControlShaderPath
Stores the path to the tessellation control shader file.
Definition: shader.hpp:56
void setUniform(GLint location, const Eigen::Matrix3f &matrix)
Sets a uniform 3x3 float matrix value given its location and eigen 3x3 matrix.
Definition: shader.hpp:1873
void setUniform(GLint location, const Eigen::Vector3i &vec)
Sets an uniform integer 3D vector (ivec3) given a location and the vector with values.
Definition: shader.hpp:1361
void setUniform(GLint location, GLfloat a, GLfloat b, GLfloat c, GLfloat d)
Sets an uniform float 4D vector (vec4) given a location and the vector values.
Definition: shader.hpp:1420
void setUniform(GLint location, const Eigen::Vector2d vec)
Sets an uniform float 2D vector (vec2) given a location and the double vector that is converted to fl...
Definition: shader.hpp:1703
void setUniform(const GLchar *name, GLint a, GLint b)
Sets an uniform integer 2D vector (ivec2) given its name in the shader and the vector values...
Definition: shader.hpp:1329
void setUniform(const GLchar *name, const Eigen::Matrix4f &matrix)
Sets a uniform 4x4 float matrix value given its name in shader and eigen 4x4 matrix.
Definition: shader.hpp:1893
void getActiveAttributes(vector< string > &attribs)
Detaches and deletes the shaders and the shader program.
Definition: shader.hpp:1205
void setUniform(const GLchar *name, const Eigen::Vector3f &vec)
Sets an uniform float 3D vector (vec3) given its name in the shader and the vector with values...
Definition: shader.hpp:1554
void setUniform(const GLchar *name, const Eigen::Vector2f &vec)
Sets an uniform float 2D vector (vec2) given its name in the shader and the vector with values...
Definition: shader.hpp:1565
void setUniform(GLint location, GLdouble a, GLdouble b)
Sets an uniform float 2D vector (vec2) given a location and the vector values as Double. The double values are converted to float.
Definition: shader.hpp:1607
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
void initializeFromStrings(string in_vertex_code, string in_fragment_code, string in_geometry_code="", string in_tessellation_evaluation_code="", string in_tessellation_control_code="")
Initializes shader directly from string, no files.
Definition: shader.hpp:589
void setUniform(const GLchar *name, const Eigen::Vector3i &vec)
Sets an uniform integer 3D vector (ivec3) given its name in the shader and the vector with values...
Definition: shader.hpp:1392
void setUniform(GLint location, GLfloat a)
Sets an uniform float given a location and the float value.
Definition: shader.hpp:1453
void setUniform(const GLchar *name, GLdouble a, GLdouble b)
Sets an uniform float 2D vector (vec2) given its name in the shader and the vector values as Double...
Definition: shader.hpp:1661
void setUniform(GLint location, GLfloat a, GLfloat b, GLfloat c)
Sets an uniform float 3D vector (vec3) given a location and the vector values.
Definition: shader.hpp:1432
void initializeTF(int size, const char **varlist, GLenum buffer_mode=GL_INTERLEAVED_ATTRIBS)
Initializes shader and prepares it to use Transform Feedback.
Definition: shader.hpp:541
void setUniform(const GLchar *name, GLint a, GLint b, GLint c)
Sets an uniform integer 3D vector (ivec3) given its name in the shader and the vector values...
Definition: shader.hpp:1317
void setVertexShader(string &vertexShaderCode)
Loads vertex code into shader program.
Definition: shader.hpp:682
void setUniform(const GLchar *name, const Eigen::Vector2d vec)
Sets an uniform float 2D vector (vec2) given its name in the shader and the double vector that is con...
Definition: shader.hpp:1735
void setUniform(GLint location, const GLfloat *v, GLuint nvalues, GLsizei count=1)
Sets a float uniform vector value given its location and an array with the values.
Definition: shader.hpp:1773
void setUniform(const GLchar *name, GLdouble a)
Sets an uniform float given its name in the shader and a double value that is converted to float...
Definition: shader.hpp:1671
Shader(string name, string vertex_shader_path, string fragment_shader_path, string geometry_shader_path="", string tessellation_evaluation_shader_path="", string tessellation_control_shader_path="")
Constructor that receives the path for each shader separately.
Definition: shader.hpp:267
void setUniform(GLint location, const Eigen::Affine3f &affine_matrix)
Sets a uniform 4x4 float matrix value given its location and eigen 3x3 affine matrix.
Definition: shader.hpp:1926
void reloadShaders(void)
Reloads all shaders by reading the files again.
Definition: shader.hpp:1126
void setUniform(GLint location, GLdouble a)
Sets an uniform float given a location and a double value that is converted to float.
Definition: shader.hpp:1617
void readComputeShaderCode(void)
Reads the external file containing the compute shader code.
Definition: shader.hpp:1096
void setUniform(const GLchar *name, const Eigen::Vector4d vec)
Sets an uniform float 4D vector (vec4) given its name in the shader and the double vector that is con...
Definition: shader.hpp:1713
void readFragmentCode(void)
Reads the external file containing the fragment shader and loads it into the shader program...
Definition: shader.hpp:1024
GLuint getTessellationControlShader(void)
Returns a handle to the tessellation control shader.
Definition: shader.hpp:393
GLuint getComputeShader(void)
Definition: shader.hpp:417
void setComputeShader(string &computeShaderCode)
Loads compute code into shader program.
Definition: shader.hpp:1052
GLuint getShaderProgram(void)
Returns the program identification handle.
Definition: shader.hpp:349
void readTessellationEvaluationCode(void)
Reads the external file containing the tessellation evaluation shader and loads it into the shader pr...
Definition: shader.hpp:875
void setUniform(GLint location, GLint a, GLint b)
Sets an uniform integer 2D vector (ivec2) given a location and the vector values. ...
Definition: shader.hpp:1281
string vertexShaderPath
Stores the path to the vertex shader file.
Definition: shader.hpp:53
string vertex_code
Vertex shader code.
Definition: shader.hpp:71
void unbind(void)
Disables the shader program.
Definition: shader.hpp:1184
void setUniform(const GLchar *name, const GLfloat *m, GLuint dim, GLboolean transpose=GL_FALSE, GLsizei count=1)
Sets a uniform float matrix value given its name in shader code.
Definition: shader.hpp:1852
void createProgramID(void)
Copy Contructor Copies the shader codes and recompiles to generate new program.
Definition: shader.hpp:155
void setUniform(GLint location, const Eigen::Vector3f &vec)
Sets an uniform float 3D vector (vec3) given a location and the vector with values.
Definition: shader.hpp:1523
string geometry_code
Geometry shader code.
Definition: shader.hpp:77
void setUniform(const GLchar *name, const GLfloat *v, GLuint nvalues, GLsizei count=1)
Sets a float uniform vector value given its name in shader and an array with the values.
Definition: shader.hpp:1809
void setUniform(const GLchar *name, GLfloat a)
Sets an uniform float given its name in the shader and the float value.
Definition: shader.hpp:1502
string fragment_code
Fragment shader code.
Definition: shader.hpp:74
void readTessellationControlCode(void)
Reads the external file containing the tessellation control shader and loads it into the shader progr...
Definition: shader.hpp:802
void setUniform(const GLchar *name, const Eigen::Vector2i &vec)
Sets an uniform integer 2D vector (ivec2) given its name in the shader and the vector with values...
Definition: shader.hpp:1403
string computeShaderPath
Stores the paths to the compute shaders files.
Definition: shader.hpp:68
void setUniform(const GLchar *name, const Eigen::Affine3f &affine_matrix)
Sets a uniform 4x4 float matrix value given its name in shader and eigen 3x3 affine matrix...
Definition: shader.hpp:1946
void setUniform(const GLchar *name, GLfloat a, GLfloat b)
Sets an uniform float 2D vector (vec4) given its name in the shader and the vector values...
Definition: shader.hpp:1491
void setUniform(const GLchar *name, GLfloat a, GLfloat b, GLfloat c)
Sets an uniform float 3D vector (vec3) given its name in the shader and the vector values...
Definition: shader.hpp:1479
void setUniform(const GLchar *name, const Eigen::Matrix2f &matrix)
Sets a uniform 2x2 float matrix value given its name in shader and eigen 2x2 matrix.
Definition: shader.hpp:1915
void setUniform(const GLchar *name, const Eigen::Vector3d vec)
Sets an uniform float 3D vector (vec3) given its name in the shader and the double vector that is con...
Definition: shader.hpp:1724
void setTessellationEvaluationShader(string &tessellationEvaluationCode)
Loads tessellation evaluation code into shader program.
Definition: shader.hpp:830
void setUniform(const GLchar *name, const Eigen::Vector4i &vec)
Sets an uniform integer 4D vector (ivec4) given its name in the shader and the vector with values...
Definition: shader.hpp:1381
void load(string name, string shader_dir="")
Loads a shader given a directory and a name. Searches for all shader extensions in directory...
Definition: shader.hpp:436
void bind(void)
Enables the shader program for usage.
Definition: shader.hpp:1176
void setUniform(GLint location, const GLint *v, GLuint nvalues, GLsizei count=1)
Sets a integer uniform vector value given its location and an array with the values.
Definition: shader.hpp:1753
void setTessellationControlShader(string &tessellationControlCode)
Loads tessellation control code into shader program.
Definition: shader.hpp:757
string tessellation_evaluation_code
Tesselation evaluation code.
Definition: shader.hpp:80
void setUniform(GLint location, const Eigen::Affine2f &affine_matrix)
Sets a uniform 3x3 float matrix value given its location and eigen 2x2 affine matrix.
Definition: shader.hpp:1936
void setUniform(GLint location, const Eigen::Vector3d vec)
Sets an uniform float 3D vector (vec3) given a location and the double vector that is converted to fl...
Definition: shader.hpp:1693
void setUniform(GLint location, const GLfloat *m, GLuint dim, GLboolean transpose=GL_FALSE, GLsizei count=1)
Sets a uniform float matrix value given its location.
Definition: shader.hpp:1830
void linkProgram(void)
Link shader program and check for link errors.
Definition: shader.hpp:508
void setUniform(GLint location, const Eigen::Matrix4f &matrix)
Sets a uniform 4x4 float matrix value given its location and eigen 4x4 matrix.
Definition: shader.hpp:1863
void setUniform(GLint location, GLfloat a, GLfloat b)
Sets an uniform float 2D vector (vec2) given a location and the vector values.
Definition: shader.hpp:1443
void setUniform(const GLchar *name, GLdouble a, GLdouble b, GLdouble c)
Sets an uniform float 3D vector (vec3) given its name in the shader and the vector values as Double...
Definition: shader.hpp:1647
void setUniform(const GLchar *name, GLint a)
Sets an uniform integer given its name in the shader and the integer value.
Definition: shader.hpp:1340
GLint getUniformLocation(const GLchar *name) const
Definition: shader.hpp:1230
string tessellation_control_code
Tesselation control code.
Definition: shader.hpp:83
void setUniform(GLint location, const Eigen::Vector4i &vec)
Sets an uniform integer 4D vector (ivec4) given a location and the vector with values.
Definition: shader.hpp:1351
void setUniform(GLint location, const Eigen::Vector2i &vec)
Sets an uniform integer 2D vector (ivec2) given a location and the vector with values.
Definition: shader.hpp:1371
void setUniform(const GLchar *name, GLdouble a, GLdouble b, GLdouble c, GLdouble d)
Sets an uniform float 4D vector (vec4) given its name in the shader and the vector values as Double...
Definition: shader.hpp:1632
void readGeometryCode(void)
Reads the external file containing the geometry shader and loads it into the shader program...
Definition: shader.hpp:950
void setUniform(const GLchar *name, const GLint *v, GLuint nvalues, GLsizei count=1)
Sets a integer uniform vector value given its name in shader and an array with the values...
Definition: shader.hpp:1793
void setUniform(GLint location, GLint a, GLint b, GLint c, GLint d)
Sets an uniform integer 4D vector (ivec4) given a location and the vector values. ...
Definition: shader.hpp:1258