Allolib  1.0
C++ Components For Interactive Multimedia
al_Graphics.hpp
1 #ifndef INCLUDE_AL_GRAPHICS_HPP
2 #define INCLUDE_AL_GRAPHICS_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37  File description:
38  A general high-level interface to graphics rendering
39 
40  File author(s):
41  Wesley Smith, 2010, wesley.hoke@gmail.com
42  Lance Putnam, 2010, putnam.lance@gmail.com
43  Graham Wakefield, 2010, grrrwaaa@gmail.com
44  Keehong Youn, 2017, younkeehong@gmail.com
45 
46 */
47 
48 #include "al/graphics/al_DefaultShaders.hpp"
49 #include "al/graphics/al_Light.hpp"
50 #include "al/graphics/al_OpenGL.hpp"
51 #include "al/graphics/al_RenderManager.hpp"
52 
53 namespace al {
54 
63 class Graphics : public RenderManager {
64  public:
65  enum class ColoringMode : unsigned int {
66  UNIFORM,
67  MESH,
68  TEXTURE,
69  MATERIAL,
70  CUSTOM
71  };
72 
73  virtual ~Graphics() {}
74 
78  inline void bufferToDraw(unsigned int buffer) { gl::bufferToDraw(buffer); }
79 
81  inline void blending(bool doBlend) { gl::blending(doBlend); }
90  inline void blendMode(unsigned int src, unsigned int dst, unsigned int eq) {
91  gl::blendMode(src, dst, eq);
92  }
94  inline void blendAdd() { gl::blendMode(GL_SRC_ALPHA, GL_ONE, GL_FUNC_ADD); }
96  inline void blendSub() {
97  gl::blendMode(GL_SRC_ALPHA, GL_ONE, GL_FUNC_REVERSE_SUBTRACT);
98  }
100  inline void blendScreen() {
101  gl::blendMode(GL_ONE, GL_ONE_MINUS_SRC_COLOR, GL_FUNC_ADD);
102  }
104  inline void blendMult() { gl::blendMode(GL_DST_COLOR, GL_ZERO, GL_FUNC_ADD); }
106  inline void blendTrans() {
107  gl::blendMode(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_FUNC_ADD);
108  }
109 
111  inline void depthTesting(bool testDepth) { gl::depthTesting(testDepth); }
113  inline void depthMask(bool maskDepth) { gl::depthMask(maskDepth); }
114 
116  inline void scissorTest(bool testScissor) { gl::scissorTest(testScissor); }
117  inline void scissorArea(int left, int bottom, int width, int height) {
118  gl::scissorArea(left, bottom, width, height);
119  }
120 
122  inline void culling(bool doCulling) { gl::culling(doCulling); }
124  inline void cullFace(unsigned int face) { gl::cullFace(face); }
125  inline void cullFaceBack() { gl::cullFace(GL_BACK); }
126  inline void cullFaceFront() { gl::cullFace(GL_FRONT); }
127  inline void cullFaceBoth() { gl::cullFace(GL_FRONT_AND_BACK); }
128 
132  inline void polygonMode(unsigned int mode) { gl::polygonMode(mode); }
134  inline void polygonPoint() { gl::polygonMode(GL_POINT); }
136  inline void polygonLine() { gl::polygonMode(GL_LINE); }
138  inline void polygonFill() { gl::polygonMode(GL_FILL); }
139 
141  inline void colorMask(bool r, bool g, bool b, bool a) {
142  gl::colorMask(r, g, b, a);
143  }
145  inline void colorMask(bool b) { gl::colorMask(b); }
146 
147  inline void pointSize(float size) { gl::pointSize(size); }
148  inline void lineWidth(float size) { gl::lineWidth(size); }
149 
150  // clears the default color buffer(buffer 0) with the provided color
151  inline void clearColor(float r, float g, float b, float a = 1.f) {
152  gl::clearColor(r, g, b, a);
153  }
154  // clears color buffer using al::Color class
155  inline void clearColor(Color const &c) { gl::clearColor(c.r, c.g, c.b, c.a); }
156 
157  // clears the depth buffer with the provided depth value
158  inline void clearDepth(float d = 1.f) { gl::clearDepth(d); }
159 
160  // clears the specified color buffer with the provided color
161  inline void clearBuffer(int buffer, float r, float g, float b,
162  float a = 1.f) {
163  gl::clearBuffer(buffer, r, g, b, a);
164  }
165 
166  // clears color & depth buffer with the provided color, and depth 1
167  inline void clear(float r, float g, float b, float a = 1.f) {
168  gl::clearColor(r, g, b, a);
169  gl::clearDepth(1.f);
170  }
171  // clears color & depth buffer with grayscale values, and depth 1
172  inline void clear(float grayscale = 0.f, float a = 1.f) {
173  gl::clearColor(grayscale, grayscale, grayscale, a);
174  gl::clearDepth(1.f);
175  }
176  // clears color & depth buffer using al::Color class, and depth 1
177  inline void clear(Color const &c) { clear(c.r, c.g, c.b, c.a); }
178 
179  // extended, predefined render managing --------------------------------------
180  void init();
181 
182  // set overall tint, regardless of rendering mode
183  void tint(Color const &c);
184  void tint(float r, float g, float b, float a = 1.f);
185  void tint(float grayscale, float a = 1.f);
186 
187  // set to uniform color mode, using previously set uniform color
188  void color();
189  // set to uniform color mode, using provided color
190  void color(float r, float g, float b, float a = 1.f);
191  // set to uniform color mode, using provided color
192  void color(Color const &c);
193  // set to uniform color mode, using provided color
194  void color(float grayscale, float a = 1.f);
195 
196  // set to mesh color mode, using mesh's color array
197  void meshColor();
198 
199  // set to texture mode, using texture bound by user at location=0
200  void texture();
201 
202  // set to material mode, using previously set material
203  // if lighting is disabled, ColoringMode::COLOR will be used
204  void material();
205 
206  // set to material mode, using provied material
207  void material(Material const &m);
208 
209  // enable/disable lighting
210  void lighting(bool b);
211 
212  // set number of lights used
213  void numLight(int n);
214 
215  // turn on/off light at specified index.
216  // if light is off it still gets calculated in the shader but zero is
217  // multiplied as intensity. to prevent calculation, reorder lights and call
218  // `numLight` function to change shader to one with less number of lights
219  void enableLight(int idx);
220  void disableLight(int idx);
221  void toggleLight(int idx);
222 
223  // does not enable lighting, call lighting(true) to enable lighting
224  void light(Light const &l, int idx = 0);
225 
226  void quad(Texture &tex, float x, float y, float w, float h);
227  void quadViewport(Texture &tex, float x = -1, float y = -1, float w = 2,
228  float h = 2);
229 
230  // use user made non-default shader. with this call user should set uniforms
231  // manually (but stiil use allolib interface for mesh and model/view/proj
232  // matrices)
233  void shader(ShaderProgram &s);
234  ShaderProgram &shader();
235  ShaderProgram *shaderPtr();
236 
237  using RenderManager::camera; // makes camera(Viewpoint::SpecialType v)
238  // accessible
239  void camera(Viewpoint const &v) override;
240 
241  void send_lighting_uniforms(ShaderProgram &s,
242  lighting_shader_uniforms const &u);
243  void update() override;
244 
245  // to pass to the shader, combined with mLens.eyeSep(),
246  // will set the uniform "eye_sep"
247  static const float LEFT_EYE;
248  static const float RIGHT_EYE;
249  static const float MONO_EYE;
250 
251  void eye(float e);
252  float eye() { return mEye; }
253 
254  Lens const &lens() const { return mLens; }
255  Lens &lens();
256  void lens(Lens const &l);
257 
258  void omni(bool b);
259  bool omni();
260 
261  private:
262  bool initialized = false;
263 
264  Color mColor{1, 1, 1, 1};
265  Color mTint{1, 1, 1, 1};
266 
267  Graphics::ColoringMode mColoringMode = ColoringMode::UNIFORM;
268  bool mRenderModeChanged = true;
269  bool mUniformChanged = true;
270  bool mLightingEnabled = false;
271 
272  ShaderProgram mesh_shader;
273  ShaderProgram color_shader;
274  ShaderProgram tex_shader;
275 
276  int color_location = 0;
277  int color_tint_location = 0;
278  int mesh_tint_location = 0;
279  int tex_tint_location = 0;
280 
281  Material mMaterial;
282  Light mLights[al_max_num_lights()];
283  bool mLightOn[al_max_num_lights()];
284  int num_lights = 1;
285 
286  ShaderProgram lighting_color_shader[al_max_num_lights()];
287  ShaderProgram lighting_mesh_shader[al_max_num_lights()];
288  ShaderProgram lighting_tex_shader[al_max_num_lights()];
289  ShaderProgram lighting_material_shader[al_max_num_lights()];
290 
291  int lighting_color_location[al_max_num_lights()];
292  int lighting_color_tint_location[al_max_num_lights()];
293  int lighting_mesh_tint_location[al_max_num_lights()];
294  int lighting_tex_tint_location[al_max_num_lights()];
295  int lighting_material_tint_location[al_max_num_lights()];
296 
297  lighting_shader_uniforms lighting_color_uniforms[al_max_num_lights()];
298  lighting_shader_uniforms lighting_mesh_uniforms[al_max_num_lights()];
299  lighting_shader_uniforms lighting_tex_uniforms[al_max_num_lights()];
300  lighting_shader_uniforms lighting_material_uniforms[al_max_num_lights()];
301 
302  bool is_omni = false;
303 
304  ShaderProgram omni_mesh_shader;
305  ShaderProgram omni_color_shader;
306  ShaderProgram omni_tex_shader;
307 
308  int omni_color_location = 0;
309  int omni_color_tint_location = 0;
310  int omni_mesh_tint_location = 0;
311  int omni_tex_tint_location = 0;
312 
313  ShaderProgram omni_lighting_color_shader[al_max_num_lights()];
314  ShaderProgram omni_lighting_mesh_shader[al_max_num_lights()];
315  ShaderProgram omni_lighting_tex_shader[al_max_num_lights()];
316  ShaderProgram omni_lighting_material_shader[al_max_num_lights()];
317 
318  int omni_lighting_color_location[al_max_num_lights()];
319  int omni_lighting_color_tint_location[al_max_num_lights()];
320  int omni_lighting_mesh_tint_location[al_max_num_lights()];
321  int omni_lighting_tex_tint_location[al_max_num_lights()];
322  int omni_lighting_material_tint_location[al_max_num_lights()];
323 
324  lighting_shader_uniforms omni_lighting_color_uniforms[al_max_num_lights()];
325  lighting_shader_uniforms omni_lighting_mesh_uniforms[al_max_num_lights()];
326  lighting_shader_uniforms omni_lighting_tex_uniforms[al_max_num_lights()];
327  lighting_shader_uniforms omni_lighting_material_uniforms[al_max_num_lights()];
328 
329  Lens mLens;
330  float mEye = 0.0f;
331 };
332 
333 } // namespace al
334 #endif
Interface for loading fonts and rendering text.
Definition: al_Graphics.hpp:63
void blendScreen()
Set blend mode to screen (symmetric multiplicative lighten)
void blendAdd()
Set blend mode to additive (symmetric additive lighten)
Definition: al_Graphics.hpp:94
void polygonMode(unsigned int mode)
void blending(bool doBlend)
Turn blending on/off.
Definition: al_Graphics.hpp:81
void polygonLine()
Draw only edges of polygons with lines.
void depthMask(bool maskDepth)
Turn the depth mask on/off.
void polygonFill()
Draw filled polygons.
void cullFace(unsigned int face)
face=[GL_FRONT, GL_BACK, GL_FRONT_AND_BACK], initial: GL_BACK
void colorMask(bool r, bool g, bool b, bool a)
Turn color mask RGBA components on/off.
void polygonPoint()
Draw only points of vertices.
void blendMult()
Set blend mode to multiplicative (symmetric multiplicative darken)
void colorMask(bool b)
Turn color mask on/off (all RGBA components)
void scissorTest(bool testScissor)
Turn scissor testing on/off.
void culling(bool doCulling)
Turn face culling on/off.
void blendSub()
Set blend mode to subtractive (symmetric additive darken)
Definition: al_Graphics.hpp:96
void blendMode(unsigned int src, unsigned int dst, unsigned int eq)
Definition: al_Graphics.hpp:90
void blendTrans()
Set blend mode to transparent (asymmetric)
void bufferToDraw(unsigned int buffer)
Definition: al_Graphics.hpp:78
void depthTesting(bool testDepth)
Turn depth testing on/off.
RenderManager class.
Definition: al_App.hpp:23