AGL
A graphics library
renderer.h
1 // Copyright 2020, Aline Normoyle, alinen@savvysine.com, MIT License
2 
3 #ifndef AGL_RENDERER_H_
4 #define AGL_RENDERER_H_
5 
6 #include <vector>
7 #include <list>
8 #include <string>
9 #include <map>
10 #include "agl/agl.h"
11 #include "agl/aglm.h"
12 #include "agl/image.h"
13 #include "agl/mesh.h"
14 
15 namespace agl {
16 
28 enum BlendMode {
29  DEFAULT,
30  ADD,
31  BLEND
32 };
33 
37 class Renderer {
38  public:
39  Renderer();
40  virtual ~Renderer();
41 
48  void init();
49 
56  void cleanup();
57 
62  bool initialized() const;
63 
66 
88  void perspective(float fovRadians,
89  float aspect, float near, float far);
90 
112  void ortho(float minx, float maxx,
113  float miny, float maxy, float minz, float maxz);
114 
133  void lookAt(const glm::vec3& lookfrom,
134  const glm::vec3& lookat,
135  const glm::vec3& up = glm::vec3(0.0f, 1.0f, 0.0f));
136 
142  glm::vec3 cameraPosition() const;
143 
149  glm::mat4 projectionMatrix() const { return _projectionMatrix; }
150 
156  glm::mat4 viewMatrix() const { return _viewMatrix; }
158 
161 
174  void loadShader(const std::string& name,
175  const std::string& vs, const std::string& fs);
176 
193  void beginShader(const std::string& shaderName);
194 
200  void endShader();
201 
213  void beginRenderTexture(const std::string& targetName);
214 
222  void endRenderTexture();
223 
236  void loadRenderTexture(const std::string& name, int slot,
237  int width, int height);
238 
247  void cleanupShaders();
248 
257  void setUniform(const std::string& name, float x, float y, float z);
258 
262  void setUniform(const std::string& name, float x, float y, float z, float w);
263 
267  void setUniform(const std::string& name, const glm::vec2 &v);
268 
272  void setUniform(const std::string& name, const glm::vec3 &v);
273 
277  void setUniform(const std::string& name, const glm::vec4 &v);
278 
282  void setUniform(const std::string& name, const glm::mat4 &m);
283 
287  void setUniform(const std::string& name, const glm::mat3 &m);
288 
292  void setUniform(const std::string& name, float val);
293 
297  void setUniform(const std::string& name, int val);
298 
302  void setUniform(const std::string& name, bool val);
303 
307  void setUniform(const std::string& name, GLuint val);
308 
319  void texture(const std::string& uniformName, const std::string& textureName);
320 
331  void cubemap(const std::string& uniformName, const std::string& texName);
332 
336 
342  void loadTexture(const std::string& name,
343  const std::string& filename, int slot);
344 
348  void loadTexture(const std::string& name, const Image& img, int slot);
349 
353  void loadCubemap(const std::string& name, const std::string& dir, int slot);
354 
358  void loadCubemap(const std::string& name,
359  const std::vector<std::string>& names, int slot);
360 
364  void loadCubemap(const std::string& name,
365  const std::vector<Image>& images, int slot);
367 
368  // drawing - positioning
371 
388  void push();
389 
395  void pop();
396 
403  void identity();
404 
415  void scale(const glm::vec3& xyz);
416 
427  void translate(const glm::vec3& xyz);
428 
438  void rotate(float angleRad, const glm::vec3& axis);
439 
449  void transform(const glm::mat4& trs);
451 
464  void blendMode(BlendMode mode);
465 
468 
477  void sprite(const glm::vec3& pos, const glm::vec4& color, float size);
478 
487  void line(const glm::vec3& p1, const glm::vec3& p2,
488  const glm::vec3& c1, const glm::vec3& c2);
489 
497  void text(const std::string& text, float x, float y);
498 
504  void fontColor(const glm::vec4& color);
505 
511  void fontSize(int s);
512 
519  float textWidth(const std::string& text);
520 
526  float textHeight();
527 
533  void sphere();
534 
541  void cube();
542 
548  void cone();
549 
554  void teapot();
555 
560  void plane();
561 
566  void cylinder();
567 
573  void capsule();
574 
579  void torus();
580 
586  void skybox(float size = 10.0);
587 
599  void mesh(const Mesh& m);
601 
602  private:
603  void initBillboards();
604  void initLines();
605  void initMesh();
606  void initText();
607 
608  private:
609  bool _initialized;
610  BlendMode _blendMode;
611 
612  // textures
613  struct Texture {
614  GLuint texId;
615  int slot;
616  };
617  std::map<std::string, Texture> _textures;
618 
619  // render targets
620  struct RenderTexture {
621  GLuint handleId; // fbo id
622  GLuint textureId; // render texture target
623  GLuint depthId; // depth buffer id
624  int slot; // texture slot
625  int width; // texture and depth buffer width
626  int height; // texture and depth buffer height
627  GLint winProps[4]; // cached window x,y,w,h (needed to restore viewport)
628  };
629  std::map<std::string, RenderTexture> _renderTextures;
630  std::string _activeRenderTexture;
631 
632  // shaders
633  class Shader* _currentShader;
634  std::map<std::string, class Shader*> _shaders;
635  std::list<Shader*> _shaderStack;
636 
637  // matrix stack
638  std::list<glm::mat4> _stack;
639  glm::mat4 _trs;
640 
641  // perspective and view
642  glm::mat4 _projectionMatrix;
643  glm::mat4 _viewMatrix;
644  glm::vec3 _lookfrom;
645 
646  // default meshes
647  class Cube* _cube;
648  class Cylinder* _cone;
649  class Capsule* _capsule;
650  class Cylinder* _cylinder;
651  class Teapot* _teapot;
652  class Torus* _torus;
653  class Plane* _plane;
654  class Sphere* _sphere;
655  class SkyBox* _skybox;
656 
657  // Quad
658  GLuint mBBVboPosId;
659  GLuint mBBVaoId;
660 
661  // Line
662  GLuint mVboLinePosId;
663  GLuint mVboLineColorId;
664  GLuint mVaoLineId;
665 
666  // Text
667  int _fontNormal;
668  unsigned int _fontColor;
669  float _fontSize;
670 };
671 
672 } // namespace agl
673 #endif // AGL_RENDERER_H_
agl::Renderer::text
void text(const std::string &text, float x, float y)
Draws text using the current font size and color.
Definition: renderer.cpp:291
agl::Renderer::initialized
bool initialized() const
Return whether the Renderer is ready for drawing.
Definition: renderer.cpp:88
agl::Renderer::torus
void torus()
Draws a torus.
Definition: renderer.cpp:422
agl::Renderer::identity
void identity()
Clear the current transform.
Definition: renderer.cpp:386
agl::Renderer::cone
void cone()
Draws a cone centered at the origin, with the tip towards +Z.
Definition: renderer.cpp:426
agl::Renderer::cameraPosition
glm::vec3 cameraPosition() const
Get the current camera position.
Definition: renderer.cpp:93
agl::Renderer::plane
void plane()
Draws a plane.
Definition: renderer.cpp:410
agl::Renderer::loadShader
void loadShader(const std::string &name, const std::string &vs, const std::string &fs)
Load a GLSL shader from files.
Definition: renderer.cpp:637
agl::Renderer::setUniform
void setUniform(const std::string &name, float x, float y, float z)
Set a uniform parameter in the currently active shader.
Definition: renderer.cpp:482
agl::Renderer::scale
void scale(const glm::vec3 &xyz)
Scale an object.
Definition: renderer.cpp:390
agl::Renderer::blendMode
void blendMode(BlendMode mode)
Mode for combining colors when drawing.
Definition: renderer.cpp:226
agl::Renderer::init
void init()
Initialize this class for drawing.
Definition: renderer.cpp:97
agl::Renderer::beginRenderTexture
void beginRenderTexture(const std::string &targetName)
Render to a texture instead of to the screen @targetName The name of the texture target.
Definition: renderer.cpp:654
agl::Renderer::texture
void texture(const std::string &uniformName, const std::string &textureName)
Set a uniform sampler parameter in the currently active shader.
Definition: renderer.cpp:259
agl::Renderer::viewMatrix
glm::mat4 viewMatrix() const
Get the current view matrix.
Definition: renderer.h:156
agl::Renderer::capsule
void capsule()
Draws a capsule with endpoints at (0,0,0) and (0,0,1). The cap radius is 0.25 and the width is 0....
Definition: renderer.cpp:418
agl::Renderer::cleanupShaders
void cleanupShaders()
Clear all active shaders.
Definition: renderer.cpp:454
agl::Renderer::mesh
void mesh(const Mesh &m)
Draws a custom mesh.
Definition: renderer.cpp:438
agl::Renderer::textHeight
float textHeight()
Get the height of a string (font metrics)
Definition: renderer.cpp:285
agl::Renderer::cubemap
void cubemap(const std::string &uniformName, const std::string &texName)
Set a uniform sampler parameter in the currently active shader.
Definition: renderer.cpp:359
agl::Renderer::fontColor
void fontColor(const glm::vec4 &color)
Set font color for drawing text.
Definition: renderer.cpp:268
agl::Renderer::translate
void translate(const glm::vec3 &xyz)
Translate an object.
Definition: renderer.cpp:394
agl::Renderer::ortho
void ortho(float minx, float maxx, float miny, float maxy, float minz, float maxz)
Set the current projection to an orthographic view.
Definition: renderer.cpp:248
agl::Renderer::line
void line(const glm::vec3 &p1, const glm::vec3 &p2, const glm::vec3 &c1, const glm::vec3 &c2)
Draws a sprite using a point billboard.
Definition: renderer.cpp:311
agl::Renderer
The Renderer class draws meshes to the screen using shaders.
Definition: renderer.h:37
agl::Renderer::loadTexture
void loadTexture(const std::string &name, const std::string &filename, int slot)
Load a texture from a file.
Definition: renderer.cpp:603
agl::Renderer::skybox
void skybox(float size=10.0)
Draws a skybox (typically with a cubemap)
Definition: renderer.cpp:367
agl::Renderer::lookAt
void lookAt(const glm::vec3 &lookfrom, const glm::vec3 &lookat, const glm::vec3 &up=glm::vec3(0.0f, 1.0f, 0.0f))
Set the camera position and orientation.
Definition: renderer.cpp:253
agl::Renderer::beginShader
void beginShader(const std::string &shaderName)
Set active shader to use for rendering.
Definition: renderer.cpp:460
agl::Renderer::teapot
void teapot()
Draws a teapot with largest side with width 1.
Definition: renderer.cpp:406
agl::Renderer::textWidth
float textWidth(const std::string &text)
Get the width of a string (font metrics)
Definition: renderer.cpp:280
agl::Renderer::projectionMatrix
glm::mat4 projectionMatrix() const
Get the current projection matrix.
Definition: renderer.h:149
agl::Renderer::rotate
void rotate(float angleRad, const glm::vec3 &axis)
Rotates an object.
Definition: renderer.cpp:398
agl::Image
Implements loading, modifying, and saving RGBA images.
Definition: image.h:27
agl::Renderer::endShader
void endShader()
Clear active shader to use for rendering.
Definition: renderer.cpp:468
agl::Renderer::sprite
void sprite(const glm::vec3 &pos, const glm::vec4 &color, float size)
Draws a sprite using a point billboard.
Definition: renderer.cpp:344
agl::Renderer::perspective
void perspective(float fovRadians, float aspect, float near, float far)
Set the current projection to a perspective view.
Definition: renderer.cpp:243
agl::Renderer::endRenderTexture
void endRenderTexture()
Revert to rendering to the screen.
Definition: renderer.cpp:667
agl::Renderer::cylinder
void cylinder()
Draws a teapot with largest side with width 1.
Definition: renderer.cpp:414
aglm.h
Global utilities and common third party includes.
agl::Renderer::sphere
void sphere()
Draws a sphere centered at the origin with radius 0.5.
Definition: renderer.cpp:434
agl::Renderer::pop
void pop()
Pop the current matrix off the matrix stack.
Definition: renderer.cpp:380
agl::Renderer::transform
void transform(const glm::mat4 &trs)
Transforms an object by the given matrix.
Definition: renderer.cpp:402
agl::Mesh
Base class for meshes.
Definition: mesh.h:18
agl::Renderer::cube
void cube()
Draws a cube centered at the origin with width, height, and depth equal to 1.0.
Definition: renderer.cpp:430
agl::Renderer::fontSize
void fontSize(int s)
Set font size for drawing text.
Definition: renderer.cpp:276
agl::Renderer::cleanup
void cleanup()
Cleanup all resources used for drawing.
Definition: renderer.cpp:55
agl::Renderer::loadCubemap
void loadCubemap(const std::string &name, const std::string &dir, int slot)
Load a cube map.
agl::Renderer::push
void push()
Push the current matrix onto the matrix stack.
Definition: renderer.cpp:376