-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
141 lines (110 loc) · 3.65 KB
/
Copy pathshader.cpp
File metadata and controls
141 lines (110 loc) · 3.65 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
#include "shader.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <exception>
// Read text in file helper function
std::string readFile(const char* filePath)
{
std::string content;
std::ifstream fileStream(filePath, std::ios::in);
if (!fileStream.is_open()) {
std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
return "";
}
std::string line = "";
while (!fileStream.eof()) {
getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
GLuint Shader::LoadShader(const char* vertex_path, const char* fragment_path)
{
GLuint vertShader, fragShader;
// Read shaders
std::string vertShaderStr = readFile(vertex_path);
std::string fragShaderStr = readFile(fragment_path);
GLint result = GL_FALSE;
int logLength;
vertShader = BuildShader(GL_VERTEX_SHADER, vertShaderStr);
fragShader = BuildShader(GL_FRAGMENT_SHADER, fragShaderStr);
std::cout << "Linking program:\n" << vertex_path + '\n' << "\n" << fragment_path + '\n' << "\n" << std::endl;
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &result);
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
std::vector<char> programError((logLength > 1) ? logLength : 1);
glGetProgramInfoLog(program, logLength, NULL, &programError[0]);
if (!&programError[0]) {
std::cout << &programError[0] << std::endl;
}
glDeleteShader(vertShader);
glDeleteShader(fragShader);
return program;
}
GLuint Shader::BuildShader(GLenum eShaderType, const std::string& shaderText)
{
GLuint shader = glCreateShader(eShaderType);
const char* strFileData = shaderText.c_str();
glShaderSource(shader, 1, &strFileData, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
// Output the compile errors
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* strInfoLog = new GLchar[infoLogLength + 1];
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
const char* strShaderType = NULL;
switch (eShaderType)
{
case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
}
std::cerr << "Compile error in " << strShaderType << "\n\t" << strInfoLog << std::endl;
delete[] strInfoLog;
throw std::runtime_error("Shader compile exception");
}
return shader;
}
GLuint Shader::BuildShaderProgram(std::string vertShaderStr, std::string fragShaderStr)
{
GLuint vertShader, fragShader;
GLint result = GL_FALSE;
try
{
vertShader = BuildShader(GL_VERTEX_SHADER, vertShaderStr);
fragShader = BuildShader(GL_FRAGMENT_SHADER, fragShaderStr);
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
throw std::exception("BuildShaderProgram() Build shader failure. Abandoning");
}
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
std::cerr << "Linker error: " << strInfoLog << std::endl;
delete[] strInfoLog;
throw std::runtime_error("Shader could not be linked.");
}
glDeleteShader(vertShader);
glDeleteShader(fragShader);
return program;
}