CUGL
Cornell University Game Library
CUShader.h
1 //
2 // CUShader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the abstract base class for our shader classes.
6 // It supports compilations and has diagnostic tools for errors. It is not
7 // useful for rendering, because there is no way to get the data there.
8 //
9 // Because it is abstract, it has only a basic constructor. It has no
10 // initializers or allocator.
11 //
12 // CUGL zlib License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 6/23/16
33 
34 #ifndef __CU_SHADER_H__
35 #define __CU_SHADER_H__
36 
37 #include "../math/CUMathBase.h"
38 #include <string>
39 
40 // A stringification trick to define shaders easily
41 // Note the version lockdown for non-OpenGLES systems
42 #if CU_GL_PLATFORM == CU_GL_OPENGLES
43  #define SHADER(A) "#version 300 es\n" #A
44 #else
45  #define SHADER(A) "#version 140\n" #A
46 #endif
47 
48 namespace cugl {
49 
62 class Shader {
63 #pragma mark Values
64 protected:
66  GLuint _program;
68  GLuint _vertShader;
70  GLuint _fragShader;
72  const char* _vertSource;
74  const char* _fragSource;
76  bool _active;
77 
78 #pragma mark -
79 #pragma mark Constructors
80 public:
86  Shader() :_program(0), _vertShader(0), _fragShader(0),
87  _vertSource(nullptr), _fragSource(nullptr),
88  _active(false) {};
89 
93  ~Shader() { dispose(); }
94 
100  virtual void dispose();
101 
102 #pragma mark -
103 #pragma mark Attributes
104 
111  std::string getVertSource() const {
112  return (_vertSource ? std::string(_vertSource) : "");
113  }
114 
122  std::string getFragSource() const {
123  return (_fragSource ? std::string(_fragSource) : "");
124  }
125 
131  bool isReady() const { return _program != 0; }
132 
141  bool isActive() const { return _active; }
142 
150  GLuint getProgram() const { return _program; }
151 
152 #pragma mark -
153 #pragma mark Rendering
154 
159  virtual void bind();
160 
166  virtual void unbind();
167 
168 #pragma mark -
169 #pragma mark Compilation
170 protected:
182  virtual bool compile();
183 
194  static bool validateShader(GLuint shader, const char* type);
195 
203  static void logShaderError(GLuint shader);
204 
212  static void logProgramError(GLuint shader);
213 };
214 
215 }
216 
217 #endif /* __CU_SHADER_H__ */
std::string getVertSource() const
Definition: CUShader.h:111
static bool validateShader(GLuint shader, const char *type)
virtual void unbind()
const char * _fragSource
Definition: CUShader.h:74
~Shader()
Definition: CUShader.h:93
static void logShaderError(GLuint shader)
GLuint _vertShader
Definition: CUShader.h:68
bool isReady() const
Definition: CUShader.h:131
GLuint getProgram() const
Definition: CUShader.h:150
bool isActive() const
Definition: CUShader.h:141
Definition: CUShader.h:62
virtual void dispose()
Shader()
Definition: CUShader.h:86
static void logProgramError(GLuint shader)
GLuint _program
Definition: CUShader.h:66
GLuint _fragShader
Definition: CUShader.h:70
std::string getFragSource() const
Definition: CUShader.h:122
virtual void bind()
const char * _vertSource
Definition: CUShader.h:72
Definition: CUAnimationNode.h:52
virtual bool compile()
bool _active
Definition: CUShader.h:76