Loading...
Searching...
No Matches
glsl.cpp
Go to the documentation of this file.
1#include "glsl.h"
2#include <fstream>
3#include <iostream>
4#include <sstream>
5#include <vector>
6
7char* glsl::readFile(const char* filename)
8{
9 // Open the file using C++ streams for better error handling
10 std::ifstream file(filename, std::ios::binary);
11 if (!file.is_open()) {
12 std::cerr << "Failed to open shader file: " << filename << std::endl;
13 return nullptr;
14 }
15
16 // Read the entire file into a string
17 std::stringstream buffer;
18 buffer << file.rdbuf();
19 std::string str = buffer.str();
20
21 // Allocate memory for the char array (+1 for null terminator)
22 char* contents = new char[str.length() + 1];
23
24 // Copy the string contents and add null terminator
25 std::copy(str.begin(), str.end(), contents);
26 contents[str.length()] = '\0';
27
28 return contents;
29}
30
31bool glsl::compiledStatus(GLint shaderID)
32{
33 GLint compiled = 0;
34 glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
35 if (compiled) {
36 return true;
37 }
38
39 GLint logLength;
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;
44 return false;
45}
46
47GLuint glsl::makeVertexShader(const char* shaderSource)
48{
49 if (!shaderSource) {
50 std::cerr << "Null vertex shader source provided" << std::endl;
51 return 0;
52 }
53
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;
60 }
61
62 // Clean up on failure
63 glDeleteShader(vertexShaderID);
64 return 0;
65}
66
67GLuint glsl::makeFragmentShader(const char* shaderSource)
68{
69 if (!shaderSource) {
70 std::cerr << "Null fragment shader source provided" << std::endl;
71 return 0;
72 }
73
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;
80 }
81
82 // Clean up on failure
83 glDeleteShader(fragmentShaderID);
84 return 0;
85}
86
87GLuint glsl::makeShaderProgram(GLuint vertexShaderID, GLuint fragmentShaderID)
88{
89 if (vertexShaderID == 0 || fragmentShaderID == 0) {
90 std::cerr << "Invalid shader IDs provided to makeShaderProgram" << std::endl;
91 return 0;
92 }
93
94 GLuint programID = glCreateProgram();
95 glAttachShader(programID, vertexShaderID);
96 glAttachShader(programID, fragmentShaderID);
97 glLinkProgram(programID);
98
99 // Check linking status
100 GLint linked;
101 glGetProgramiv(programID, GL_LINK_STATUS, &linked);
102 if (!linked) {
103 GLint logLength;
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;
108
109 // Clean up
110 glDeleteProgram(programID);
111 return 0;
112 }
113
114 // Detach shaders after successful linking
115 glDetachShader(programID, vertexShaderID);
116 glDetachShader(programID, fragmentShaderID);
117
118 std::clog << "Shader program created successfully id: " << programID << std::endl;
119
120 return programID;
121}
Definition glsl.h:9
static char * readFile(const char *filename)
Definition glsl.cpp:7