CUGL 1.3
Cornell University Game Library
CUSpriteShader.h
1 //
2 // CUSpriteShader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the default shader for a sprite batch. You may replace
6 // the shader code, but all shaders must support the attributes and uniforms
7 // listed below.
8 //
9 // This class uses our standard shared-pointer architecture.
10 //
11 // 1. The constructor does not perform any initialization; it just sets all
12 // attributes to their defaults.
13 //
14 // 2. All initialization takes place via init methods, which can fail if an
15 // object is initialized more than once.
16 //
17 // 3. All allocation takes place via static constructors which return a shared
18 // pointer.
19 //
20 // CUGL MIT License:
21 // This software is provided 'as-is', without any express or implied
22 // warranty. In no event will the authors be held liable for any damages
23 // arising from the use of this software.
24 //
25 // Permission is granted to anyone to use this software for any purpose,
26 // including commercial applications, and to alter it and redistribute it
27 // freely, subject to the following restrictions:
28 //
29 // 1. The origin of this software must not be misrepresented; you must not
30 // claim that you wrote the original software. If you use this software
31 // in a product, an acknowledgment in the product documentation would be
32 // appreciated but is not required.
33 //
34 // 2. Altered source versions must be plainly marked as such, and must not
35 // be misrepresented as being the original software.
36 //
37 // 3. This notice may not be removed or altered from any source distribution.
38 //
39 // Author: Walker White
40 // Version: 6/23/16
41 
42 #ifndef __CU_SPRITE_SHADER_H__
43 #define __CU_SPRITE_SHADER_H__
44 
45 #include <cugl/renderer/CUShader.h>
46 #include <cugl/renderer/CUTexture.h>
47 #include <cugl/math/CUMat4.h>
48 
49 namespace cugl {
50 
74 class SpriteShader : public Shader {
75 #pragma mark Values
76 private:
78  GLint _aPosition;
80  GLint _aColor;
82  GLint _aTexCoord;
84  GLint _uPerspective;
86  GLint _uTexture;
87 
89  Mat4 _mPerspective;
90 
92  std::shared_ptr<Texture> _mTexture;
93 
94 #pragma mark -
95 #pragma mark Constructors
96 public:
102  SpriteShader() : Shader(), _aPosition(-1), _aColor(-1), _aTexCoord(-1),
103  _uPerspective(-1), _uTexture(-1) { }
104 
109 
115  void dispose() override;
116 
127  bool init();
128 
142  bool init(std::string vsource, std::string fsource) {
143  return init(vsource.c_str(),fsource.c_str());
144  }
145 
159  bool init(const char* vsource, const char* fsource);
160 
161 #pragma mark -
162 #pragma mark Static Constructors
163 
173  static std::shared_ptr<SpriteShader> alloc() {
174  std::shared_ptr<SpriteShader> result = std::make_shared<SpriteShader>();
175  return (result->init() ? result : nullptr);
176  }
177 
191  static std::shared_ptr<SpriteShader> alloc(std::string vsource, std::string fsource) {
192  std::shared_ptr<SpriteShader> result = std::make_shared<SpriteShader>();
193  return (result->init(vsource, fsource) ? result : nullptr);
194  }
195 
209  static std::shared_ptr<SpriteShader> alloc(const char* vsource, const char* fsource) {
210  std::shared_ptr<SpriteShader> result = std::make_shared<SpriteShader>();
211  return (result->init(vsource, fsource) ? result : nullptr);
212  }
213 
214 #pragma mark -
215 #pragma mark Attributes
216 
223  GLint getPositionAttr() const { return _aPosition; }
224 
232  GLint getColorAttr() const { return _aColor; }
233 
241  GLint getTexCoodAttr() const { return _aTexCoord; }
242 
250  GLint getPerspectiveUni() const { return _uPerspective; }
251 
259  GLint getTextureUni() const { return _uTexture; }
260 
266  void setPerspective(const Mat4& matrix);
267 
273  const Mat4& getPerspective() { return _mPerspective; }
274 
280  void setTexture(const std::shared_ptr<Texture>& texture);
281 
287  std::shared_ptr<Texture> getTexture() { return _mTexture; }
288 
294  const std::shared_ptr<Texture>& getTexture() const { return _mTexture; }
295 
296 #pragma mark -
297 #pragma mark Rendering
298 
307  void attach(GLuint vArray, GLuint vBuffer);
308 
314  void bind() override;
315 
321  void unbind() override;
322 
323 #pragma mark -
324 #pragma mark Compilation
325 protected:
337  bool compile() override;
338 
349  bool validateVariable(GLint variable, const char* name);
350 
351 };
352 
353 }
354 
355 #endif /* __CU_SPRITE_SHADER_H__ */
cugl::SpriteShader::getColorAttr
GLint getColorAttr() const
Definition: CUSpriteShader.h:232
cugl::SpriteShader::getPositionAttr
GLint getPositionAttr() const
Definition: CUSpriteShader.h:223
cugl::SpriteShader::validateVariable
bool validateVariable(GLint variable, const char *name)
cugl::SpriteShader::init
bool init()
cugl::SpriteShader::alloc
static std::shared_ptr< SpriteShader > alloc(std::string vsource, std::string fsource)
Definition: CUSpriteShader.h:191
cugl::SpriteShader::compile
bool compile() override
cugl::SpriteShader::getPerspective
const Mat4 & getPerspective()
Definition: CUSpriteShader.h:273
cugl::SpriteShader::alloc
static std::shared_ptr< SpriteShader > alloc(const char *vsource, const char *fsource)
Definition: CUSpriteShader.h:209
cugl::Mat4
Definition: CUMat4.h:83
cugl::SpriteShader::bind
void bind() override
cugl::SpriteShader::getPerspectiveUni
GLint getPerspectiveUni() const
Definition: CUSpriteShader.h:250
cugl::SpriteShader::init
bool init(std::string vsource, std::string fsource)
Definition: CUSpriteShader.h:142
cugl::SpriteShader::setTexture
void setTexture(const std::shared_ptr< Texture > &texture)
cugl::SpriteShader::attach
void attach(GLuint vArray, GLuint vBuffer)
cugl::SpriteShader::SpriteShader
SpriteShader()
Definition: CUSpriteShader.h:102
cugl::SpriteShader::getTexture
const std::shared_ptr< Texture > & getTexture() const
Definition: CUSpriteShader.h:294
cugl::SpriteShader::~SpriteShader
~SpriteShader()
Definition: CUSpriteShader.h:108
cugl::SpriteShader
Definition: CUSpriteShader.h:74
cugl::Shader
Definition: CUShader.h:62
cugl::SpriteShader::dispose
void dispose() override
cugl::SpriteShader::getTexCoodAttr
GLint getTexCoodAttr() const
Definition: CUSpriteShader.h:241
cugl::SpriteShader::alloc
static std::shared_ptr< SpriteShader > alloc()
Definition: CUSpriteShader.h:173
cugl::SpriteShader::getTextureUni
GLint getTextureUni() const
Definition: CUSpriteShader.h:259
cugl::SpriteShader::unbind
void unbind() override
cugl::SpriteShader::setPerspective
void setPerspective(const Mat4 &matrix)
cugl::SpriteShader::getTexture
std::shared_ptr< Texture > getTexture()
Definition: CUSpriteShader.h:287