-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaderClass.cpp
More file actions
157 lines (140 loc) · 5.33 KB
/
shaderClass.cpp
File metadata and controls
157 lines (140 loc) · 5.33 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include"shaderClass.h"
// Reads a text file and outputs a string with everything in the text file
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);
}
// Constructor that build the Shader Program from 2 different shaders
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);
// Checks if Shader compiled succesfully
compileErrors(vertexShader, "VERTEX");
// 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);
// Checks if Shader compiled succesfully
compileErrors(fragmentShader, "FRAGMENT");
// 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);
// Checks if Shaders linked succesfully
compileErrors(ID, "PROGRAM");
// Delete the now useless Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
Shader::Shader(const char* vertexFile, const char* fragmentFile, const char* geometryFile)
{
// Read vertexFile and fragmentFile and store the strings
std::string vertexCode = get_file_contents(vertexFile);
std::string fragmentCode = get_file_contents(fragmentFile);
std::string geometryCode = get_file_contents(geometryFile);
// Convert the shader source strings into character arrays
const char* vertexSource = vertexCode.c_str();
const char* fragmentSource = fragmentCode.c_str();
const char* geometrySource = geometryCode.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);
// Checks if Shader compiled succesfully
compileErrors(vertexShader, "VERTEX");
// 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 Fragment Shader into machine code
glCompileShader(fragmentShader);
// Checks if Shader compiled succesfully
compileErrors(fragmentShader, "FRAGMENT");
// Create Geometry Shader Object and get its reference
GLuint geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
// Attach Geometry Shader source to the Fragment Shader Object
glShaderSource(geometryShader, 1, &geometrySource, NULL);
// Compile the Geometry Shader into machine code
glCompileShader(geometryShader);
// Checks if Shader compiled succesfully
compileErrors(geometryShader, "GEOMETRY");
// 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);
glAttachShader(ID, geometryShader);
// Wrap-up/Link all the shaders together into the Shader Program
glLinkProgram(ID);
// Checks if Shaders linked succesfully
compileErrors(ID, "PROGRAM");
// Delete the now useless Vertex and Fragment Shader objects
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteShader(geometryShader);
}
// Activates the Shader Program
void Shader::Activate()
{
glUseProgram(ID);
}
// Deletes the Shader Program
void Shader::Delete()
{
glDeleteProgram(ID);
}
// Checks if the different Shaders have compiled properly
void Shader::compileErrors(unsigned int shader, const char* type)
{
// Stores status of compilation
GLint hasCompiled;
// Character array to store error message in
char infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &hasCompiled);
if (hasCompiled == GL_FALSE)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "SHADER_COMPILATION_ERROR for:" << type << "\n" << infoLog << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &hasCompiled);
if (hasCompiled == GL_FALSE)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "SHADER_LINKING_ERROR for:" << type << "\n" << infoLog << std::endl;
}
}
}