OpenCPN Partial API docs
Loading...
Searching...
No Matches
pi_shaders.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2017 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
41#ifndef PISHADERS_H__
42#define PISHADERS_H__
43
44#include <wx/wxprec.h>
45
46#ifndef WX_PRECOMP
47#include <wx/wx.h>
48#endif
49
50#include "pi_gl.h"
51
52extern GLint GRIBpi_color_tri_shader_program;
53extern GLint GRIBpi_colorv_tri_shader_program;
54extern GLint pi_texture_2D_shader_program;
55extern GLint pi_circle_filled_shader_program;
56
57bool pi_loadShaders();
58void configureShaders(float width, float height);
59
60extern const GLchar *PI_shader_preamble;
61
62enum Consts { INFOLOG_LEN = 512 };
63
65public:
66 class Builder {
67 public:
68 Builder() : linked_(false) { programId_ = glCreateProgram(); }
69
70 Builder &addShaderFromSource(std::string const &shaderSource,
71 GLenum shaderType) {
72 char const *shaderCStr = shaderSource.c_str();
73 GLuint shaderId = glCreateShader(shaderType);
74
75 GLchar const *files[] = {PI_shader_preamble, shaderCStr};
76 GLint lengths[] = {(GLint)strlen(PI_shader_preamble),
77 (GLint)strlen(shaderCStr)};
78
79 glShaderSource(shaderId, 2, files, lengths);
80 // glShaderSource(shaderId, 1, &shaderCStr, nullptr);
81
82 glCompileShader(shaderId);
83 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
84 if (!success) {
85 glGetShaderInfoLog(shaderId, INFOLOG_LEN, nullptr, infoLog);
86 printf("ERROR::SHADER::COMPILATION_FAILED\n%s\n", infoLog);
87 // ret_val = false;
88 }
89
90 glAttachShader(programId_, shaderId);
91 return *this;
92 }
93
94 // void addShaderFromFile(std::string const &shaderFile, GLenum
95 // shaderType) {
96 // std::ifstream fileName(shaderFile);
97 // std::istreambuf_iterator<char> fileBegin(fileName), fileEnd;
98 // std::string fileContents(fileBegin, fileEnd);
99 // return addShaderFromSource(fileContents, shaderType);
100 // }
101
102 PI_GLShaderProgram linkProgram() {
103 glLinkProgram(programId_);
104 glGetProgramiv(programId_, GL_LINK_STATUS,
105 &linkSuccess); // requesting the status
106 if (linkSuccess == GL_FALSE) {
107 glGetProgramInfoLog(programId_, INFOLOG_LEN, nullptr, infoLog);
108 printf("ERROR::SHADER::LINK_FAILED\n%s\n", infoLog);
109 }
110
111 PI_GLShaderProgram theProgram(programId_);
112 // deleteAttachedShaders(programId_);
113 linked_ = true;
114 return theProgram;
115 }
116
117 ~Builder() {
118 if (!linked_) {
119 glDeleteProgram(programId_);
120 }
121 }
122
123 private:
124 GLuint programId_;
125 bool linked_;
126 GLint success;
127 GLint linkSuccess;
128 GLchar infoLog[INFOLOG_LEN];
129 };
130
131 PI_GLShaderProgram() : programId_(0) {}
132
133 PI_GLShaderProgram(PI_GLShaderProgram &&other) { *this = std::move(other); }
134
135 PI_GLShaderProgram &operator=(PI_GLShaderProgram &&other) {
136 programId_ = other.programId_;
137 other.programId_ = 0;
138
139 if (other.programId_ != 0) {
140 glDeleteProgram(other.programId_);
141 }
142
143 return *this;
144 }
145
146 ~PI_GLShaderProgram() = default;
147
148 GLuint programId() const { return programId_; }
149
150 PI_GLShaderProgram(PI_GLShaderProgram const &other) = delete;
151 PI_GLShaderProgram &operator=(PI_GLShaderProgram const &other) = delete;
152
153private:
154 PI_GLShaderProgram(GLuint programId) : programId_(programId) {}
155 GLuint programId_;
156};
157
158#endif // PISHADERS_H__
OpenGL Platform Abstraction Layer.