Careful, you may have to shade your eyes
[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                                 return Colour(Real(target.pixels[index+0])/Real(255),
59                                         Real(target.pixels[index+1])/Real(255),
60                                         Real(target.pixels[index+2])/Real(255),
61                                         Real(target.pixels[index+3])/Real(255));
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                                 target.pixels[index+0] = c.r*255;
68                                 target.pixels[index+1] = c.g*255;
69                                 target.pixels[index+2] = c.b*255;
70                                 target.pixels[index+3] = c.a*255;
71                         }
72                         
73                         struct PixelBounds
74                         {
75                                 int64_t x; int64_t y; int64_t w; int64_t h;
76                                 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
77                         };
78                         
79                         typedef std::pair<int64_t, int64_t> PixelPoint;
80
81                         static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
82                         static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
83
84                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
85
86
87                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
88                         
89                         
90                         
91                         const ObjectType m_type; /** Type of objects **/
92                 protected:
93                         friend class View; //View is a friendly fellow in the world of IPDF
94                         void PrepareBuffers(unsigned max_size);
95                         void FinaliseBuffers();
96                         void AddObjectToBuffers(unsigned index);                        
97                 
98                         /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
99                         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);
100                         
101                         static void FloodFillOnCPU(int64_t x0, int64_t y0, const PixelBounds & bounds, const CPURenderTarget & target, const Colour & fill);
102
103                         ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
104                         GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
105                         std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
106                         BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
107         };
108
109         /** Renderer for filled rectangles **/
110         class RectFilledRenderer : public ObjectRenderer
111         {
112                 public:
113                         RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
114                         virtual ~RectFilledRenderer() {}
115                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
116         };
117         /** Renderer for outlined rectangles **/
118         class RectOutlineRenderer : public ObjectRenderer
119         {
120                 public:
121                         RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
122                         virtual ~RectOutlineRenderer() {}
123                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
124         };
125         /** Renderer for filled circles **/
126         class CircleFilledRenderer : public ObjectRenderer
127         {
128                 public:
129                         CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
130                         virtual ~CircleFilledRenderer() {}
131                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
132         };
133
134         /** Renderer for bezier curves **/
135         class BezierRenderer : public ObjectRenderer
136         {
137                 public:
138                         BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
139                         virtual ~BezierRenderer() {}
140                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); 
141                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
142                         void PrepareBezierGPUBuffer(const Objects & objects);
143                 private:
144                         GraphicsBuffer m_bezier_coeffs;
145                         GraphicsBuffer m_bezier_ids;
146                         struct GPUBezierCoeffs
147                         {
148                                 float x0, y0;
149                                 float x1, y1;
150                                 float x2, y2;
151                                 float x3, y3;
152                         };
153
154                         GLuint m_bezier_buffer_texture;
155                         GLuint m_bezier_id_buffer_texture;
156
157         };
158         
159         /** Renderer for filled paths **/
160         class PathRenderer : public ObjectRenderer
161         {
162                 public:
163                         PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
164                         virtual ~PathRenderer() {}
165                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
166                         // do nothing on GPU
167                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
168         };
169         
170 }
171
172 #endif //_OBJECT_RENDERER_H

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