-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
91 lines (80 loc) · 2.57 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <GL/glew.h>
#include "Shader.h"
#include <fstream>
#include <sstream>
#include <string>
#include <exception>
#include <iostream>
extern const char* vsSource;
extern const char* fsSource;
Shader::Shader(const char* vsPath, const char* fsPath) {
/*std::stringstream vsCode;
std::stringstream fsCode;
std::ifstream vsFile;
std::ifstream fsFile;
vsFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fsFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vsFile.open(vsPath);
vsCode << vsFile.rdbuf();
vsFile.close();
fsFile.open(fsPath);
fsCode << fsFile.rdbuf();
fsFile.close();
std::string vsString = vsCode.str(), fsString = fsCode.str();
const char* vsSource = vsString.c_str(), * fsSource = fsString.c_str();*/
GLuint vs, fs;
GLint success;
char infoLog[512];
vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vsSource, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vs, 512, NULL, infoLog);
std::cout << infoLog;
throw std::exception(infoLog);
}
fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fsSource, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fs, 512, NULL, infoLog);
throw std::exception(infoLog);
}
this->program = glCreateProgram();
glAttachShader(this->program, vs);
glAttachShader(this->program, fs);
glLinkProgram(this->program);
glGetProgramiv(this->program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->program, 512, NULL, infoLog);
throw std::exception(infoLog);
}
glDetachShader(this->program, vs);
glDetachShader(this->program, fs);
glDeleteShader(vs);
glDeleteShader(fs);
}
void Shader::use() const {
glUseProgram(this->program);
}
void Shader::setVec4(const char* name, int count, const float* value) const {
glUniform4fv(glGetUniformLocation(this->program, name), count, value);
}
void Shader::setFloat(const char* name, float value) const {
glUniform1f(glGetUniformLocation(this->program, name), value);
}
void Shader::setVec3(const char* name, const float* value) const {
glUniform3f(glGetUniformLocation(this->program, name), value[0], value[1], value[2]);
}
void Shader::setMat4(const char* name, int count, const float* value) const {
glUniformMatrix4fv(glGetUniformLocation(this->program, name), count, GL_FALSE, value);
}
void Shader::setInt(const char* name, int value) const {
glUniform1i(glGetUniformLocation(this->program, name), value);
}
Shader::~Shader() {
glUseProgram(0);
glDeleteProgram(this->program);
}