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"
19 * Abstract Base class representing how a particular type of object will be rendered
20 * Includes GPU rendering and CPU rendering
21 * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
22 * To leave unimplemented, just pass NULL filename strings
23 * For CPU rendering, implement RenderUsingCPU in the derived class
24 * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
25 * The View class uses ObjectRenderer's; see view.h
30 /** Construct the ObjectRenderer **/
31 ObjectRenderer(const ObjectType & type, const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file = "");
32 virtual ~ObjectRenderer() {}
35 * Use the GPU to render the objects - GLSL shader approach
36 * This way is definitely faster, but subject to the GPU's limitations on precision
38 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
41 * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
42 * This way is definitely slower, but gives us more control over the number representations than a GPU
45 struct CPURenderTarget
53 int64_t x; int64_t y; int64_t w; int64_t h;
54 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
57 static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
60 static void SaveBMP(const CPURenderTarget & target, const char * filename);
63 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
67 const ObjectType m_type; /** Type of objects **/
69 friend class View; //View is a friendly fellow in the world of IPDF
70 void PrepareBuffers(unsigned max_size);
71 void FinaliseBuffers();
72 void AddObjectToBuffers(unsigned index);
74 /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
75 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);
77 ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
78 GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
79 std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
80 BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
83 /** Renderer for filled rectangles **/
84 class RectFilledRenderer : public ObjectRenderer
87 RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
88 virtual ~RectFilledRenderer() {}
89 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
91 /** Renderer for outlined rectangles **/
92 class RectOutlineRenderer : public ObjectRenderer
95 RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
96 virtual ~RectOutlineRenderer() {}
97 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
99 /** Renderer for filled circles **/
100 class CircleFilledRenderer : public ObjectRenderer
103 CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
104 virtual ~CircleFilledRenderer() {}
105 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
108 /** Renderer for bezier curves **/
109 class BezierRenderer : public ObjectRenderer
112 BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
113 virtual ~BezierRenderer() {}
114 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
115 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
116 void PrepareBezierGPUBuffer(const Objects & objects);
118 GraphicsBuffer m_bezier_coeffs;
119 GraphicsBuffer m_bezier_ids;
120 struct GPUBezierCoeffs
128 GLuint m_bezier_buffer_texture;
129 GLuint m_bezier_id_buffer_texture;
133 /** Renderer for filled circles **/
134 class GroupRenderer : public ObjectRenderer
137 GroupRenderer() : ObjectRenderer(GROUP, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
138 virtual ~GroupRenderer() {}
139 virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
141 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
146 #endif //_OBJECT_RENDERER_H