-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
134 lines (82 loc) · 3.17 KB
/
Copy pathShaderProgram.cpp
File metadata and controls
134 lines (82 loc) · 3.17 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "ShaderProgram.hpp"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <glad/glad.h>
ShaderProgram::ShaderProgram(const std::string& vertexPath, const std::string& fragmentPath){
GLuint vertex_shader = this->compileShader(GL_VERTEX_SHADER, vertexPath);
GLuint fragment_shader = this->compileShader(GL_FRAGMENT_SHADER, fragmentPath);
int program = glCreateProgram();
if(program == 0){
std::cout << "couldnt create the shader program" << std::endl;
}
glAttachShader(program,vertex_shader);
glAttachShader(program,fragment_shader);
glLinkProgram(program);
//Program link debug
GLint linkSuccess;
glGetProgramiv(program, GL_LINK_STATUS, &linkSuccess);
if (!linkSuccess) {
GLint logLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
std::vector<GLchar> infoLog(logLength);
glGetProgramInfoLog(program, logLength, nullptr, infoLog.data());
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog.data() << std::endl;
}
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
this->programID = program;
};
GLuint ShaderProgram::compileShader(GLenum type, const std::string& source){
GLuint shader = glCreateShader(type);
std::string shaderCodeString = this->readFile(source);
const GLchar* shaderCode = shaderCodeString.c_str();
glShaderSource(shader, 1, &shaderCode, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(success){
return shader;
// Print compilation errors
} else {
GLint logLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
std::vector<GLchar> infoLog(logLength);
glGetShaderInfoLog(shader, logLength, nullptr, infoLog.data());
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n"
<< "Path: " << source << "\n"
<< "Log: " << infoLog.data() << std::endl;
} else {
std::cout << "failed to compile shader from path: " << source << " (No log available)" << std::endl;
}
}
return 0;
};
std::string ShaderProgram::readFile(const std::string& filePath){
std::ifstream ShaderFile;
ShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
ShaderFile.open(filePath);
std::stringstream ShaderStream;
ShaderStream << ShaderFile.rdbuf();
ShaderFile.close();
return ShaderStream.str();
}
catch (std::ifstream::failure& e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
return "";
}
};
void ShaderProgram::use() const{
glUseProgram(this -> programID);
};
ShaderProgram::~ShaderProgram() {
if (programID != 0) {
glDeleteProgram(programID);
}
}