2
// Copyright (c) 2011 Linaro Limited
4
// All rights reserved. This program and the accompanying materials
5
// are made available under the terms of the MIT License which accompanies
6
// this distribution, and is available at
7
// http://www.opensource.org/licenses/mit-license.php
10
// Jesse Barker - original implementation.
18
#include "gl-headers.h"
22
using LibMatrix::mat4;
23
using LibMatrix::vec2;
24
using LibMatrix::vec3;
25
using LibMatrix::vec4;
28
gotSource(const string& filename, string& source)
31
ifstream inputFile(filename.c_str());
34
std::cerr << "Failed to open \"" << filename << "\"" << std::endl;
39
while (getline(inputFile, curLine))
48
Shader::Shader(unsigned int type, const string& source) :
55
// Create our shader and setup the source code.
56
handle_ = glCreateShader(type);
59
message_ = string("Failed to create the new shader.");
62
const GLchar* shaderSource = source_.c_str();
63
glShaderSource(handle_, 1, &shaderSource, NULL);
65
glGetShaderiv(handle_, GL_SHADER_SOURCE_LENGTH, ¶m);
66
if (static_cast<unsigned int>(param) != source_.length() + 1)
68
std::ostringstream o(string("Expected shader source length "));
69
o << source_.length() << ", but got " << param << std::endl;
87
// Make sure we have a good shader and haven't already compiled it.
88
if (!valid_ || ready_)
92
glCompileShader(handle_);
94
glGetShaderiv(handle_, GL_COMPILE_STATUS, ¶m);
95
if (param == GL_FALSE)
97
glGetShaderiv(handle_, GL_INFO_LOG_LENGTH, ¶m);
98
GLchar* infoLog = new GLchar[param + 1];
99
glGetShaderInfoLog(handle_, param + 1, NULL, infoLog);
108
Shader::attach(unsigned int program)
110
// Shader must be valid and compiled to be attached to a program.
111
if (!valid_ || !ready_)
115
glAttachShader(program, handle_);
123
glDeleteShader(handle_);
140
// First release all of the shader resources attached to us and clean up
148
handle_ = glCreateProgram();
151
message_ = string("Failed to create the new program");
161
// First delete all of the shader resources attached to us.
162
for (std::vector<Shader>::iterator shaderIt = shaders_.begin(); shaderIt != shaders_.end(); shaderIt++)
167
// Clear out the shader vector so we're ready to reuse it.
170
// Clear out the error string to make sure we don't return anything stale.
175
glDeleteProgram(handle_);
182
Program::addShader(unsigned int type, const string& source)
189
Shader shader(type, source);
192
message_ = shader.errorMessage();
201
message_ = shader.errorMessage();
206
shader.attach(handle_);
207
shaders_.push_back(shader);
214
if (!valid_ || ready_)
219
if (shaders_.empty())
221
message_ = string("There are no shaders attached to this program");
225
glLinkProgram(handle_);
227
glGetProgramiv(handle_, GL_LINK_STATUS, ¶m);
228
if (param == GL_FALSE)
230
glGetProgramiv(handle_, GL_INFO_LOG_LENGTH, ¶m);
231
GLchar* infoLog = new GLchar[param + 1];
232
glGetProgramInfoLog(handle_, param + 1, NULL, infoLog);
243
if (!valid_ || !ready_)
247
glUseProgram(handle_);
257
Program::loadUniformMatrix(const mat4& m, const string& name)
260
GLint location = glGetUniformLocation(handle_, name.c_str());
263
message_ = string("Failed to get uniform location for \"") + name +
268
// Our matrix representation is column-major, so transpose is false here.
269
glUniformMatrix4fv(location, 1, GL_FALSE, m);
274
Program::loadUniformVector(const vec2& v, const string& name)
277
GLint location = glGetUniformLocation(handle_, name.c_str());
280
message_ = string("Failed to get uniform location for \"") + name +
285
glUniform2fv(location, 1, v);
290
Program::loadUniformVector(const vec3& v, const string& name)
293
GLint location = glGetUniformLocation(handle_, name.c_str());
296
message_ = string("Failed to get uniform location for \"") + name +
301
glUniform3fv(location, 1, v);
306
Program::loadUniformVector(const vec4& v, const string& name)
309
GLint location = glGetUniformLocation(handle_, name.c_str());
312
message_ = string("Failed to get uniform location for \"") + name +
317
glUniform4fv(location, 1, v);
322
Program::loadUniformScalar(const float& f, const string& name)
325
GLint location = glGetUniformLocation(handle_, name.c_str());
328
message_ = string("Failed to get uniform location for \"") + name +
333
glUniform1f(location, f);
338
Program::loadUniformScalar(const int& i, const string& name)
341
GLint location = glGetUniformLocation(handle_, name.c_str());
344
message_ = string("Failed to get uniform location for \"") + name +
349
glUniform1i(location, i);
354
Program::getAttribIndex(const string& name)
356
GLint index = glGetAttribLocation(handle_, name.c_str());
359
message_ = string("Failed to get attribute location for \"") + name +