-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglsupport.cpp
97 lines (74 loc) · 2.67 KB
/
glsupport.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
93
94
95
96
97
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <stdexcept>
#include "glsupport.h"
using namespace std;
void checkGlErrors() {
const GLenum errCode = glGetError();
if (errCode != GL_NO_ERROR) {
string error("GL Error: ");
error += reinterpret_cast<const char*>(gluErrorString(errCode));
cerr << error << endl;
throw runtime_error(error);
}
}
// Dump text file into a character vector, throws exception on error
static void readTextFile(const char *fn, vector<char>& data) {
// Sets ios::binary bit to prevent end of line translation, so that the
// number of bytes we read equals file size
ifstream ifs(fn, ios::binary);
if (!ifs)
throw runtime_error(string("Cannot open file ") + fn);
// Sets bits to report IO error using exception
ifs.exceptions(ios::eofbit | ios::failbit | ios::badbit);
ifs.seekg(0, ios::end);
size_t len = ifs.tellg();
data.resize(len);
ifs.seekg(0, ios::beg);
ifs.read(&data[0], len);
}
// Print info regarding an GL object
static void printInfoLog(GLuint obj, const string& filename) {
GLint infologLength = 0;
GLint charsWritten = 0;
glGetObjectParameterivARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength);
if (infologLength > 0) {
string infoLog(infologLength, ' ');
glGetInfoLogARB(obj, infologLength, &charsWritten, &infoLog[0]);
std::cerr << "##### Log [" << filename << "]:\n" << infoLog << endl;
}
}
void readAndCompileSingleShader(GLuint shaderHandle, const char *fn) {
vector<char> source;
readTextFile(fn, source);
const char *ptrs[] = {&source[0]};
const GLint lens[] = {source.size()};
glShaderSource(shaderHandle, 1, ptrs, lens); // load the shader sources
glCompileShader(shaderHandle);
printInfoLog(shaderHandle, fn);
GLint compiled = 0;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compiled);
if (!compiled)
throw runtime_error("fails to compile GL shader");
}
void linkShader(GLuint programHandle, GLuint vs, GLuint fs) {
glAttachShader(programHandle, vs);
glAttachShader(programHandle, fs);
glLinkProgram(programHandle);
glDetachShader(programHandle, vs);
glDetachShader(programHandle, fs);
GLint linked = 0;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linked);
printInfoLog(programHandle, "linking");
if (!linked)
throw runtime_error("fails to link shaders");
}
void readAndCompileShader(GLuint programHandle, const char * vertexShaderFileName, const char * fragmentShaderFileName) {
GlShader vs(GL_VERTEX_SHADER);
GlShader fs(GL_FRAGMENT_SHADER);
readAndCompileSingleShader(vs, vertexShaderFileName);
readAndCompileSingleShader(fs, fragmentShaderFileName);
linkShader(programHandle, vs, fs);
}