8f52aa00c95065b6ed99a92e6ee9b6a52cdb0aa6
[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                         struct PixelBounds
52                         {
53                                 int64_t x; int64_t y; int64_t w; int64_t h;
54                                 PixelBounds(const Rect & bounds) : x(Double(bounds.x)), y(Double(bounds.y)), w(Double(bounds.w)), h(Double(bounds.h)) {}
55                         };
56
57                         static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
58
59
60                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
61
62
63                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
64                         
65                         
66                         
67                         const ObjectType m_type; /** Type of objects **/
68                 protected:
69                         friend class View; //View is a friendly fellow in the world of IPDF
70                         void PrepareBuffers(unsigned max_size);
71                         void FinaliseBuffers();
72                         void AddObjectToBuffers(unsigned index);                        
73                 
74                         /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
75                         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);
76
77                         ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
78                         GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
79                         std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
80                         BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
81         };
82
83         /** Renderer for filled rectangles **/
84         class RectFilledRenderer : public ObjectRenderer
85         {
86                 public:
87                         RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
88                         virtual ~RectFilledRenderer() {}
89                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
90         };
91         /** Renderer for outlined rectangles **/
92         class RectOutlineRenderer : public ObjectRenderer
93         {
94                 public:
95                         RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
96                         virtual ~RectOutlineRenderer() {}
97                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
98         };
99         /** Renderer for filled circles **/
100         class CircleFilledRenderer : public ObjectRenderer
101         {
102                 public:
103                         CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
104                         virtual ~CircleFilledRenderer() {}
105                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
106         };
107
108         /** Renderer for bezier curves **/
109         class BezierRenderer : public ObjectRenderer
110         {
111                 public:
112                         BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
113                         virtual ~BezierRenderer() {}
114                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); 
115                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
116                         void PrepareBezierGPUBuffer(const Objects & objects);
117                 private:
118                         GraphicsBuffer m_bezier_coeffs;
119                         GraphicsBuffer m_bezier_ids;
120                         struct GPUBezierCoeffs
121                         {
122                                 float x0, y0;
123                                 float x1, y1;
124                                 float x2, y2;
125                                 float x3, y3;
126                         };
127
128                         GLuint m_bezier_buffer_texture;
129                         GLuint m_bezier_id_buffer_texture;
130
131         };
132         
133                 /** Renderer for filled circles **/
134         class GroupRenderer : public ObjectRenderer
135         {
136                 public:
137                         GroupRenderer() : ObjectRenderer(GROUP, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
138                         virtual ~GroupRenderer() {}
139                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
140                         // do nothing on GPU
141                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
142         };
143         
144 }
145
146 #endif //_OBJECT_RENDERER_H

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