2
* Copyright Ā© 2011 Linaro Ltd.
4
* Permission to use, copy, modify, distribute, and sell this software
5
* and its documentation for any purpose is hereby granted without
6
* fee, provided that the above copyright notice appear in all copies
7
* and that both that copyright notice and this permission notice
8
* appear in supporting documentation, and that the name of
9
* Linaro Ltd. not be used in advertising or publicity pertaining to
10
* distribution of the software without specific, written prior permission.
11
* Linaro Ltd. makes no representations about the suitability of this
12
* software for any purpose. It is provided "as is" without express or
15
* LINARO LTD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17
* NO EVENT SHALL LINARO LTD. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
* Authors: Travis Watkins <travis.watkins@linaro.org>
28
#include <opengl/opengl.h>
38
void printShaderInfoLog (GLuint shader)
43
(*GL::getShaderiv) (shader, GL::INFO_LOG_LENGTH, &length);
48
infoLog = new GLchar[length];
49
(*GL::getShaderInfoLog) (shader, length, &chars, infoLog);
50
std::cout << infoLog << std::endl;
55
void printProgramInfoLog(GLuint program)
60
(*GL::getProgramiv) (program, GL::INFO_LOG_LENGTH, &length);
65
infoLog = new GLchar[length];
66
(*GL::getProgramInfoLog) (program, length, &chars, infoLog);
67
std::cout << infoLog << std::endl;
72
static bool compileShader (GLuint *shader, GLenum type, CompString &source)
77
data = (GLchar *)source.c_str ();
79
*shader = (*GL::createShader) (type);
80
(*GL::shaderSource) (*shader, 1, &data, NULL);
81
(*GL::compileShader) (*shader);
83
(*GL::getShaderiv) (*shader, GL::COMPILE_STATUS, &status);
84
return (status == GL_TRUE);
87
GLProgram::GLProgram (CompString &vertexShader, CompString &fragmentShader) :
88
priv (new PrivateProgram ())
90
GLuint vertex, fragment;
94
priv->program = (*GL::createProgram) ();
96
if (!compileShader (&vertex, GL::VERTEX_SHADER, vertexShader))
98
printShaderInfoLog (vertex);
99
std::cout << vertexShader << std::endl << std::endl;
103
if (!compileShader (&fragment, GL::FRAGMENT_SHADER, fragmentShader))
105
printShaderInfoLog (fragment);
106
std::cout << fragmentShader << std::endl << std::endl;
110
(*GL::attachShader) (priv->program, vertex);
111
(*GL::attachShader) (priv->program, fragment);
113
(*GL::linkProgram) (priv->program);
114
(*GL::validateProgram) (priv->program);
116
(*GL::getProgramiv) (priv->program, GL::LINK_STATUS, &status);
117
if (status == GL_FALSE)
119
printProgramInfoLog (priv->program);
123
(*GL::deleteShader) (vertex);
124
(*GL::deleteShader) (fragment);
129
GLProgram::~GLProgram ()
131
(*GL::deleteProgram) (priv->program);
135
bool GLProgram::valid ()
140
void GLProgram::bind ()
142
(*GL::useProgram) (priv->program);
145
void GLProgram::unbind ()
147
(*GL::useProgram) (0);
150
bool GLProgram::setUniform (const char *name, GLfloat value)
152
GLint location = (*GL::getUniformLocation) (priv->program, name);
156
(*GL::uniform1f) (location, value);
160
bool GLProgram::setUniform (const char *name, GLint value)
162
GLint location = (*GL::getUniformLocation) (priv->program, name);
166
(*GL::uniform1i) (location, value);
170
bool GLProgram::setUniform (const char *name, const GLMatrix &value)
172
GLint location = (*GL::getUniformLocation) (priv->program, name);
176
(*GL::uniformMatrix4fv) (location, 1, GL_FALSE, value.getMatrix ());
180
bool GLProgram::setUniform2f (const char *name,
184
GLint location = (*GL::getUniformLocation) (priv->program, name);
188
(*GL::uniform2f) (location, x, y);
192
bool GLProgram::setUniform3f (const char *name,
197
GLint location = (*GL::getUniformLocation) (priv->program, name);
201
(*GL::uniform3f) (location, x, y, z);
205
bool GLProgram::setUniform4f (const char *name,
211
GLint location = (*GL::getUniformLocation) (priv->program, name);
215
(*GL::uniform4f) (location, x, y, z, w);
219
bool GLProgram::setUniform2i (const char *name,
223
GLint location = (*GL::getUniformLocation) (priv->program, name);
227
(*GL::uniform2i) (location, x, y);
231
bool GLProgram::setUniform3i (const char *name,
236
GLint location = (*GL::getUniformLocation) (priv->program, name);
240
(*GL::uniform3i) (location, x, y, z);
244
bool GLProgram::setUniform4i (const char *name,
250
GLint location = (*GL::getUniformLocation) (priv->program, name);
254
(*GL::uniform4i) (location, x, y, z, w);
258
GLuint GLProgram::attributeLocation (const char *name)
260
return (*GL::getAttribLocation) (priv->program, name);