37#include <unordered_map>
48extern const GLchar* preamble;
54 programId_ = glCreateProgram();
58 bool addShaderFromSource(std::string
const &shaderSource, GLenum shaderType) {
59 char const *shaderCStr = shaderSource.c_str();
60 GLuint shaderId = glCreateShader(shaderType);
62 GLchar
const* files[] = { preamble, shaderCStr };
63 GLint lengths[] = { (GLint)strlen(preamble), (GLint)strlen(shaderCStr) };
65 glShaderSource(shaderId, 2, files, lengths);
67 glCompileShader(shaderId);
68 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
71 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
73 auto log = std::unique_ptr<char>(
new char[logLength]);
74 glGetShaderInfoLog(shaderId, logLength, &logLength, log.get());
75 printf(
"ERROR::SHADER::COMPILATION_FAILED\n%s\n", log.get());
76#ifdef USE_ANDROID_GLES2
77 qDebug() <<
"SHADER COMPILE ERROR " << log.get();
83 glAttachShader(programId_, shaderId);
88 glLinkProgram(programId_);
89 glGetProgramiv( programId_, GL_LINK_STATUS, &linkSuccess);
90 if (linkSuccess == GL_FALSE) {
92 glGetShaderiv(programId_, GL_INFO_LOG_LENGTH, &logLength);
94 auto log = std::unique_ptr<char>(
new char[logLength]);
95 glGetShaderInfoLog(programId_, logLength, &logLength, log.get());
96 printf(
"ERROR::SHADER::LINK_FAILED\n%s\n", log.get());
105 void Bind() { glUseProgram(programId_); }
107 glDisableVertexAttribArray(0);
111 void SetUniform1f(
const std::string &name,
float value) {
112 GLint loc = getUniformLocation(name);
113 glUniform1f( loc, value);
115 void SetUniform2fv(
const std::string &name,
float *value) {
116 GLint loc = getUniformLocation(name);
117 glUniform2fv( loc, 1, value);
119 void SetUniform4fv(
const std::string &name,
float *value) {
120 GLint loc = getUniformLocation(name);
121 glUniform4fv( loc, 1, value);
123 void SetUniform1i(
const std::string &name, GLint value) {
124 GLint loc = getUniformLocation(name);
125 glUniform1i( loc, value);
127 void SetUniformMatrix4fv(
const std::string &name,
float *value) {
128 GLint matloc = getUniformLocation(name);
129 glUniformMatrix4fv(matloc, 1, GL_FALSE, value);
132 void SetAttributePointerf(
const char *name,
float *value ){
133 GLint aloc = glGetAttribLocation(programId_, name);
134 glVertexAttribPointer(aloc, 2, GL_FLOAT, GL_FALSE, 2 *
sizeof(
float), value);
135 glEnableVertexAttribArray(aloc);
138 glBindBuffer(GL_ARRAY_BUFFER, 0);
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
146 GLuint programId()
const {
return programId_; }
147 bool isOK()
const {
return linked_; }
150 std::unordered_map<std::string, GLint> m_uniformLocationCache;
156 GLint getUniformLocation(
const std::string &name) {
157 if(m_uniformLocationCache.find(name) != m_uniformLocationCache.end())
158 return m_uniformLocationCache[name];
160 GLint loc = glGetUniformLocation(programId_, name.c_str());
161 m_uniformLocationCache[name] = loc;
168bool loadShaders(
int index = 0);
169void reConfigureShaders(
int index = 0);