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

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