-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
85 lines (77 loc) · 1.64 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
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <GL/glew.h>
#include "Shader.hpp"
Shader::Shader(
const std::string &name,
const std::string &filename,
ShaderType type
) : name(name), type(type)
{
switch (type)
{
case VERTEX :
{
shaderId = glCreateShader(GL_VERTEX_SHADER_ARB);
break;
}
case FRAGMENT :
{
shaderId = glCreateShader(GL_FRAGMENT_SHADER_ARB);
break;
}
case GEOMETRY :
{
shaderId = glCreateShader(GL_GEOMETRY_SHADER);
break;
}
case COMPUTE:
{
shaderId = glCreateShader(GL_COMPUTE_SHADER);
break;
}
}
load(filename);
}
GLuint Shader::load(const std::string &filename)
{
std::ifstream shaderSourceFileHandle(filename.c_str());
if (!shaderSourceFileHandle.is_open())
{
std::cerr << "File not found " << filename.c_str() << "\n";
exit(EXIT_FAILURE);
}
std::string source = std::string((std::istreambuf_iterator<char>(shaderSourceFileHandle)), std::istreambuf_iterator<char>());
shaderSourceFileHandle.close();
const char *data = source.c_str();
glShaderSource(shaderId, 1, &data, NULL);
glCompileShader(shaderId);
GLint success = 0;
GLchar infoLog[1024];
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shaderId, 1024, NULL, infoLog);
fprintf(stderr, "Error compiling shader type %d: '%s'\n", type, infoLog);
exit(1);
}
return shaderId;
}
ShaderType const Shader::getType() const
{
return type;
}
GLuint Shader::getShaderId() const
{
return shaderId;
}
void Shader::setName(std::string &name)
{
Shader::name = name;
}
const std::string &Shader::getName() const
{
return name;
}