27 #include <wx/wxprec.h>
37 #include <unordered_map>
48 extern GLint texture_2DA_shader_program;
50 extern const GLchar* preamble;
56 programId_ = glCreateProgram();
59 glDeleteProgram(programId_) ;
62 bool addShaderFromSource(std::string
const &shaderSource, GLenum shaderType) {
63 char const *shaderCStr = shaderSource.c_str();
64 GLuint shaderId = glCreateShader(shaderType);
66 GLchar
const* files[] = { preamble, shaderCStr };
67 GLint lengths[] = { (GLint)strlen(preamble), (GLint)strlen(shaderCStr) };
69 glShaderSource(shaderId, 2, files, lengths);
71 glCompileShader(shaderId);
72 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
75 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
77 auto log = std::unique_ptr<char[]>(
new char[logLength]);
78 glGetShaderInfoLog(shaderId, logLength, &logLength, log.get());
79 printf(
"ERROR::SHADER::COMPILATION_FAILED\n%s\n", log.get());
80 #ifdef USE_ANDROID_GLES2
81 qDebug() <<
"SHADER COMPILE ERROR " << log.get();
82 qDebug() << shaderCStr;
88 glAttachShader(programId_, shaderId);
93 glLinkProgram(programId_);
94 glGetProgramiv( programId_, GL_LINK_STATUS, &linkSuccess);
95 if (linkSuccess == GL_FALSE) {
97 glGetShaderiv(programId_, GL_INFO_LOG_LENGTH, &logLength);
99 auto log = std::unique_ptr<char[]>(
new char[logLength]);
100 glGetShaderInfoLog(programId_, logLength, &logLength, log.get());
101 printf(
"ERROR::SHADER::LINK_FAILED\n%s\n", log.get());
110 void Bind() { glUseProgram(programId_); }
112 glDisableVertexAttribArray(0);
116 void SetUniform1f(
const std::string &name,
float value) {
117 GLint loc = getUniformLocation(name);
118 glUniform1f( loc, value);
120 void SetUniform2fv(
const std::string &name,
float *value) {
121 GLint loc = getUniformLocation(name);
122 glUniform2fv( loc, 1, value);
124 void SetUniform4fv(
const std::string &name,
float *value) {
125 GLint loc = getUniformLocation(name);
126 glUniform4fv( loc, 1, value);
128 void SetUniform1i(
const std::string &name, GLint value) {
129 GLint loc = getUniformLocation(name);
130 glUniform1i( loc, value);
132 void SetUniformMatrix4fv(
const std::string &name,
float *value) {
133 GLint matloc = getUniformLocation(name);
134 glUniformMatrix4fv(matloc, 1, GL_FALSE, value);
137 void SetAttributePointerf(
const char *name,
float *value ){
138 GLint aloc = glGetAttribLocation(programId_, name);
139 glVertexAttribPointer(aloc, 2, GL_FLOAT, GL_FALSE, 2 *
sizeof(
float), value);
140 glEnableVertexAttribArray(aloc);
143 glBindBuffer(GL_ARRAY_BUFFER, 0);
144 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
151 GLuint programId()
const {
return programId_; }
152 bool isOK()
const {
return linked_; }
155 std::unordered_map<std::string, GLint> m_uniformLocationCache;
161 GLint getUniformLocation(
const std::string &name) {
162 if(m_uniformLocationCache.find(name) != m_uniformLocationCache.end())
163 return m_uniformLocationCache[name];
165 GLint loc = glGetUniformLocation(programId_, name.c_str());
166 m_uniformLocationCache[name] = loc;
173 bool loadShaders(
int index = 0);
174 void reConfigureShaders(
int index = 0);
175 void unloadShaders();