-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.cpp
54 lines (45 loc) · 1.46 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
#include "shader.h"
Shader::Shader(const std::string& shaderPath, unsigned int forceShaderType) : ID_ptr(std::make_shared<ShaderID_>(shaderPath, forceShaderType)) {}
unsigned int Shader::getID()
{
return ID_ptr->ID;
}
ShaderID_::ShaderID_(const std::string& shaderPath, unsigned int forceShaderType)
{
std::string shaderCode;
std::ifstream shaderFile;
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
shaderFile.open(shaderPath);
std::stringstream shaderStream;
// read file's buffer contents into stream
shaderStream << shaderFile.rdbuf();
// close file handler
shaderFile.close();
// convert stream into string
shaderCode = shaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const char* rawShaderCode = shaderCode.c_str();
int success;
char infoLog[512];
ID = glCreateShader(forceShaderType);
glShaderSource(ID, 1, &rawShaderCode, NULL);
glCompileShader(ID);
// print compile errors if any
glGetShaderiv(ID, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(ID, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
ShaderID_::~ShaderID_()
{
glDeleteShader(ID);
}