10 std::ifstream file(filename, std::ios::binary);
11 if (!file.is_open()) {
12 std::cerr <<
"Failed to open shader file: " << filename << std::endl;
17 std::stringstream buffer;
18 buffer << file.rdbuf();
19 std::string str = buffer.str();
22 char* contents =
new char[str.length() + 1];
25 std::copy(str.begin(), str.end(), contents);
26 contents[str.length()] =
'\0';
31bool glsl::compiledStatus(GLint shaderID)
34 glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
40 glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
41 std::vector<
char> msgBuffer(logLength);
42 glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer.data());
43 std::cerr <<
"Shader compilation error: " << msgBuffer.data() << std::endl;
47GLuint
glsl::makeVertexShader(
const char* shaderSource)
50 std::cerr <<
"Null vertex shader source provided" << std::endl;
54 GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
55 glShaderSource(vertexShaderID, 1, &shaderSource, NULL);
56 glCompileShader(vertexShaderID);
57 bool compiledCorrectly = compiledStatus(vertexShaderID);
58 if (compiledCorrectly) {
59 return vertexShaderID;
63 glDeleteShader(vertexShaderID);
67GLuint
glsl::makeFragmentShader(
const char* shaderSource)
70 std::cerr <<
"Null fragment shader source provided" << std::endl;
74 GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
75 glShaderSource(fragmentShaderID, 1, &shaderSource, NULL);
76 glCompileShader(fragmentShaderID);
77 bool compiledCorrectly = compiledStatus(fragmentShaderID);
78 if (compiledCorrectly) {
79 return fragmentShaderID;
83 glDeleteShader(fragmentShaderID);
87GLuint
glsl::makeShaderProgram(GLuint vertexShaderID, GLuint fragmentShaderID)
89 if (vertexShaderID == 0 || fragmentShaderID == 0) {
90 std::cerr <<
"Invalid shader IDs provided to makeShaderProgram" << std::endl;
94 GLuint programID = glCreateProgram();
95 glAttachShader(programID, vertexShaderID);
96 glAttachShader(programID, fragmentShaderID);
97 glLinkProgram(programID);
101 glGetProgramiv(programID, GL_LINK_STATUS, &linked);
104 glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &logLength);
105 std::vector<
char> msgBuffer(logLength);
106 glGetProgramInfoLog(programID, logLength, NULL, msgBuffer.data());
107 std::cerr <<
"Shader program linking error: " << msgBuffer.data() << std::endl;
110 glDeleteProgram(programID);
115 glDetachShader(programID, vertexShaderID);
116 glDetachShader(programID, fragmentShaderID);
118 std::clog <<
"Shader program created successfully id: " << programID << std::endl;
static char * readFile(const char *filename)