PICCANTE  0.4
The hottest HDR imaging library!
program.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_GL_PROGRAM_HPP
19 #define PIC_UTIL_GL_PROGRAM_HPP
20 
21 #include <string>
22 
23 #include "../../base.hpp"
24 
25 namespace pic {
26 
27 class ProgramGL
28 {
29 protected:
30  GLint bCheckStatus;
31  GLenum type;
32  GLuint object;
33  std::string source;
34  std::string log;
35  bool bType;
36 
42  {
43  log.clear();
44 
45  bCheckStatus = GL_FALSE;
46  glGetShaderiv(object, GL_COMPILE_STATUS, &bCheckStatus);
47 
48  //get the log in case of failure
49  if(bCheckStatus == GL_FALSE) {
50  GLint max_length = -1;
51  glGetShaderiv(object, GL_INFO_LOG_LENGTH, &max_length);
52 
53  std::vector<GLchar> error(max_length);
54  glGetShaderInfoLog(object, max_length, &max_length, &error[0]);
55 
56  glDeleteShader(object);
57  object = 0;
58 
59  log += "-----------------------------------------\n";
60  log += "-- This shader was not compiled!\n";
61  log += "-----------------------------------------\n";
62 
63  return false;
64  } else {
65  log += "-----------------------------------------\n";
66  log += "-- This shader was compiled successfully!\n";
67  log += "-----------------------------------------\n";
68 
69  return true;
70  }
71  }
72 
78  {
79  bCheckStatus = GL_FALSE;
80  glGetProgramiv(object, GL_LINK_STATUS, &bCheckStatus);
81 
82  //get the log in case of failure
83  if(bCheckStatus == GL_FALSE) {
84  GLint max_length = -1;
85  glGetProgramiv(object, GL_INFO_LOG_LENGTH, &max_length);
86 
87  std::vector<GLchar> error(max_length);
88  glGetProgramInfoLog(object, max_length, &max_length, &error[0]);
89 
90  glDeleteProgram(object);
91  object = 0;
92 
93  log += "----------------------------------------\n";
94  log += "-- This program was not linked!\n";
95  log += "----------------------------------------\n";
96 
97  return false;
98  } else {
99  log += "----------------------------------------\n";
100  log += "-- This program was linked successfully!\n";
101  log += "----------------------------------------\n";
102 
103  return true;
104  }
105  }
106 
107 public:
108 
109  bool bCompiled;
110 
112  {
113  setNULL();
114  }
115 
116  ProgramGL( std::string version,
117  std::string extensions,
118  std::string source,
119  GLenum type)
120  {
121  setNULL();
122  initShader(version, extensions, source, type);
123  }
124 
125  ProgramGL(std::vector<ProgramGL*> &shaders)
126  {
127  initProgram(shaders);
128  }
129 
131  {
132  if(object != 0) {
133  if(bType) {
134  glDeleteShader(object);
135  } else {
136  glDeleteProgram(object);
137  }
138  object = 0;
139  }
140 
141  log.clear();
142  source.clear();
143  }
144 
148  void setNULL()
149  {
150  this->bCompiled = false;
151  this->bType = true;
152  this->type = 0;
153  this->object = 0;
154  this->log = "";
155  this->source = "";
156  this->bCheckStatus = GL_FALSE;
157  }
158 
163  GLuint getObject()
164  {
165  return object;
166  }
167 
173  bool initProgram(std::vector<ProgramGL*> &shaders)
174  {
175  bType = false;
176  object = glCreateProgram();
177 
178  for(auto i = 0; i < shaders.size(); i++)
179  {
180  GLuint tmp = shaders[i]->getObject();
181  if(tmp != 0) {
182  glAttachShader(object, tmp);
183  }
184  }
185 
186  glLinkProgram(object);
187 
189 
190  return bCompiled;
191  }
192 
201  bool initShader( std::string version_number,
202  std::string extensions,
203  std::string source,
204  GLenum type)
205  {
206  this->type = type;
207  this->bType = true;
208 
209  object = glCreateShader(type);
210 
211  this->source.clear();
212 
213  //create full source
214  if(!version_number.empty()) {
215  this->source += "#version ";
216  this->source += version_number;
217  this->source += "\n";
218  }
219 
220  if(!extensions.empty()) {
221  this->source += extensions;
222  this->source += "\n";
223  }
224 
225  if(!source.empty()) {
226  this->source += source;
227  }
228 
229  const GLchar *tmp = (const GLchar *) this->source.c_str();
230  glShaderSource(object, 1, &tmp, NULL);
231  glCompileShader(object);
232 
233  return checkShaderStatus();
234  }
235 
239  void printLog()
240  {
241  printf("%s", log.c_str());
242  }
243 
244 };
245 
246 } // end namespace pic
247 
248 #endif /* PIC_UTIL_GL_PROGRAM_HPP */
249 
bool checkProgramStatus()
checkProgramStatus
Definition: program.hpp:77
ProgramGL()
Definition: program.hpp:111
GLuint object
Definition: program.hpp:32
GLint bCheckStatus
Definition: program.hpp:30
std::string source
Definition: program.hpp:33
GLuint getObject()
getObject
Definition: program.hpp:163
bool initShader(std::string version_number, std::string extensions, std::string source, GLenum type)
initShader
Definition: program.hpp:201
Definition: program.hpp:27
ProgramGL(std::vector< ProgramGL *> &shaders)
Definition: program.hpp:125
void setNULL()
SetNULL.
Definition: program.hpp:148
void printLog()
printLog
Definition: program.hpp:239
GLenum type
Definition: program.hpp:31
std::string log
Definition: program.hpp:34
~ProgramGL()
Definition: program.hpp:130
bool bType
Definition: program.hpp:35
bool checkShaderStatus()
checkShaderStatus
Definition: program.hpp:41
Definition: bilateral_separation.hpp:25
ProgramGL(std::string version, std::string extensions, std::string source, GLenum type)
Definition: program.hpp:116
bool bCompiled
Definition: program.hpp:109
bool initProgram(std::vector< ProgramGL *> &shaders)
initProgram
Definition: program.hpp:173