OpenCPN Partial API docs
shaders.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Copyright (C) 2017 by David S. Register *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  **************************************************************************/
23 
24 #ifndef __SHADERS_H__
25 #define __SHADERS_H__
26 
27 #include <wx/wxprec.h>
28 #ifndef WX_PRECOMP
29 #include <wx/wx.h>
30 #endif // precompiled headers
31 
32 #include "dychart.h"
33 
34 #include <memory>
35 #include <vector>
36 #include <fstream>
37 #include <unordered_map>
38 
39 class GLShaderProgram;
40 
41 extern GLShaderProgram *pAALine_shader_program[2];
42 extern GLShaderProgram *pcolor_tri_shader_program[2];
43 extern GLShaderProgram *ptexture_2D_shader_program[2];
44 extern GLShaderProgram *pcircle_filled_shader_program[2];
45 extern GLShaderProgram *ptexture_2DA_shader_program[2];
46 extern GLShaderProgram *pring_shader_program[2];
47 
48 extern GLint texture_2DA_shader_program;
49 
50 extern const GLchar* preamble;
51 
53 {
54 public:
55  GLShaderProgram() : programId_(0), linked_(false) {
56  programId_ = glCreateProgram();
57  }
58  ~GLShaderProgram() {
59  glDeleteProgram(programId_) ;
60  }
61 
62  bool addShaderFromSource(std::string const &shaderSource, GLenum shaderType) {
63  char const *shaderCStr = shaderSource.c_str();
64  GLuint shaderId = glCreateShader(shaderType);
65 
66  GLchar const* files[] = { preamble, shaderCStr };
67  GLint lengths[] = { (GLint)strlen(preamble), (GLint)strlen(shaderCStr) };
68 
69  glShaderSource(shaderId, 2, files, lengths);
70 
71  glCompileShader(shaderId);
72  glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
73  if (!success) {
74  GLint logLength = 0;
75  glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
76  if (logLength > 0) {
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;
83 #endif
84  }
85  return false;
86  }
87 
88  glAttachShader(programId_, shaderId);
89  return true;
90  }
91 
92  bool linkProgram() {
93  glLinkProgram(programId_);
94  glGetProgramiv( programId_, GL_LINK_STATUS, &linkSuccess); //requesting the status
95  if (linkSuccess == GL_FALSE) {
96  GLint logLength = 0;
97  glGetShaderiv(programId_, GL_INFO_LOG_LENGTH, &logLength);
98  if (logLength > 0) {
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());
102  }
103  return false;
104  }
105  linked_ = true;
106  return true;
107  }
108 
109 
110  void Bind() { glUseProgram(programId_); }
111  void UnBind() {
112  glDisableVertexAttribArray(0);
113  glUseProgram(0);
114  }
115 
116  void SetUniform1f( const std::string &name, float value) {
117  GLint loc = getUniformLocation(name);
118  glUniform1f( loc, value);
119  }
120  void SetUniform2fv( const std::string &name, float *value) {
121  GLint loc = getUniformLocation(name);
122  glUniform2fv( loc, 1, value);
123  }
124  void SetUniform4fv( const std::string &name, float *value) {
125  GLint loc = getUniformLocation(name);
126  glUniform4fv( loc, 1, value);
127  }
128  void SetUniform1i( const std::string &name, GLint value) {
129  GLint loc = getUniformLocation(name);
130  glUniform1i( loc, value);
131  }
132  void SetUniformMatrix4fv( const std::string &name, float *value) {
133  GLint matloc = getUniformLocation(name);
134  glUniformMatrix4fv(matloc, 1, GL_FALSE, value);
135  }
136 
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);
141 
142  // Disable VBO's (vertex buffer objects) for attributes.
143  glBindBuffer(GL_ARRAY_BUFFER, 0);
144  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
145 
146  }
147 
148 
149 
150 
151  GLuint programId() const { return programId_; }
152  bool isOK() const { return linked_; }
153 
154 private:
155  std::unordered_map<std::string, GLint> m_uniformLocationCache;
156  GLuint programId_;
157  bool linked_;
158  GLint success;
159  GLint linkSuccess;
160 
161  GLint getUniformLocation(const std::string &name) {
162  if(m_uniformLocationCache.find(name) != m_uniformLocationCache.end())
163  return m_uniformLocationCache[name];
164 
165  GLint loc = glGetUniformLocation(programId_, name.c_str());
166  m_uniformLocationCache[name] = loc;
167  return loc;
168  }
169 
170 };
171 
172 
173 bool loadShaders(int index = 0);
174 void reConfigureShaders(int index = 0);
175 void unloadShaders();
176 
177 GLShaderProgram *GetStaticTriShader();
178 #endif