-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.cpp
92 lines (78 loc) · 2.74 KB
/
shaders.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
92
#include"shaders.h"
//text file to string
std::string get_file_contents(const char* filename)
{
std::ifstream in(filename, std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
throw(errno);
}
//build the Shader Program from vertex and fragment shader
Shader::Shader(const char* vertexFile, const char* fragmentFile)
{
// Read vertexFile and fragmentFile and store the strings
std::string vertexCode = get_file_contents(vertexFile);
std::string fragmentCode = get_file_contents(fragmentFile);
// Convert the shader source strings into character arrays
const char* vertexSource = vertexCode.c_str();
const char* fragmentSource = fragmentCode.c_str();
// Create Vertex Shader Object and get its reference
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Attach Vertex Shader source to the Vertex Shader Object
glShaderSource(vertexShader, 1, &vertexSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(vertexShader);
int success;
char log[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, 0, log);
std::cerr << "[IN FILE]: " << vertexFile << std::endl;
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED: " << log << std::endl;
}
// Create Fragment Shader Object and get its reference
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Attach Fragment Shader source to the Fragment Shader Object
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
// Compile the Vertex Shader into machine code
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, 0, log);
std::cerr << "[IN FILE]: " << fragmentFile << std::endl;
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED: " << log << std::endl;
}
// Create Shader Program Object and get its reference
ID = glCreateProgram();
// Attach the Vertex and Fragment Shaders to the Shader Program
glAttachShader(ID, vertexShader);
glAttachShader(ID, fragmentShader);
// Wrap-up/Link all the shaders together into the Shader Program
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(ID, 512, 0, log);
fprintf(stderr, "ERROR::SHADER::PROGRAM::LINKING_FAILED: %s\n", log);
}
// Delete the now useless Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
// Activates the Shader Program
void Shader::Activate()
{
glUseProgram(ID);
}
// Deletes the Shader Program
void Shader::Delete()
{
glDeleteProgram(ID);
}