11 string vertex_shader_source;
12 if (FileExists(path+
".vert")) {
13 vertex_shader_source = ReadFile(path+
".vert");
15 cerr <<
"Vertex shader file "
16 << path <<
".vert does not exist." << endl;
20 string fragment_shader_source;
21 if (FileExists(path+
".frag")) {
22 fragment_shader_source = ReadFile(path+
".frag");
24 cerr <<
"Fragment shader file " << path <<
".frag does not exist." << endl;
28 string geometry_shader_source;
29 if (FileExists(path+
".geom")) {
30 geometry_shader_source = ReadFile(path+
".geom");
32 cerr <<
"Geometry shader file " << path <<
".geom does not exist." << endl;
38 _vertex_shader = compile(GL_VERTEX_SHADER, vertex_shader_source);
39 if (_vertex_shader == 0)
43 _fragment_shader = compile(GL_FRAGMENT_SHADER, fragment_shader_source);
44 if (_fragment_shader == 0)
48 _geometry_shader = compile(GL_GEOMETRY_SHADER, geometry_shader_source);
49 if (_geometry_shader == 0)
67 glDeleteProgram(_program);
68 glDeleteShader(_vertex_shader);
69 glDeleteShader(_fragment_shader);
70 glDeleteShader(_geometry_shader);
76 void Shader::GenerateAddresses(
void) {
78 _shaderAddresses[Shader::Position] = 0;
79 _shaderAddresses[Shader::Radius] = 1;
80 _shaderAddresses[Shader::Color] = 2;
81 _shaderAddresses[Shader::UVQuad] = 3;
106 return _shaderAddresses[address];
109 GLuint Shader::compile (GLenum type,
const string &source)
113 GLuint shader = glCreateShader(type);
116 cerr <<
"Could not create shader object." << endl;
122 const char* src = source.data();
123 int len = source.size();
125 glShaderSource(shader, 1, &src, &len);
127 glCompileShader(shader);
133 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
135 if (status != GL_TRUE) {
136 qDebug() <<
"Shader compilation failed." << endl;
146 void Shader::link(
void)
149 _program = glCreateProgram();
153 glAttachShader(_program, _vertex_shader);
154 glAttachShader(_program, _fragment_shader);
156 glAttachShader(_program, _geometry_shader);
158 glLinkProgram(_program);
164 glGetProgramiv(_program, GL_LINK_STATUS, &status);
166 if (status != GL_TRUE) {
167 qDebug() <<
"Shader linking failed." << endl;
168 program_log(_program);
170 glDeleteProgram(_program);
177 #define LOG_BUFFER_SIZE 8096
179 void Shader::program_log(GLuint program)
181 char logBuffer[LOG_BUFFER_SIZE];
185 glGetProgramInfoLog(program, LOG_BUFFER_SIZE, &length,logBuffer);
188 qDebug() << logBuffer << endl;
192 void Shader::shader_log(GLuint shader)
194 char logBuffer[LOG_BUFFER_SIZE];
198 glGetShaderInfoLog(shader, LOG_BUFFER_SIZE, &length,logBuffer);
201 qDebug() << logBuffer << endl;