Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfShader.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfShader.hh"
26 #include "glfException.hh"
27 #include <fstream>
28 
29 using namespace BIAS;
30 using namespace std;
31 
33 {
34  id_ = 0;
35 }
36 
38 {
39  if (id_ != 0) {
40  glDeleteShader(id_);
41  }
42 }
43 
44 glfShader&
45 glfShader::operator=(const glfShader& b)
46 {
47  return (*this);
48 }
49 
50 
52 {}
53 
54 void glfShader::
55 Create(GLenum type)
56 {
57  if (id_ == 0) {
58  id_ = glCreateShader(type);
59  type_ = type;
60  }
61  GLF_THROW_ON_OPENGL_ERROR;
62 }
63 
64 void glfShader::Create(GLenum type, const std::string& sourceCode)
65 {
66  if (id_ == 0) {
67  id_ = glCreateShader(type);
68  type_ = type;
69  }
70 
71  // fstream file;
72  // file.open("shader.txt",fstream::out);
73  // file<<sourceCode.c_str();
74  // file.close();
75  // cout<<"sourceCode:"<<sourceCode;
76  // cout<<"wrote shader to file:"<<endl;
77 
78  // compile shader
79  const char* sourceString = sourceCode.c_str();
80  glShaderSource(id_, 1, &sourceString, NULL);
81  glCompileShader(id_);
82 
83  // check success
84  GLint compilationSuccess;
85  glGetShaderiv(id_, GL_COMPILE_STATUS, &compilationSuccess);
86 
87  if (compilationSuccess != GL_TRUE) {
88  GLF_THROW_EXCEPTION("Failed to compile shader: " <<
89  endl << GetInfoLog());
90  }
91  GLF_THROW_ON_OPENGL_ERROR;
92 }
93 
94 void glfShader::CreateFromFile(GLenum type, const std::string& fileName)
95 {
96  ifstream ifs(fileName.c_str());
97  if (!ifs) {
98  GLF_THROW_EXCEPTION("Failed to load shader source file: " << fileName);
99  }
100 
101  string source;
102  while (!ifs.eof()) {
103  string line;
104  getline(ifs, line);
105  source += line+"\r\n";
106  }
107 
108  return Create(type, source);
109 }
110 
111 string glfShader::GetInfoLog() const
112 {
113  BIASASSERT(id_ != 0);
114 
115  GLint logLength;
116  glGetShaderiv(id_, GL_INFO_LOG_LENGTH, &logLength);
117  char* infoLog = new char[logLength];
118  glGetShaderInfoLog(id_, logLength, NULL, infoLog);
119  std::string out = infoLog;
120  delete [] infoLog;
121 
122  GLF_THROW_ON_OPENGL_ERROR;
123 
124  return out;
125 }
126 
128 {
129  return id_;
130 }
void CreateFromFile(GLenum type, const std::string &fileName)
Creates the shader from GLSL source code loaded from a file.
Definition: glfShader.cpp:94
void Create(GLenum type)
Creates the shader without the GLSL source code, use other Create() methods to upload source afterwar...
Definition: glfShader.cpp:55
GLuint GetShaderID() const
Returns the OpenGL id of the shader.
Definition: glfShader.cpp:127
A GLSL vertex shader or fragment shader, which must be linked in a shader program.
Definition: glfShader.hh:39
std::string GetInfoLog() const
Returns the info log containing information about the compilation of the shader.
Definition: glfShader.cpp:111