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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
40#ifndef __PISHADERS_H__
41#define __PISHADERS_H__
42
43#include "wx/wxprec.h"
44#ifndef WX_PRECOMP
45#include "wx/wx.h"
46#endif // precompiled headers
47
48#include "pi_gl.h"
49
50extern GLint GRIBpi_color_tri_shader_program;
51extern GLint GRIBpi_colorv_tri_shader_program;
52extern GLint pi_texture_2D_shader_program;
53extern GLint pi_circle_filled_shader_program;
54
55bool pi_loadShaders();
56void configureShaders(float width, float height);
57
58extern const GLchar *PI_shader_preamble;
59
60enum Consts { INFOLOG_LEN = 512 };
61
63public:
64 class Builder {
65 public:
66 Builder() : linked_(false) { programId_ = glCreateProgram(); }
67
68 Builder &addShaderFromSource(std::string const &shaderSource,
69 GLenum shaderType) {
70 char const *shaderCStr = shaderSource.c_str();
71 GLuint shaderId = glCreateShader(shaderType);
72
73 GLchar const *files[] = {PI_shader_preamble, shaderCStr};
74 GLint lengths[] = {(GLint)strlen(PI_shader_preamble),
75 (GLint)strlen(shaderCStr)};
76
77 glShaderSource(shaderId, 2, files, lengths);
78 // glShaderSource(shaderId, 1, &shaderCStr, nullptr);
79
80 glCompileShader(shaderId);
81 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
82 if (!success) {
83 glGetShaderInfoLog(shaderId, INFOLOG_LEN, nullptr, infoLog);
84 printf("ERROR::SHADER::COMPILATION_FAILED\n%s\n", infoLog);
85 // ret_val = false;
86 }
87
88 glAttachShader(programId_, shaderId);
89 return *this;
90 }
91
92 // void addShaderFromFile(std::string const &shaderFile, GLenum
93 // shaderType) {
94 // std::ifstream fileName(shaderFile);
95 // std::istreambuf_iterator<char> fileBegin(fileName), fileEnd;
96 // std::string fileContents(fileBegin, fileEnd);
97 // return addShaderFromSource(fileContents, shaderType);
98 // }
99
100 PI_GLShaderProgram linkProgram() {
101 glLinkProgram(programId_);
102 glGetProgramiv(programId_, GL_LINK_STATUS,
103 &linkSuccess); // requesting the status
104 if (linkSuccess == GL_FALSE) {
105 glGetProgramInfoLog(programId_, INFOLOG_LEN, nullptr, infoLog);
106 printf("ERROR::SHADER::LINK_FAILED\n%s\n", infoLog);
107 }
108
109 PI_GLShaderProgram theProgram(programId_);
110 // deleteAttachedShaders(programId_);
111 linked_ = true;
112 return theProgram;
113 }
114
115 ~Builder() {
116 if (!linked_) {
117 glDeleteProgram(programId_);
118 }
119 }
120
121 private:
122 GLuint programId_;
123 bool linked_;
124 GLint success;
125 GLint linkSuccess;
126 GLchar infoLog[INFOLOG_LEN];
127 };
128
129 PI_GLShaderProgram() : programId_(0) {}
130
131 PI_GLShaderProgram(PI_GLShaderProgram &&other) { *this = std::move(other); }
132
133 PI_GLShaderProgram &operator=(PI_GLShaderProgram &&other) {
134 programId_ = other.programId_;
135 other.programId_ = 0;
136
137 if (other.programId_ != 0) {
138 glDeleteProgram(other.programId_);
139 }
140
141 return *this;
142 }
143
145
146 GLuint programId() const { return programId_; }
147
148 PI_GLShaderProgram(PI_GLShaderProgram const &other) = delete;
149 PI_GLShaderProgram &operator=(PI_GLShaderProgram const &other) = delete;
150
151private:
152 PI_GLShaderProgram(GLuint programId) : programId_(programId) {}
153 GLuint programId_;
154};
155
156#endif
OpenGL Platform Abstraction Layer.