9ea8a12ef13b40c4302af92925c2d9a6fd2fa295
[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 #define BEZIER_CPU_DECASTELJAU
15
16 namespace IPDF
17 {
18         class View;
19         /**
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
27          */
28         class ObjectRenderer
29         {
30                 public:
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() {}
34
35                         /**
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
38                          */
39                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
40
41                         /** 
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
44                          */
45
46                         struct CPURenderTarget
47                         {
48                                 uint8_t * pixels;
49                                 int64_t w;
50                                 int64_t h;
51                                 
52                                 
53                                 
54                         };
55                         
56                         static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
57                         {
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]);
62                         }
63                         
64                         static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
65                         {
66                                 int64_t index = 4*(x+y*target.w);
67                                 if (index < 0 || index >= 4*(target.w*target.h))
68                                         return;
69                                 
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;
74                         }
75                         
76                         struct PixelBounds
77                         {
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)) {}
80                         };
81                         
82                         typedef std::pair<int64_t, int64_t> PixelPoint;
83
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);
86
87                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
88
89
90                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
91                         
92                         
93                         
94                         const ObjectType m_type; /** Type of objects **/
95                 protected:
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);                        
100                 
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);
103                         
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));
105
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 **/
110         };
111
112         /** Renderer for filled rectangles **/
113         class RectFilledRenderer : public ObjectRenderer
114         {
115                 public:
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);
119         };
120         /** Renderer for outlined rectangles **/
121         class RectOutlineRenderer : public ObjectRenderer
122         {
123                 public:
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);
127         };
128         /** Renderer for filled circles **/
129         class CircleFilledRenderer : public ObjectRenderer
130         {
131                 public:
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);
135         };
136
137         /** Renderer for bezier curves **/
138         class BezierRenderer : public ObjectRenderer
139         {
140                 public:
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);
146                         
147                         static void RenderBezierOnCPU(const Bezier & relative, const Rect & bounds, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
148                         
149                 private:
150                         GraphicsBuffer m_bezier_coeffs;
151                         GraphicsBuffer m_bezier_ids;
152                         struct GPUBezierCoeffs
153                         {
154                                 float x0, y0;
155                                 float x1, y1;
156                                 float x2, y2;
157                                 float x3, y3;
158                         };
159
160                         GLuint m_bezier_buffer_texture;
161                         GLuint m_bezier_id_buffer_texture;
162
163         };
164         
165         /** Renderer for filled paths **/
166         class PathRenderer : public ObjectRenderer
167         {
168                 public:
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);
172                         // do nothing on GPU
173                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
174
175         };
176
177         class FakeRenderer : public ObjectRenderer
178         {
179                 public:
180                         FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
181                         ~FakeRenderer() {}
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) {}
184         };
185         
186 }
187
188 #endif //_OBJECT_RENDERER_H

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