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"
14 #define BEZIER_CPU_DECASTELJAU
20 * Abstract Base class representing how a particular type of object will be rendered
21 * Includes GPU rendering and CPU rendering
22 * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
23 * To leave unimplemented, just pass NULL filename strings
24 * For CPU rendering, implement RenderUsingCPU in the derived class
25 * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
26 * The View class uses ObjectRenderer's; see view.h
31 /** Construct the ObjectRenderer **/
32 ObjectRenderer(const ObjectType & type, const char * vert_glsl_file="", const char * frag_glsl_file="", const char * geom_glsl_file = "");
33 virtual ~ObjectRenderer() {}
36 * Use the GPU to render the objects - GLSL shader approach
37 * This way is definitely faster, but subject to the GPU's limitations on precision
39 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
42 * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
43 * This way is definitely slower, but gives us more control over the number representations than a GPU
46 struct CPURenderTarget
56 static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
58 int64_t index = 4*(x+y*target.w);
59 if (index < 0 || index >= 4*(target.w*target.h))
60 return Colour(0,0,0,0);
61 return Colour(target.pixels[index+0],target.pixels[index+1],target.pixels[index+2],target.pixels[index+3]);
64 static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
66 int64_t index = 4*(x+y*target.w);
67 if (index < 0 || index >= 4*(target.w*target.h))
70 target.pixels[index+0] = c.r;
71 target.pixels[index+1] = c.g;
72 target.pixels[index+2] = c.b;
73 target.pixels[index+3] = c.a;
78 int64_t x; int64_t y; int64_t w; int64_t h;
79 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
82 typedef std::pair<int64_t, int64_t> PixelPoint;
84 static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
85 static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
87 static void SaveBMP(const CPURenderTarget & target, const char * filename);
90 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
94 const ObjectType m_type; /** Type of objects **/
96 friend class View; //View is a friendly fellow in the world of IPDF
97 void PrepareBuffers(unsigned max_size);
98 void FinaliseBuffers();
99 void AddObjectToBuffers(unsigned index);
101 /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
102 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);
104 static void FloodFillOnCPU(int64_t x0, int64_t y0, const PixelBounds & bounds, const CPURenderTarget & target, const Colour & fill, const Colour & stroke=Colour(0,0,0,0));
106 ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
107 GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
108 std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
109 BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
112 /** Renderer for filled rectangles **/
113 class RectFilledRenderer : public ObjectRenderer
116 RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
117 virtual ~RectFilledRenderer() {}
118 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
120 /** Renderer for outlined rectangles **/
121 class RectOutlineRenderer : public ObjectRenderer
124 RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
125 virtual ~RectOutlineRenderer() {}
126 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
128 /** Renderer for filled circles **/
129 class CircleFilledRenderer : public ObjectRenderer
132 CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
133 virtual ~CircleFilledRenderer() {}
134 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
137 /** Renderer for bezier curves **/
138 class BezierRenderer : public ObjectRenderer
141 BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
142 virtual ~BezierRenderer() {}
143 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
144 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
145 void PrepareBezierGPUBuffer(Objects & objects);
147 static void RenderBezierOnCPU(const Bezier & relative, const Rect & bounds, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
150 GraphicsBuffer m_bezier_coeffs;
151 GraphicsBuffer m_bezier_ids;
152 struct GPUBezierCoeffs
160 GLuint m_bezier_buffer_texture;
161 GLuint m_bezier_id_buffer_texture;
165 /** Renderer for filled paths **/
166 class PathRenderer : public ObjectRenderer
169 PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbug_geom.glsl") {}
170 virtual ~PathRenderer() {}
171 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
173 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
177 class FakeRenderer : public ObjectRenderer
180 FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
182 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) {}
183 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
188 #endif //_OBJECT_RENDERER_H