2 * @file objectrenderer.h
3 * @brief Definition of ObjectRenderer class
6 #ifndef _OBJECT_RENDERER_H
7 #define _OBJECT_RENDERER_H
10 #include "graphicsbuffer.h"
11 #include "shaderprogram.h"
12 #include "bufferbuilder.h"
18 * Abstract Base class representing how a particular type of object will be rendered
19 * Includes GPU rendering and CPU rendering
20 * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
21 * To leave unimplemented, just pass NULL filename strings
22 * For CPU rendering, implement RenderUsingCPU in the derived class
23 * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
24 * The View class uses ObjectRenderer's; see view.h
29 /** Construct the ObjectRenderer **/
30 ObjectRenderer(const ObjectType & type, const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file = "");
31 virtual ~ObjectRenderer() {}
34 * Use the GPU to render the objects - GLSL shader approach
35 * This way is definitely faster, but subject to the GPU's limitations on precision
37 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
40 * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
41 * This way is definitely slower, but gives us more control over the number representations than a GPU
44 struct CPURenderTarget
52 int64_t x; int64_t y; int64_t w; int64_t h;
53 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
56 static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
59 static void SaveBMP(const CPURenderTarget & target, const char * filename);
62 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
66 const ObjectType m_type; /** Type of objects **/
68 friend class View; //View is a friendly fellow in the world of IPDF
69 void PrepareBuffers(unsigned max_size);
70 void FinaliseBuffers();
71 void AddObjectToBuffers(unsigned index);
73 /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
74 static void RenderLineOnCPU(int64_t x0, int64_t y0, int64_t x1, int64_t y1, const CPURenderTarget & target, const Colour & colour = Colour(0,0,0,1), bool transpose = false);
76 ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
77 GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
78 std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
79 BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
82 /** Renderer for filled rectangles **/
83 class RectFilledRenderer : public ObjectRenderer
86 RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
87 virtual ~RectFilledRenderer() {}
88 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
90 /** Renderer for outlined rectangles **/
91 class RectOutlineRenderer : public ObjectRenderer
94 RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
95 virtual ~RectOutlineRenderer() {}
96 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
98 /** Renderer for filled circles **/
99 class CircleFilledRenderer : public ObjectRenderer
102 CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
103 virtual ~CircleFilledRenderer() {}
104 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
107 /** Renderer for bezier curves **/
108 class BezierRenderer : public ObjectRenderer
111 BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
112 virtual ~BezierRenderer() {}
113 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
114 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
115 void PrepareBezierGPUBuffer(const Objects & objects);
117 GraphicsBuffer m_bezier_coeffs;
118 GraphicsBuffer m_bezier_ids;
119 struct GPUBezierCoeffs
127 GLuint m_bezier_buffer_texture;
128 GLuint m_bezier_id_buffer_texture;
133 #endif //_OBJECT_RENDERER_H