3d5ac109a6f4da0752ba0070934d4b9b263c91d9
[tar-legacy.git] / MCDV / Shader.hpp
1 #pragma once
2 #include <string>
3 #include <fstream>
4 #include <iostream>
5
6 #include <glad\glad.h>
7 #include <GLFW\glfw3.h>
8
9 #include <glm\glm.hpp>
10 #include <glm\gtc\matrix_transform.hpp>
11 #include <glm\gtc\type_ptr.hpp>
12
13 bool USE_DEBUG = false;
14
15 //Prototype functions
16 unsigned int LoadShader(std::string path, GLint shaderType, int* load_success);
17
18 class Shader
19 {
20 public:
21 unsigned int programID;
22
23 bool compileUnsuccessful = false;
24
25 //Constructor
26 Shader(std::string vertexPath, std::string fragmentPath);
27 ~Shader();
28
29 //Set active
30 void use();
31
32 //Util functions
33 void setBool(const std::string &name, bool value) const;
34 void setInt(const std::string &name, int value) const;
35 void setFloat(const std::string &name, float value) const;
36 void setMatrix(const std::string &name, glm::mat4 matrix) const;
37 void setVec3(const std::string &name, glm::vec3 vector) const;
38
39 void setVec3(const std::string &name, float v1, float v2, float v3) const;
40
41 unsigned int getUniformLocation(const std::string &name) const;
42 };
43
44
45
46 //Constructor
47 Shader::Shader(std::string pVertexShader, std::string pFragmentShader)
48 {
49 int success = 1;
50 unsigned int vertexShader = LoadShader(pVertexShader, GL_VERTEX_SHADER, &success); //Load the vertex shader
51 unsigned int fragmentShader = LoadShader(pFragmentShader, GL_FRAGMENT_SHADER, &success); //Load the fragment shader
52
53 if (success == 0) {
54 std::cout << "ERROR::SHADER::SOURCE_UNAVAILIBLE: " << pVertexShader << " | " << pFragmentShader << "\n";
55 this->compileUnsuccessful = true;
56 return;
57 }
58
59 this->programID = glCreateProgram();
60
61 //Attach the shaders to our program
62 glAttachShader(this->programID, vertexShader);
63 glAttachShader(this->programID, fragmentShader);
64 glLinkProgram(this->programID);
65
66 char infoLog[512];
67
68 glGetProgramiv(this->programID, GL_LINK_STATUS, &success);
69 if (!success) {
70 glGetProgramInfoLog(this->programID, 512, NULL, infoLog);
71 this->compileUnsuccessful = true;
72 std::cout << infoLog << std::endl;
73 }
74
75 glDeleteShader(vertexShader);
76 glDeleteShader(fragmentShader);
77 }
78
79 unsigned int LoadShader(std::string path, GLint shaderType, int* load_success)
80 {
81 //Load the data into an std::string
82 std::ifstream shaderFile(path);
83
84 if (!shaderFile) {
85 *load_success = 0;
86 return 0;
87 }
88
89 std::string shaderString((std::istreambuf_iterator<char>(shaderFile)),
90 std::istreambuf_iterator<char>());
91
92 //Debug for now by printing the data its opened to the console window
93 if (USE_DEBUG)
94 std::cout << "Compiling shader from source: " << path << std::endl << shaderString.c_str() << std::endl << std::endl;
95 else
96 std::cout << "Compiling shader from source: " << path << std::endl;
97
98 const char* shaderSource = shaderString.c_str();
99
100 GLint shader = glCreateShader(shaderType);
101 glShaderSource(shader, 1, &shaderSource, NULL);
102 glCompileShader(shader);
103
104 int success;
105 char infoLog[512];
106 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
107
108 if (!success)
109 {
110 glGetShaderInfoLog(shader, 512, NULL, infoLog);
111 std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl;
112 }
113
114 return shader;
115 }
116
117 //Destructor
118 Shader::~Shader()
119 {
120 //Delete this shader
121 glDeleteProgram(this->programID);
122 }
123
124 void Shader::use()
125 {
126 glUseProgram(this->programID);
127 }
128
129 //Setter functions
130 void Shader::setBool(const std::string &name, bool value) const
131 {
132 glUniform1i(glGetUniformLocation(this->programID, name.c_str()), (int)value);
133 }
134
135 void Shader::setInt(const std::string &name, int value) const
136 {
137 glUniform1i(glGetUniformLocation(this->programID, name.c_str()), value);
138 }
139
140 void Shader::setFloat(const std::string &name, float value) const
141 {
142 glUniform1f(glGetUniformLocation(this->programID, name.c_str()), value);
143 }
144
145 unsigned int Shader::getUniformLocation(const std::string &name) const
146 {
147 return glGetUniformLocation(this->programID, name.c_str());
148 }
149
150 void Shader::setMatrix(const std::string &name, glm::mat4 matrix) const
151 {
152 glUniformMatrix4fv(glGetUniformLocation(this->programID, name.c_str()),
153 1,
154 GL_FALSE,
155 glm::value_ptr(matrix));
156 }
157
158 void Shader::setVec3(const std::string &name, glm::vec3 vector) const
159 {
160 glUniform3fv(glGetUniformLocation(this->programID, name.c_str()),
161 1,
162 glm::value_ptr(vector));
163 }
164
165 void Shader::setVec3(const std::string &name, float v1, float v2, float v3) const
166 {
167 glUniform3f(glGetUniformLocation(this->programID, name.c_str()), v1, v2, v3);
168 }