This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
66 lines (52 loc) · 1.61 KB
/
shader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "shader.hpp"
#include <glad/glad.h>
#include <fstream>
#include <map>
Shader::Shader() {
programId = glCreateProgram();
}
Shader::~Shader() {
glDeleteProgram(programId);
}
void Shader::attachShader(const char* fileName, unsigned int shaderType) {
unsigned int shaderId = glCreateShader(shaderType);
std::string sourceCode = getShaderFromFile(fileName);
const char* chSourceCode = &sourceCode[0];
glShaderSource(shaderId, 1, &chSourceCode, NULL);
glCompileShader(shaderId);
glAttachShader(programId, shaderId);
glDeleteShader(shaderId);
}
void Shader::link() const {
glLinkProgram(programId);
}
void Shader::use() const {
glUseProgram(programId);
}
void Shader::addUniform(const std::string& varName) {
uniformData[varName] = glGetUniformLocation(programId, varName.c_str());
}
void Shader::setVec3(const std::string& varName, const glm::vec3& value) {
glUniform3f(uniformData[varName], value.x, value.y, value.z);
}
void Shader::setVec4(const std::string& varName, const glm::vec4& value) {
glUniform4f(uniformData[varName], value.r, value.g, value.b, value.a);
}
void Shader::setMat3(const std::string& varName, const glm::mat3* value) {
glUniformMatrix3fv(uniformData[varName], 1, false, (GLfloat*)value);
}
void Shader::setMat4(const std::string& varName, const glm::mat4* value) {
glUniformMatrix4fv(uniformData[varName], 1, false, (GLfloat*)value);
}
std::string Shader::getShaderFromFile(const char* fileName) {
std::ifstream file(fileName);
std::string data;
if (file.is_open()) {
char readChar;
while ((readChar = file.get())!=EOF) {
data += readChar;
}
file.close();
}
return data;
}