OpenCPN Partial API docs
Loading...
Searching...
No Matches
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
39class GLShaderProgram;
40
41extern GLShaderProgram *pAALine_shader_program[2];
42extern GLShaderProgram *pcolor_tri_shader_program[2];
43extern GLShaderProgram *ptexture_2D_shader_program[2];
44extern GLShaderProgram *pcircle_filled_shader_program[2];
45extern GLShaderProgram *ptexture_2DA_shader_program[2];
46extern GLShaderProgram *pring_shader_program[2];
47
48extern const GLchar* preamble;
49
51{
52public:
53 GLShaderProgram() : programId_(0), linked_(false) {
54 programId_ = glCreateProgram();
55 }
57
58 bool addShaderFromSource(std::string const &shaderSource, GLenum shaderType) {
59 char const *shaderCStr = shaderSource.c_str();
60 GLuint shaderId = glCreateShader(shaderType);
61
62 GLchar const* files[] = { preamble, shaderCStr };
63 GLint lengths[] = { (GLint)strlen(preamble), (GLint)strlen(shaderCStr) };
64
65 glShaderSource(shaderId, 2, files, lengths);
66
67 glCompileShader(shaderId);
68 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
69 if (!success) {
70 GLint logLength = 0;
71 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
72 if (logLength > 0) {
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();
78#endif
79 }
80 return false;
81 }
82
83 glAttachShader(programId_, shaderId);
84 return true;
85 }
86
87 bool linkProgram() {
88 glLinkProgram(programId_);
89 glGetProgramiv( programId_, GL_LINK_STATUS, &linkSuccess); //requesting the status
90 if (linkSuccess == GL_FALSE) {
91 GLint logLength = 0;
92 glGetShaderiv(programId_, GL_INFO_LOG_LENGTH, &logLength);
93 if (logLength > 0) {
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());
97 }
98 return false;
99 }
100 linked_ = true;
101 return true;
102 }
103
104
105 void Bind() { glUseProgram(programId_); }
106 void UnBind() {
107 glDisableVertexAttribArray(0);
108 glUseProgram(0);
109 }
110
111 void SetUniform1f( const std::string &name, float value) {
112 GLint loc = getUniformLocation(name);
113 glUniform1f( loc, value);
114 }
115 void SetUniform2fv( const std::string &name, float *value) {
116 GLint loc = getUniformLocation(name);
117 glUniform2fv( loc, 1, value);
118 }
119 void SetUniform4fv( const std::string &name, float *value) {
120 GLint loc = getUniformLocation(name);
121 glUniform4fv( loc, 1, value);
122 }
123 void SetUniform1i( const std::string &name, GLint value) {
124 GLint loc = getUniformLocation(name);
125 glUniform1i( loc, value);
126 }
127 void SetUniformMatrix4fv( const std::string &name, float *value) {
128 GLint matloc = getUniformLocation(name);
129 glUniformMatrix4fv(matloc, 1, GL_FALSE, value);
130 }
131
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);
136
137 // Disable VBO's (vertex buffer objects) for attributes.
138 glBindBuffer(GL_ARRAY_BUFFER, 0);
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
140
141 }
142
143
144
145
146 GLuint programId() const { return programId_; }
147 bool isOK() const { return linked_; }
148
149private:
150 std::unordered_map<std::string, GLint> m_uniformLocationCache;
151 GLuint programId_;
152 bool linked_;
153 GLint success;
154 GLint linkSuccess;
155
156 GLint getUniformLocation(const std::string &name) {
157 if(m_uniformLocationCache.find(name) != m_uniformLocationCache.end())
158 return m_uniformLocationCache[name];
159
160 GLint loc = glGetUniformLocation(programId_, name.c_str());
161 m_uniformLocationCache[name] = loc;
162 return loc;
163 }
164
165};
166
167
168bool loadShaders(int index = 0);
169void reConfigureShaders(int index = 0);
170void unloadShaders();
171
172GLShaderProgram *GetStaticTriShader();
173#endif