-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
75 lines (59 loc) · 1.71 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
#include "Shader.h"
#include <vector>
Shader::Shader(Shader::Type t) : m_sourceCode(""), m_filepath(""), m_type(t)
{
}
Shader::Shader(Shader::Type t, const std::string &shaderPath) : Shader(t)
{
LoadFromFile(shaderPath);
}
bool Shader::LoadFromFile(const std::string& filepath)
{
this->m_filepath = filepath;
std::ifstream f;
f.open(filepath);
if (!f.is_open())
{
std::cerr << "Could not open shader file '" << filepath << "'. Does it exist / is reachable?" << std::endl;
return false;
}
std::stringstream ss;
ss << f.rdbuf();
m_sourceCode = ss.str();
m_idGL = glCreateShader(GLint(m_type));
const GLchar *source = (const GLchar*)(m_sourceCode.c_str());
GLint size = m_sourceCode.length();
glShaderSource(m_idGL, 1, &source, &size);
glCompileShader(m_idGL);
GLint ok;
glGetShaderiv(m_idGL, GL_COMPILE_STATUS, &ok);
if (!ok)
{
GLint maxLength = 0;
glGetShaderiv(m_idGL, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> v(maxLength);
glGetShaderInfoLog(m_idGL, maxLength, &maxLength, &v[0]);
std::string errorStr(v.begin(), v.end());
std::cerr << "Failed to compile shader: '" << filepath << "': " << errorStr << std::endl;
glDeleteShader(m_idGL);
return false;
}
return true;
}
const std::string& Shader::GetSourceCode() const
{
return m_sourceCode;
}
const std::string& Shader::GetFilepath() const
{
return m_filepath;
}
Shader::Type Shader::GetType() const
{
return m_type;
}
const std::string Shader::ToString() const
{
if (m_type == Type::Vertex) return "Vertex Shader: '" + m_filepath + "'";
return "Fragment Shader: '" + m_filepath + "'";
}