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"
15 #define BEZIER_CPU_DECASTELJAU
21 * Abstract Base class representing how a particular type of object will be rendered
22 * Includes GPU rendering and CPU rendering
23 * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
24 * To leave unimplemented, just pass NULL filename strings
25 * For CPU rendering, implement RenderUsingCPU in the derived class
26 * To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
27 * The View class uses ObjectRenderer's; see view.h
32 /** Construct the ObjectRenderer **/
33 ObjectRenderer(const ObjectType & type, const char * vert_glsl_file="", const char * frag_glsl_file="", const char * geom_glsl_file = "");
34 virtual ~ObjectRenderer() {}
37 * Use the GPU to render the objects - GLSL shader approach
38 * This way is definitely faster, but subject to the GPU's limitations on precision
40 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
43 * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
44 * This way is definitely slower, but gives us more control over the number representations than a GPU
47 struct CPURenderTarget
57 static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
59 int64_t index = 4*(x+y*target.w);
60 if (index < 0 || index >= 4*(target.w*target.h))
61 return Colour(0,0,0,0);
62 return Colour(target.pixels[index+0],target.pixels[index+1],target.pixels[index+2],target.pixels[index+3]);
65 static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
67 int64_t index = 4*(x+y*target.w);
68 if (index < 0 || index >= 4*(target.w*target.h))
71 target.pixels[index+0] = c.r;
72 target.pixels[index+1] = c.g;
73 target.pixels[index+2] = c.b;
74 target.pixels[index+3] = c.a;
79 int64_t x; int64_t y; int64_t w; int64_t h;
80 PixelBounds(const Rect & bounds);
83 typedef std::pair<int64_t, int64_t> PixelPoint;
85 static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
86 static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
88 static void SaveBMP(const CPURenderTarget & target, const char * filename);
91 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
95 const ObjectType m_type; /** Type of objects **/
97 friend class View; //View is a friendly fellow in the world of IPDF
98 void PrepareBuffers(unsigned max_size);
99 void FinaliseBuffers();
100 void AddObjectToBuffers(unsigned index);
102 /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
103 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);
105 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));
107 ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
108 GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
109 std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
110 BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
113 /** Renderer for filled rectangles **/
114 class RectFilledRenderer : public ObjectRenderer
117 RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
118 virtual ~RectFilledRenderer() {}
119 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
121 /** Renderer for outlined rectangles **/
122 class RectOutlineRenderer : public ObjectRenderer
125 RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
126 virtual ~RectOutlineRenderer() {}
127 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
129 /** Renderer for filled circles **/
130 class CircleFilledRenderer : public ObjectRenderer
133 CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
134 virtual ~CircleFilledRenderer() {}
135 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
138 /** Renderer for bezier curves **/
139 class BezierRenderer : public ObjectRenderer
142 BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
143 virtual ~BezierRenderer() {}
144 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
145 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
146 void PrepareBezierGPUBuffer(Objects & objects);
148 static void RenderBezierOnCPU(const Bezier & relative, const Rect & bounds, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
151 GraphicsBuffer m_bezier_coeffs;
152 GraphicsBuffer m_bezier_ids;
153 struct GPUBezierCoeffs
161 GLuint m_bezier_buffer_texture;
162 GLuint m_bezier_id_buffer_texture;
166 /** Renderer for filled paths **/
167 class PathRenderer : public ObjectRenderer
170 PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
171 virtual ~PathRenderer() {}
172 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
174 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
178 class FakeRenderer : public ObjectRenderer
181 FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
183 virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) {}
184 virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
189 #endif //_OBJECT_RENDERER_H