NGL  6.5
The NCCA Graphics Library
Shader.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
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 3 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 <http://www.gnu.org/licenses/>.
16 */
17 
18 //#include <QFile>
19 #include <fstream>
20 #include "Shader.h"
21 #include <string> // needed for windows build as get error otherwise
22 
23 //----------------------------------------------------------------------------------------------------------------------
26 //----------------------------------------------------------------------------------------------------------------------
27 
28 namespace ngl
29 {
30 //----------------------------------------------------------------------------------------------------------------------
31 void printInfoLog( const GLuint &_obj)
32 {
33  GLint infologLength = 0;
34  GLint charsWritten = 0;
35  char *infoLog;
36 
37  glGetShaderiv(_obj, GL_INFO_LOG_LENGTH,&infologLength);
38  //std::cerr<<"info log length "<<infologLength<<"\n";
39  if(infologLength > 0)
40  {
41  infoLog = new char[infologLength];
42  glGetShaderInfoLog(_obj, infologLength, &charsWritten, infoLog);
43 
44  std::cerr<<infoLog<<std::endl;
45  delete [] infoLog;
46 
47  }
48 
49 }
50 
51 
52 Shader::Shader( std::string _name, ShaderType _type ) noexcept
53 {
54  m_name=_name;
55  m_shaderType = _type;
56  m_debugState = true;
57  m_compiled=false;
58 
59  switch (_type)
60  {
61  case ShaderType::VERTEX : { m_shaderHandle = glCreateShader(GL_VERTEX_SHADER); break; }
62  case ShaderType::FRAGMENT : { m_shaderHandle = glCreateShader(GL_FRAGMENT_SHADER); break; }
63  #ifndef USINGIOS_
64  case ShaderType::GEOMETRY : { m_shaderHandle = glCreateShader(GL_GEOMETRY_SHADER); break; }
65  case ShaderType::TESSCONTROL : { m_shaderHandle =glCreateShader(GL_TESS_CONTROL_SHADER); break; }
66  case ShaderType::TESSEVAL : { m_shaderHandle =glCreateShader(GL_TESS_EVALUATION_SHADER); break; }
67  case ShaderType::COMPUTE : { m_shaderHandle =glCreateShader(GL_COMPUTE_SHADER); break; }
68  #endif
69  case ShaderType::NONE :{;}
70  }
71  m_compiled = false;
72  m_refCount=0;
73 }
75 {
76  std::cerr<<"removing shader "<<m_name<<"\n";
78 }
79 
80 void Shader::compile() noexcept
81 {
82  if (m_source.empty() )
83  {
84  std::cerr<<"Warning no shader source loaded\n";
85  return;
86  }
87 
89  if(m_debugState==true)
90  {
91  GLint infologLength = 0;
92  std::cerr <<"Compiling Shader "<<m_name<<"\n";
94  if( infologLength == GL_FALSE)
95  {
96  std::cerr<<"Shader compile failed or had warnings \n";
98  exit(EXIT_FAILURE);
99  }
100  }
101  m_compiled=true;
102 }
103 
104 
105 void Shader::load( std::string _name ) noexcept
106 {
107  // see if we already have some source attached
108  if(m_source.empty())
109  {
110  std::cerr<<"deleting existing source code\n";
111  m_source.clear();
112  }
113  std::ifstream shaderSource(_name.c_str());
114  if (!shaderSource.is_open())
115  {
116  std::cerr<<"File not found "<<_name.c_str()<<"\n";
117  exit(EXIT_FAILURE);
118  }
119  // now read in the data
120  m_source = std::string((std::istreambuf_iterator<char>(shaderSource)), std::istreambuf_iterator<char>());
121  shaderSource.close();
122  // glShaderSource needs null terminated const char *
123  m_source+='\0';
124  const char* data=m_source.c_str();
125  glShaderSource(m_shaderHandle , 1, &data,NULL);
126  m_compiled=false;
127 
128  if (m_debugState == true)
129  {
131  }
132 }
133 
134 void Shader::loadFromString(const std::string &_string ) noexcept
135 {
136  // see if we already have some source attached
137  if(m_source.size()!=0)
138  {
139  std::cerr<<"deleting existing source code\n";
140  m_source.clear();
141  }
142 
143  // we need this for the check in the compile bit
144  m_source=_string;
145  const char* data=m_source.c_str();
146 
147  glShaderSource(m_shaderHandle , 1, &data,NULL);
148  m_compiled=false;
149  if (m_debugState == true)
150  {
152  }
153 }
154 
155 } // end ngl namespace
unsigned int GLuint
Definition: glew.h:280
#define glGetShaderiv
Definition: glew.h:1930
#define glCreateShader
Definition: glew.h:1915
Shader(std::string _name, ShaderType _type) noexcept
ctor
Definition: Shader.cpp:52
bool m_debugState
flag to indicate the debug state
Definition: Shader.h:119
#define GL_FALSE
Definition: glew.h:314
#define GL_TESS_EVALUATION_SHADER
Definition: glew.h:6686
#define GL_GEOMETRY_SHADER
Definition: glew.h:2354
~Shader()
dtor, will clean up shader source and remove shader from GL
Definition: Shader.cpp:74
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1382
GLsizei GLsizei GLchar * infoLog
Definition: glew.h:1832
implementation files for RibExport class
Definition: AABB.cpp:22
void loadFromString(const std::string &_string) noexcept
Definition: Shader.cpp:134
#define GL_COMPUTE_SHADER
Definition: glew.h:3834
#define glGetShaderInfoLog
Definition: glew.h:1928
std::string m_source
the actual source code for this shader (used for debug and initial loading)
Definition: Shader.h:102
ShaderType
Definition: Shader.h:30
#define GL_COMPILE_STATUS
Definition: glew.h:1796
#define GL_FRAGMENT_SHADER
Definition: glew.h:1768
std::string m_name
the text name of this Shader used in the search for shader
Definition: Shader.h:98
GLuint m_shaderHandle
the GL handle for this shader object used in linking etc
Definition: Shader.h:115
#define GL_VERTEX_SHADER
Definition: glew.h:1769
void compile() noexcept
compile the current shader will check to see if source is attached and issue warning if not ...
Definition: Shader.cpp:80
int GLint
Definition: glew.h:281
void printInfoLog(const GLuint &_obj)
Definition: Shader.cpp:31
#define glDeleteShader
Definition: glew.h:1917
#define GL_TESS_CONTROL_SHADER
Definition: glew.h:6687
#define GL_INFO_LOG_LENGTH
Definition: glew.h:1799
bool m_compiled
flag to indicate if the shader has been compiled this will get channged on re-load of source to false...
Definition: Shader.h:107
GLsizei const GLchar *const * string
Definition: glew.h:1847
#define glShaderSource
Definition: glew.h:1941
#define glCompileShader
Definition: glew.h:1913
void load(std::string _name) noexcept
load in shader source and attach it to the shader object if source is already loaded it will re-load ...
Definition: Shader.cpp:105