sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
shader.h
1#ifndef __SHADER_H__
2#define __SHADER_H__
3
4#include "app.h"
5
6namespace sgltk {
7
12class Shader {
13 EXPORT static int counter;
14
15 //The id of the currently bound shader object
16 EXPORT static int bound;
17
18 //The id of the shader object
19 int id;
20
21 bool modify;
22 bool linked;
23
24 std::vector<GLuint> attached;
25
26 std::map<std::string, GLenum> shader_path_map;
27
28 std::map<std::string, GLenum> shader_string_map;
29
30 static std::vector<std::string> paths;
31public:
44 EXPORT static void add_path(std::string path);
45
49 GLuint program;
50
51 EXPORT Shader();
52 EXPORT ~Shader();
53
60 EXPORT bool attach_file(const std::string& filename, GLenum type);
67 EXPORT bool attach_string(const std::string& shader_string, GLenum type);
77 EXPORT void set_transform_feedback_variables(std::vector<std::string>& variables, GLenum buffer_mode);
81 EXPORT void recompile();
86 EXPORT bool link();
90 EXPORT void bind();
94 EXPORT void unbind();
100 EXPORT int get_attribute_location(const std::string& name);
106 EXPORT int get_uniform_location(const std::string& name);
112 template <typename T>
113 void set_uniform(int location, T v0);
119 template <typename T>
120 void set_uniform(const std::string& name, T v0);
127 template <typename T>
128 void set_uniform(int location, T v0, T v1);
135 template <typename T>
136 void set_uniform(const std::string& name, T v0, T v1);
144 template <typename T>
145 void set_uniform(int location, T v0, T v1, T v2);
153 template <typename T>
154 void set_uniform(const std::string& name, T v0, T v1, T v2);
163 template <typename T>
164 void set_uniform(int location, T v0, T v1, T v2, T v3);
173 template <typename T>
174 void set_uniform(const std::string& name, T v0, T v1, T v2, T v3);
182 template <typename T>
183 void set_uniform(int location, unsigned int count,
184 unsigned int elements, const T *value);
192 template <typename T>
193 void set_uniform(const std::string& name, unsigned int count,
194 unsigned int elements, const T *value);
204 template <typename T>
205 void set_uniform(int location,
206 unsigned int count,
207 unsigned int columns,
208 unsigned int rows,
209 bool transpose,
210 const T *value);
220 template <typename T>
221 void set_uniform(const std::string& name,
222 unsigned int count,
223 unsigned int columns,
224 unsigned int rows,
225 bool transpose,
226 const T *value);
232 EXPORT void set_uniform(int location, const glm::vec2& value);
238 EXPORT void set_uniform(int location, const glm::dvec2& value);
244 EXPORT void set_uniform(int location, const glm::vec3& value);
250 EXPORT void set_uniform(int location, const glm::dvec3& value);
256 EXPORT void set_uniform(int location, const glm::vec4& value);
262 EXPORT void set_uniform(int location, const glm::dvec4& value);
268 EXPORT void set_uniform(const std::string& name,
269 const glm::vec2& value);
275 EXPORT void set_uniform(const std::string& name,
276 const glm::dvec2& value);
282 EXPORT void set_uniform(const std::string& name,
283 const glm::vec3& value);
289 EXPORT void set_uniform(const std::string& name,
290 const glm::dvec3& value);
296 EXPORT void set_uniform(const std::string& name,
297 const glm::vec4& value);
303 EXPORT void set_uniform(const std::string& name,
304 const glm::dvec4& value);
311 EXPORT void set_uniform(int location,
312 bool transpose,
313 const glm::mat2& value);
320 EXPORT void set_uniform(int location,
321 bool transpose,
322 const glm::dmat2& value);
329 EXPORT void set_uniform(int location,
330 bool transpose,
331 const glm::mat3& value);
338 EXPORT void set_uniform(int location,
339 bool transpose,
340 const glm::dmat3& value);
347 EXPORT void set_uniform(int location,
348 bool transpose,
349 const glm::mat4& value);
356 EXPORT void set_uniform(int location,
357 bool transpose,
358 const glm::dmat4& value);
365 EXPORT void set_uniform(const std::string& name,
366 bool transpose,
367 const glm::mat2& value);
374 EXPORT void set_uniform(const std::string& name,
375 bool transpose,
376 const glm::dmat2& value);
383 EXPORT void set_uniform(const std::string& name,
384 bool transpose,
385 const glm::mat3& value);
392 EXPORT void set_uniform(const std::string& name,
393 bool transpose,
394 const glm::dmat3& value);
401 EXPORT void set_uniform(const std::string& name,
402 bool transpose,
403 const glm::mat4& value);
410 EXPORT void set_uniform(const std::string& name,
411 bool transpose,
412 const glm::dmat4& value);
418 EXPORT void set_uniform(int location,
419 const std::vector<glm::vec2>& value);
425 EXPORT void set_uniform(int location,
426 const std::vector<glm::dvec2>& value);
432 EXPORT void set_uniform(int location,
433 const std::vector<glm::vec3>& value);
439 EXPORT void set_uniform(int location,
440 const std::vector<glm::dvec3>& value);
446 EXPORT void set_uniform(int location,
447 const std::vector<glm::vec4>& value);
453 EXPORT void set_uniform(int location,
454 const std::vector<glm::dvec4>& value);
460 EXPORT void set_uniform(const std::string& name,
461 const std::vector<glm::vec2>& value);
467 EXPORT void set_uniform(const std::string& name,
468 const std::vector<glm::dvec2>& value);
474 EXPORT void set_uniform(const std::string& name,
475 const std::vector<glm::vec3>& value);
481 EXPORT void set_uniform(const std::string& name,
482 const std::vector<glm::dvec3>& value);
488 EXPORT void set_uniform(const std::string& name,
489 const std::vector<glm::vec4>& value);
495 EXPORT void set_uniform(const std::string& name,
496 const std::vector<glm::dvec4>& value);
503 EXPORT void set_uniform(int location,
504 bool transpose,
505 const std::vector<glm::mat2>& value);
512 EXPORT void set_uniform(int location,
513 bool transpose,
514 const std::vector<glm::dmat2>& value);
521 EXPORT void set_uniform(int location,
522 bool transpose,
523 const std::vector<glm::mat3>& value);
530 EXPORT void set_uniform(int location,
531 bool transpose,
532 const std::vector<glm::dmat3>& value);
539 EXPORT void set_uniform(int location,
540 bool transpose,
541 const std::vector<glm::mat4>& value);
548 EXPORT void set_uniform(int location,
549 bool transpose,
550 const std::vector<glm::dmat4>& value);
557 EXPORT void set_uniform(const std::string& name,
558 bool transpose,
559 const std::vector<glm::mat2>& value);
566 EXPORT void set_uniform(const std::string& name,
567 bool transpose,
568 const std::vector<glm::dmat2>& value);
575 EXPORT void set_uniform(const std::string& name,
576 bool transpose,
577 const std::vector<glm::mat3>& value);
584 EXPORT void set_uniform(const std::string& name,
585 bool transpose,
586 const std::vector<glm::dmat3>& value);
593 EXPORT void set_uniform(const std::string& name,
594 bool transpose,
595 const std::vector<glm::mat4>& value);
602 EXPORT void set_uniform(const std::string& name,
603 bool transpose,
604 const std::vector<glm::dmat4>& value);
605};
606
607template <>
608inline void Shader::set_uniform<int>(int location, int v0) {
609 bind();
610 glUniform1i(location, v0);
611}
612
613template <>
614inline void Shader::set_uniform<unsigned int>(int location, unsigned int v0) {
615 bind();
616 glUniform1ui(location, v0);
617}
618
619template <>
620inline void Shader::set_uniform<float>(int location, float v0) {
621 bind();
622 glUniform1f(location, v0);
623}
624
625template <>
626inline void Shader::set_uniform<double>(int location, double v0) {
627 bind();
628 glUniform1d(location, v0);
629}
630
631template <>
632inline void Shader::set_uniform<int>(const std::string& name, int v0) {
633 int loc = get_uniform_location(name);
634 set_uniform(loc, v0);
635}
636
637template <>
638inline void Shader::set_uniform<unsigned int>(const std::string& name, unsigned int v0) {
639 int loc = get_uniform_location(name);
640 set_uniform(loc, v0);
641}
642
643template <>
644inline void Shader::set_uniform<float>(const std::string& name, float v0) {
645 int loc = get_uniform_location(name);
646 set_uniform(loc, v0);
647}
648
649template <>
650inline void Shader::set_uniform<double>(const std::string& name, double v0) {
651 int loc = get_uniform_location(name);
652 set_uniform(loc, v0);
653}
654
655template <>
656inline void Shader::set_uniform<int>(int location, int v0, int v1) {
657 bind();
658 glUniform2i(location, v0, v1);
659}
660
661template <>
662inline void Shader::set_uniform<unsigned int>(int location, unsigned int v0, unsigned int v1) {
663 bind();
664 glUniform2ui(location, v0, v1);
665}
666
667template <>
668inline void Shader::set_uniform<float>(int location, float v0, float v1) {
669 bind();
670 glUniform2f(location, v0, v1);
671}
672
673template <>
674inline void Shader::set_uniform<double>(int location, double v0, double v1) {
675 bind();
676 glUniform2d(location, v0, v1);
677}
678
679template <>
680inline void Shader::set_uniform<int>(const std::string& name, int v0, int v1) {
681 int loc = get_uniform_location(name);
682 set_uniform(loc, v0, v1);
683}
684
685template <>
686inline void Shader::set_uniform<unsigned int>(const std::string& name, unsigned int v0, unsigned int v1) {
687 int loc = get_uniform_location(name);
688 set_uniform(loc, v0, v1);
689}
690
691template <>
692inline void Shader::set_uniform<float>(const std::string& name, float v0, float v1) {
693 int loc = get_uniform_location(name);
694 set_uniform(loc, v0, v1);
695}
696
697template <>
698inline void Shader::set_uniform<double>(const std::string& name, double v0, double v1) {
699 int loc = get_uniform_location(name);
700 set_uniform(loc, v0, v1);
701}
702
703template <>
704inline void Shader::set_uniform<int>(int location, int v0, int v1, int v2) {
705 bind();
706 glUniform3i(location, v0, v1, v2);
707}
708
709template <>
710inline void Shader::set_uniform<unsigned int>(int location, unsigned int v0, unsigned int v1, unsigned int v2) {
711 bind();
712 glUniform3ui(location, v0, v1, v2);
713}
714
715template <>
716inline void Shader::set_uniform<float>(int location, float v0, float v1, float v2) {
717 bind();
718 glUniform3f(location, v0, v1, v2);
719}
720
721template <>
722inline void Shader::set_uniform<double>(int location, double v0, double v1, double v2) {
723 bind();
724 glUniform3d(location, v0, v1, v2);
725}
726
727template <>
728inline void Shader::set_uniform<int>(const std::string& name, int v0, int v1, int v2) {
729 int loc = get_uniform_location(name);
730 set_uniform(loc, v0, v1, v2);
731}
732
733template <>
734inline void Shader::set_uniform<unsigned int>(const std::string& name, unsigned int v0, unsigned int v1, unsigned int v2) {
735 int loc = get_uniform_location(name);
736 set_uniform(loc, v0, v1, v2);
737}
738
739template <>
740inline void Shader::set_uniform<float>(const std::string& name, float v0, float v1, float v2) {
741 int loc = get_uniform_location(name);
742 set_uniform(loc, v0, v1, v2);
743}
744
745template <>
746inline void Shader::set_uniform<double>(const std::string& name, double v0, double v1, double v2) {
747 int loc = get_uniform_location(name);
748 set_uniform(loc, v0, v1, v2);
749}
750
751template <>
752inline void Shader::set_uniform<int>(int location, int v0, int v1, int v2, int v3) {
753 bind();
754 glUniform4i(location, v0, v1, v2, v3);
755}
756
757template <>
758inline void Shader::set_uniform<unsigned int>(int location, unsigned int v0, unsigned int v1, unsigned int v2, unsigned int v3) {
759 bind();
760 glUniform4ui(location, v0, v1, v2, v3);
761}
762
763template <>
764inline void Shader::set_uniform<float>(int location, float v0, float v1, float v2, float v3) {
765 bind();
766 glUniform4f(location, v0, v1, v2, v3);
767}
768
769template <>
770inline void Shader::set_uniform<double>(int location, double v0, double v1, double v2, double v3) {
771 bind();
772 glUniform4d(location, v0, v1, v2, v3);
773}
774
775template <>
776inline void Shader::set_uniform<int>(const std::string& name, int v0, int v1, int v2, int v3) {
777 int loc = get_uniform_location(name);
778 set_uniform(loc, v0, v1, v2, v3);
779}
780
781template <>
782inline void Shader::set_uniform<unsigned int>(const std::string& name, unsigned int v0, unsigned int v1, unsigned int v2, unsigned int v3) {
783 int loc = get_uniform_location(name);
784 set_uniform(loc, v0, v1, v2, v3);
785}
786
787template <>
788inline void Shader::set_uniform<float>(const std::string& name, float v0, float v1, float v2, float v3) {
789 int loc = get_uniform_location(name);
790 set_uniform(loc, v0, v1, v2, v3);
791}
792
793template <>
794inline void Shader::set_uniform<double>(const std::string& name, double v0, double v1, double v2, double v3) {
795 int loc = get_uniform_location(name);
796 set_uniform(loc, v0, v1, v2, v3);
797}
798
799template <>
800inline void Shader::set_uniform<int>(int location, unsigned int count, unsigned int elements, const int *value) {
801 if(location < 0)
802 return;
803
804 bind();
805
806 switch(elements) {
807 case 1:
808 glUniform1iv(location, count, value);
809 break;
810 case 2:
811 glUniform2iv(location, count, value);
812 break;
813 case 3:
814 glUniform3iv(location, count, value);
815 break;
816 case 4:
817 glUniform4iv(location, count, value);
818 break;
819 default:
820 std::string error = "Wrong number of elements given to"
821 "the set_uniform function";
822 App::error_string.push_back(error);
823 throw std::runtime_error(error);
824 break;
825 }
826}
827
828template <>
829inline void Shader::set_uniform<unsigned int>(int location, unsigned int count, unsigned int elements, const unsigned int *value) {
830 if(location < 0)
831 return;
832
833 bind();
834
835 switch(elements) {
836 case 1:
837 glUniform1uiv(location, count, value);
838 break;
839 case 2:
840 glUniform2uiv(location, count, value);
841 break;
842 case 3:
843 glUniform3uiv(location, count, value);
844 break;
845 case 4:
846 glUniform4uiv(location, count, value);
847 break;
848 default:
849 std::string error = "Wrong number of elements given to"
850 "the set_uniform function";
851 App::error_string.push_back(error);
852 throw std::runtime_error(error);
853 break;
854 }
855}
856
857template <>
858inline void Shader::set_uniform<float>(int location, unsigned int count, unsigned int elements, const float *value) {
859 if(location < 0)
860 return;
861
862 bind();
863
864 switch(elements) {
865 case 1:
866 glUniform1fv(location, count, value);
867 break;
868 case 2:
869 glUniform2fv(location, count, value);
870 break;
871 case 3:
872 glUniform3fv(location, count, value);
873 break;
874 case 4:
875 glUniform4fv(location, count, value);
876 break;
877 default:
878 std::string error = "Wrong number of elements given to"
879 "the set_uniform function";
880 App::error_string.push_back(error);
881 throw std::runtime_error(error);
882 break;
883 }
884}
885
886template <>
887inline void Shader::set_uniform<double>(int location, unsigned int count, unsigned int elements, const double *value) {
888 if(location < 0)
889 return;
890
891 bind();
892
893 switch(elements) {
894 case 1:
895 glUniform1dv(location, count, value);
896 break;
897 case 2:
898 glUniform2dv(location, count, value);
899 break;
900 case 3:
901 glUniform3dv(location, count, value);
902 break;
903 case 4:
904 glUniform4dv(location, count, value);
905 break;
906 default:
907 std::string error = "Wrong number of elements given to"
908 "the set_uniform function";
909 App::error_string.push_back(error);
910 throw std::runtime_error(error);
911 break;
912 }
913}
914
915template <>
916inline void Shader::set_uniform<int>(const std::string& name, unsigned int count, unsigned int elements, const int *value) {
917
918 int loc = get_uniform_location(name);
919 set_uniform(loc, count, elements, value);
920}
921
922template <>
923inline void Shader::set_uniform<unsigned int>(const std::string& name, unsigned int count, unsigned int elements, const unsigned int *value) {
924
925 int loc = get_uniform_location(name);
926 set_uniform(loc, count, elements, value);
927}
928
929template <>
930inline void Shader::set_uniform<float>(const std::string& name, unsigned int count, unsigned int elements, const float *value) {
931
932 int loc = get_uniform_location(name);
933 set_uniform(loc, count, elements, value);
934}
935
936template <>
937inline void Shader::set_uniform<double>(const std::string& name, unsigned int count, unsigned int elements, const double *value) {
938
939 int loc = get_uniform_location(name);
940 set_uniform(loc, count, elements, value);
941}
942
943template <>
944inline void Shader::set_uniform<float>(int location, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const float *value) {
945 if(location < 0)
946 return;
947 if(columns < 2 || rows < 2 || columns > 4 || rows > 4)
948 return;
949
950 bind();
951
952 if(columns == rows) {
953 switch(rows) {
954 case 2:
955 glUniformMatrix2fv(location,
956 count,
957 transpose,
958 value);
959 break;
960 case 3:
961 glUniformMatrix3fv(location,
962 count,
963 transpose,
964 value);
965 break;
966 case 4:
967 glUniformMatrix4fv(location,
968 count,
969 transpose,
970 value);
971 break;
972 default:
973 std::string error = "Wrong number of elements given to"
974 "the set_uniform function";
975 App::error_string.push_back(error);
976 throw std::runtime_error(error);
977 break;
978 }
979 } else {
980 if(columns == 2 && rows == 3) {
981 glUniformMatrix2x3fv(location, count, transpose, value);
982 } else if(columns == 3 && rows == 2) {
983 glUniformMatrix3x2fv(location, count, transpose, value);
984 } else if(columns == 2 && rows == 4) {
985 glUniformMatrix2x4fv(location, count, transpose, value);
986 } else if(columns == 4 && rows == 2) {
987 glUniformMatrix4x2fv(location, count, transpose, value);
988 } else if(columns == 3 && rows == 4) {
989 glUniformMatrix3x4fv(location, count, transpose, value);
990 } else if(columns == 4 && rows == 3) {
991 glUniformMatrix4x3fv(location, count, transpose, value);
992 } else {
993 std::string error = "Wrong number of elements given to"
994 "the set_uniform function";
995 App::error_string.push_back(error);
996 throw std::runtime_error(error);
997 }
998 }
999}
1000
1001template <>
1002inline void Shader::set_uniform<double>(int location, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const double *value) {
1003 if(location < 0)
1004 return;
1005 if(columns < 2 || rows < 2 || columns > 4 || rows > 4)
1006 return;
1007
1008 bind();
1009
1010 if(columns == rows) {
1011 switch(rows) {
1012 case 2:
1013 glUniformMatrix2dv(location,
1014 count,
1015 transpose,
1016 value);
1017 break;
1018 case 3:
1019 glUniformMatrix3dv(location,
1020 count,
1021 transpose,
1022 value);
1023 break;
1024 case 4:
1025 glUniformMatrix4dv(location,
1026 count,
1027 transpose,
1028 value);
1029 break;
1030 default:
1031 std::string error = "Wrong number of elements given to"
1032 "the set_uniform function";
1033 App::error_string.push_back(error);
1034 throw std::runtime_error(error);
1035 break;
1036
1037 }
1038 } else {
1039 if(columns == 2 && rows == 3) {
1040 glUniformMatrix2x3dv(location, count, transpose, value);
1041 } else if(columns == 3 && rows == 2) {
1042 glUniformMatrix3x2dv(location, count, transpose, value);
1043 } else if(columns == 2 && rows == 4) {
1044 glUniformMatrix2x4dv(location, count, transpose, value);
1045 } else if(columns == 4 && rows == 2) {
1046 glUniformMatrix4x2dv(location, count, transpose, value);
1047 } else if(columns == 3 && rows == 4) {
1048 glUniformMatrix3x4dv(location, count, transpose, value);
1049 } else if(columns == 4 && rows == 3) {
1050 glUniformMatrix4x3dv(location, count, transpose, value);
1051 } else {
1052 std::string error = "Wrong number of elements given to"
1053 "the set_uniform function";
1054 App::error_string.push_back(error);
1055 throw std::runtime_error(error);
1056 }
1057 }
1058}
1059
1060template <>
1061inline void Shader::set_uniform<float>(const std::string& name, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const float *value) {
1062
1063 int loc = get_uniform_location(name);
1064 set_uniform(loc, count, columns, rows, transpose, value);
1065}
1066
1067template <>
1068inline void Shader::set_uniform<double>(const std::string& name, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const double *value) {
1069
1070 int loc = get_uniform_location(name);
1071 set_uniform(loc, count, columns, rows, transpose, value);
1072}
1073
1074}
1075
1076#endif
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
void set_uniform(int location, T v0, T v1, T v2)
Sets the uniform.
bool link()
Links the attached shaders.
Definition shader.cpp:160
void set_uniform(int location, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const T *value)
Sets the uniform.
void unbind()
Unbinds the shader and restores the previously bound shader.
Definition shader.cpp:188
void set_uniform(int location, unsigned int count, unsigned int elements, const T *value)
Sets the uniform.
bool attach_file(const std::string &filename, GLenum type)
Compiles and attaches shader files.
Definition shader.cpp:40
bool attach_string(const std::string &shader_string, GLenum type)
Compiles and attaches shader strings.
Definition shader.cpp:103
void set_uniform(const std::string &name, T v0, T v1, T v2)
Sets the uniform.
void recompile()
Reads, compiles and links all associated shaders again.
Definition shader.cpp:145
int get_attribute_location(const std::string &name)
Returns the location of an attribute variable.
Definition shader.cpp:195
void set_transform_feedback_variables(std::vector< std::string > &variables, GLenum buffer_mode)
Sets the variable to record in transform feedback buffers.
Definition shader.cpp:133
void set_uniform(const std::string &name, T v0, T v1, T v2, T v3)
Sets the uniform.
bool transform_feedback
True if the shader records values into transform feedback buffer(s)
Definition shader.h:36
void set_uniform(int location, T v0)
Sets the uniform.
void set_uniform(int location, T v0, T v1)
Sets the uniform.
void bind()
Binds the shader and stores the previously bound shader.
Definition shader.cpp:180
void set_uniform(const std::string &name, unsigned int count, unsigned int elements, const T *value)
Sets the uniform.
GLuint program
Shader name.
Definition shader.h:49
void set_uniform(const std::string &name, T v0, T v1)
Sets the uniform.
void set_uniform(const std::string &name, unsigned int count, unsigned int columns, unsigned int rows, bool transpose, const T *value)
Sets the uniform.
void set_uniform(int location, T v0, T v1, T v2, T v3)
Sets the uniform.
void set_uniform(const std::string &name, T v0)
Sets the uniform.
int get_uniform_location(const std::string &name)
Returns the location of a uniform variable.
Definition shader.cpp:199
static void add_path(std::string path)
Adds a path to the list of paths to be searched ▸ for image files.
Definition shader.cpp:9