CMSC23700 Common Code Library
Support code for CS23700 programming projects
cs237-shader.hxx
Go to the documentation of this file.
1 
8 /*
9  * COPYRIGHT (c) 2013 John Reppy (http://cs.uchicago.edu/~jhr)
10  * All rights reserved.
11  */
12 
13 #ifndef _CS237_SHADER_HXX_
14 #define _CS237_SHADER_HXX_
15 
16 #ifndef _CS237_HXX_
17 #error "cs237-shader.hxx should not be included directly"
18 #endif
19 
20 namespace cs237 {
21 
22  namespace __detail {
23 
24  class Shader {
25  public:
26  GLuint Id() const { return this->_obj->_id; }
27 
28  protected:
29  Shader (GLenum kind);
30 
31  bool LoadFromFile (const char *file);
32  bool LoadFromString (const char **prog);
33 
34  private:
35  // wrapper class around GL Shader ID
36  struct Obj {
37  GLuint _id;
38  Obj (GLuint id) : _id(id) { }
39  ~Obj();
40  };
41 
42  std::shared_ptr<Obj> _obj;
43  GLenum _kind;
44  };
45 
46  } // namespace __detail
47 
48  class VertexShader : public __detail::Shader {
49  public:
50 
58  VertexShader (const char *file);
59 
68  VertexShader (const char **prog);
69 
70  }; /* VertexShader */
71 
73  public:
74 
82  GeometryShader (const char *file);
83 
92  GeometryShader (const char **prog);
93 
94  }; /* GeometryShader */
95 
97  public:
98 
106  FragmentShader (const char *file);
107 
116  FragmentShader (const char **prog);
117 
118  }; /* FragmentShader */
119 
121  public:
122 
123  ShaderProgram (VertexShader const &vsh, FragmentShader const &fsh);
124  ShaderProgram (VertexShader const &vsh, GeometryShader const &gsh, FragmentShader const &fsh);
125 
128  void Use ();
129 
134  bool hasAttribute (const char *name);
135 
140  int AttributeLocation (const char *name);
141 
146  bool hasUniform (const char *name);
147 
152  int UniformLocation (const char *name);
153 
155  GLuint Id () { return this->_obj->_id; }
156 
157  private:
158 
159  // wrapper class around GL Shader Program ID
160  struct Obj {
161  GLuint _id;
162  Obj (GLuint id) : _id(id) { }
163  ~Obj();
164  };
165 
166  std::shared_ptr<Obj> _obj;
167 
168  void _Link ();
169 
170  }; // ShaderProgram
171 
172 } /* namespace cs237 */
173 
174 #endif /* !_CS237_SHADER_HXX_ */
bool hasAttribute(const char *name)
does the shader have the given attribute variable?
Definition: cs237-shader.inl:31
bool hasUniform(const char *name)
does the shader have the given uniform variable?
Definition: cs237-shader.inl:36
int AttributeLocation(const char *name)
return the location of the given attribute in the given program.
ShaderProgram(VertexShader const &vsh, FragmentShader const &fsh)
bool LoadFromString(const char **prog)
int UniformLocation(const char *name)
return the location of the given uniform variable in the given program.
GLuint Id()
return the underlying OpenGL shader program ID
Definition: cs237-shader.hxx:155
VertexShader(const char *file)
load a vertex shader from a file.
Definition: cs237-shader.hxx:72
void Use()
use the given shader program for rendering.
Definition: cs237-shader.inl:26
Definition: cs237-shader.hxx:96
Definition: cs237-shader.hxx:48
GeometryShader(const char *file)
load a fragment shader from a file.
GLuint Id() const
Definition: cs237-shader.hxx:26
Definition: cs237-aabb.hxx:18
Definition: cs237-shader.hxx:120
Definition: cs237-shader.hxx:24
bool LoadFromFile(const char *file)
FragmentShader(const char *file)
load a fragment shader from a file.