Allolib  1.0
C++ Components For Interactive Multimedia
al_RenderManager.hpp
1 #ifndef INCLUDE_AL_RENDER_MANAGER_HPP
2 #define INCLUDE_AL_RENDER_MANAGER_HPP
3 
4 #include <unordered_map>
5 
6 #include "al/graphics/al_EasyFBO.hpp"
7 #include "al/graphics/al_EasyVAO.hpp"
8 #include "al/graphics/al_FBO.hpp"
9 #include "al/graphics/al_Shader.hpp"
10 #include "al/graphics/al_VAOMesh.hpp"
11 #include "al/graphics/al_Viewpoint.hpp"
12 #include "al/math/al_Matrix4.hpp"
13 #include "al/math/al_Quat.hpp"
14 #include "al/math/al_Vec.hpp"
15 
16 namespace al {
17 
22 class MatrixStack {
23 public:
24  MatrixStack();
25  void push();
26  void pop();
27  void mult(Matrix4f const &m);
28  void set(const Matrix4f &m);
29  void setIdentity();
30  void pop_all();
31  Matrix4f get() const;
32 
33 private:
34  std::vector<Matrix4f> stack;
35 };
36 
42 public:
43  ViewportStack();
44  void push();
45  void pop();
46  Viewport get() const;
47  void set(const Viewport &m);
48  void set(int left, int bottom, int width, int height);
49 
50 private:
51  std::vector<Viewport> stack;
52 };
53 
54 class FBOStack {
55 public:
56  FBOStack();
57  void push();
58  void pop();
59  unsigned int get() const;
60  void set(unsigned int id);
61 
62 private:
63  std::vector<unsigned int> stack;
64 };
65 
99 public:
101  void multModelMatrix(const Matrix4f &m) {
102  mModelStack.mult(m);
103  mMatChanged = true;
104  }
105  void multViewMatrix(const Matrix4f &m) {
106  mViewStack.mult(m);
107  mMatChanged = true;
108  }
109  void multProjMatrix(const Matrix4f &m) {
110  mProjStack.mult(m);
111  mMatChanged = true;
112  }
113 
114  Matrix4f modelMatrix() const { return mModelStack.get(); }
115  Matrix4f viewMatrix() const { return mViewStack.get(); }
116  Matrix4f projMatrix() const { return mProjStack.get(); }
117 
118  void modelMatrix(const Matrix4f &m) {
119  mModelStack.set(m);
120  mMatChanged = true;
121  }
122  void viewMatrix(const Matrix4f &m) {
123  mViewStack.set(m);
124  mMatChanged = true;
125  }
126  void projMatrix(const Matrix4f &m) {
127  mProjStack.set(m);
128  mMatChanged = true;
129  }
130 
131  void pushModelMatrix() { mModelStack.push(); }
132  void pushViewMatrix() { mViewStack.push(); }
133  void pushProjMatrix() { mProjStack.push(); }
134 
135  void pushModelMatrix(const Matrix4f &m) {
136  mModelStack.push();
137  modelMatrix(m);
138  }
139  void pushViewMatrix(const Matrix4f &m) {
140  mViewStack.push();
141  viewMatrix(m);
142  }
143  void pushProjMatrix(const Matrix4f &m) {
144  mProjStack.push();
145  projMatrix(m);
146  }
147 
148  void popModelMatrix() {
149  mModelStack.pop();
150  mMatChanged = true;
151  }
152  void popViewMatrix() {
153  mViewStack.pop();
154  mMatChanged = true;
155  }
156  void popProjMatrix() {
157  mProjStack.pop();
158  mMatChanged = true;
159  }
160 
161  void resetModelMatrixStack() {
162  mModelStack.pop_all();
163  mMatChanged = true;
164  }
165  void resetViewMatrixStack() {
166  mViewStack.pop_all();
167  mMatChanged = true;
168  }
169  void resetProjMatrixStack() {
170  mProjStack.pop_all();
171  mMatChanged = true;
172  }
173  void resetMatrixStack() {
174  resetModelMatrixStack();
175  resetViewMatrixStack();
176  resetProjMatrixStack();
177  }
178 
180  void pushMatrix() { pushModelMatrix(); }
181  void pushMatrix(const Matrix4f &m) { pushModelMatrix(m); }
182 
184  void popMatrix() { popModelMatrix(); }
185 
187  void loadIdentity() { mModelStack.setIdentity(); }
188 
190  void translate(float x, float y, float z = 0.);
192  template <class T> void translate(const Vec<3, T> &v) {
193  translate(float(v[0]), float(v[1]), float(v[2]));
194  }
196  template <class T> void translate(const Vec<2, T> &v) {
197  translate(v[0], v[1]);
198  }
199 
205  void rotate(float angle, float x = 0., float y = 0., float z = 1.);
207  void rotate(const Quatf &q);
209  void rotate(const Quatd &q);
213  template <class T> void rotate(float angle, const Vec<3, T> &axis) {
214  rotate(angle, axis[0], axis[1], axis[2]);
215  }
216 
218  void scale(float x, float y, float z = 1.);
220  void scale(float s) { scale(s, s, s); }
222  template <class T> void scale(const Vec<3, T> &v) { scale(v[0], v[1], v[2]); }
224  template <class T> void scale(const Vec<2, T> &v) { scale(v[0], v[1]); }
225 
227  void viewport(int left, int bottom, int width, int height);
228  void viewport(const Viewport &v) { viewport(v.l, v.b, v.w, v.h); }
229  Viewport viewport() { return mViewportStack.get(); }
230  void pushViewport();
231  void popViewport();
232  void pushViewport(int l, int b, int w, int h) {
233  pushViewport();
234  viewport(l, b, w, h);
235  }
236  void pushViewport(const Viewport &v) {
237  pushViewport();
238  viewport(v);
239  }
240  void pushViewport(int w, int h) {
241  pushViewport();
242  viewport(0, 0, w, h);
243  }
244 
245  void framebuffer(EasyFBO &easyFBO) { framebuffer(easyFBO.fbo().id()); }
246  void framebuffer(FBO &fbo) { framebuffer(fbo.id()); }
247  void framebuffer(unsigned int id);
248  // static unsigned int framebuffer() { return mFBOID; }
249  unsigned int framebuffer() { return mFBOStack.get(); }
250  void pushFramebuffer();
251  void popFramebuffer();
252  void pushFramebuffer(EasyFBO &f) {
253  pushFramebuffer();
254  framebuffer(f);
255  }
256  void pushFramebuffer(FBO &f) {
257  pushFramebuffer();
258  framebuffer(f);
259  }
260  void pushFramebuffer(unsigned int f) {
261  pushFramebuffer();
262  framebuffer(f);
263  }
264 
265  void shader(ShaderProgram &s);
266  ShaderProgram &shader() { return *mShaderPtr; }
267  ShaderProgram *shaderPtr() { return mShaderPtr; }
268 
269  virtual void camera(Viewpoint const &v);
270  virtual void camera(Viewpoint::SpecialType v);
271  void pushCamera();
272  void popCamera();
273  void pushCamera(Viewpoint const &v) {
274  pushCamera();
275  camera(v);
276  }
277  void pushCamera(Viewpoint::SpecialType v) {
278  pushCamera();
279  camera(v);
280  }
281 
282  virtual void update();
283  void draw(VAOMesh &mesh);
284  void draw(EasyVAO &vao);
285  void draw(const Mesh &mesh);
286  void draw(Mesh &&mesh);
287 
288 protected:
289  ShaderProgram *mShaderPtr = nullptr;
290  std::unordered_map<unsigned int, int> modelviewLocs;
291  std::unordered_map<unsigned int, int> projLocs;
292  bool mShaderChanged = false;
293 
294  // let matrix stack be local to objects
295  MatrixStack mViewStack;
296  MatrixStack mProjStack;
297  MatrixStack mModelStack;
298  bool mMatChanged = false;
299 
300  ViewportStack mViewportStack;
301  EasyVAO mInternalVAO;
302  // unsigned int mFBOID = 0;
303  FBOStack mFBOStack;
304 };
305 
306 } // namespace al
307 #endif
MatrixStack class.
RenderManager class.
void translate(const Vec< 3, T > &v)
Translate current matrix.
void rotate(float angle, const Vec< 3, T > &axis)
void viewport(int left, int bottom, int width, int height)
Set viewport.
void rotate(float angle, float x=0., float y=0., float z=1.)
void scale(float x, float y, float z=1.)
Scale current matrix along each dimension.
void rotate(const Quatd &q)
Rotate current matrix.
void multModelMatrix(const Matrix4f &m)
Multiply current matrix.
void scale(float s)
Scale current matrix uniformly.
void scale(const Vec< 2, T > &v)
Scale current matrix along each dimension.
void scale(const Vec< 3, T > &v)
Scale current matrix along each dimension.
void translate(float x, float y, float z=0.)
Translate current matrix.
void translate(const Vec< 2, T > &v)
Translate current matrix.
void popMatrix()
Pop current matrix stack.
void pushMatrix()
Push current matrix stack.
void rotate(const Quatf &q)
Rotate current matrix.
void loadIdentity()
Set current matrix to identity.
ViewportStack class.
Definition: al_App.hpp:23
Matrix4< float > Matrix4f
Single-precision 4-by-4 matrix.
Definition: al_Matrix4.hpp:57
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
Definition: al_Vec.hpp:634
int h
left, bottom, width, height