Add #define to transform Object bounds on the fly
[ipdf/code.git] / src / objectrenderer.h
1 /**
2  * @file objectrenderer.h
3  * @brief Definition of ObjectRenderer class
4  */
5
6 #ifndef _OBJECT_RENDERER_H
7 #define _OBJECT_RENDERER_H
8
9 #include "ipdf.h"
10 #include "graphicsbuffer.h"
11 #include "shaderprogram.h"
12 #include "bufferbuilder.h"
13
14
15 namespace IPDF
16 {
17         class View;
18         /**
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
26          */
27         class ObjectRenderer
28         {
29                 public:
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() {}
33
34                         /**
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
37                          */
38                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
39
40                         /** 
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
43                          */
44
45                         struct CPURenderTarget
46                         {
47                                 uint8_t * pixels;
48                                 int64_t w;
49                                 int64_t h;
50                                 
51                                 
52                                 
53                         };
54                         
55                         static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
56                         {
57                                 int64_t index = 4*(x+y*target.w);
58                                 if (index < 0 || index >= 4*(target.w*target.h))
59                                         return Colour(0,0,0,0);
60                                 return Colour(target.pixels[index+0],target.pixels[index+1],target.pixels[index+2],target.pixels[index+3]);
61                         }
62                         
63                         static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
64                         {
65                                 int64_t index = 4*(x+y*target.w);
66                                 if (index < 0 || index >= 4*(target.w*target.h))
67                                         return;
68                                 
69                                 target.pixels[index+0] = c.r;
70                                 target.pixels[index+1] = c.g;
71                                 target.pixels[index+2] = c.b;
72                                 target.pixels[index+3] = c.a;
73                         }
74                         
75                         struct PixelBounds
76                         {
77                                 int64_t x; int64_t y; int64_t w; int64_t h;
78                                 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
79                         };
80                         
81                         typedef std::pair<int64_t, int64_t> PixelPoint;
82
83                         static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
84                         static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
85
86                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
87
88
89                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
90                         
91                         
92                         
93                         const ObjectType m_type; /** Type of objects **/
94                 protected:
95                         friend class View; //View is a friendly fellow in the world of IPDF
96                         void PrepareBuffers(unsigned max_size);
97                         void FinaliseBuffers();
98                         void AddObjectToBuffers(unsigned index);                        
99                 
100                         /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
101                         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);
102                         
103                         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));
104
105                         ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
106                         GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
107                         std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
108                         BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
109         };
110
111         /** Renderer for filled rectangles **/
112         class RectFilledRenderer : public ObjectRenderer
113         {
114                 public:
115                         RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
116                         virtual ~RectFilledRenderer() {}
117                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
118         };
119         /** Renderer for outlined rectangles **/
120         class RectOutlineRenderer : public ObjectRenderer
121         {
122                 public:
123                         RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
124                         virtual ~RectOutlineRenderer() {}
125                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
126         };
127         /** Renderer for filled circles **/
128         class CircleFilledRenderer : public ObjectRenderer
129         {
130                 public:
131                         CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
132                         virtual ~CircleFilledRenderer() {}
133                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
134         };
135
136         /** Renderer for bezier curves **/
137         class BezierRenderer : public ObjectRenderer
138         {
139                 public:
140                         BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
141                         virtual ~BezierRenderer() {}
142                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); 
143                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
144                         void PrepareBezierGPUBuffer(Objects & objects);
145                         
146                         static void RenderBezierOnCPU(unsigned index, Objects & objects, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
147                         
148                 private:
149                         GraphicsBuffer m_bezier_coeffs;
150                         GraphicsBuffer m_bezier_ids;
151                         struct GPUBezierCoeffs
152                         {
153                                 float x0, y0;
154                                 float x1, y1;
155                                 float x2, y2;
156                                 float x3, y3;
157                         };
158
159                         GLuint m_bezier_buffer_texture;
160                         GLuint m_bezier_id_buffer_texture;
161
162         };
163         
164         /** Renderer for filled paths **/
165         class PathRenderer : public ObjectRenderer
166         {
167                 public:
168                         PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
169                         virtual ~PathRenderer() {}
170                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
171                         // do nothing on GPU
172                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
173         };
174
175         class FakeRenderer : public ObjectRenderer
176         {
177                 public:
178                         FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
179                         ~FakeRenderer() {}
180                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) {}
181                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
182         };
183         
184 }
185
186 #endif //_OBJECT_RENDERER_H

UCC git Repository :: git.ucc.asn.au