B├ęziers on the GPU.
[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 namespace IPDF
15 {
16         class View;
17         /**
18          * Abstract Base class representing how a particular type of object will be rendered
19          * Includes GPU rendering and CPU rendering
20          * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
21          *      To leave unimplemented, just pass NULL filename strings
22          * For CPU rendering, implement RenderUsingCPU in the derived class
23          *  To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
24          * The View class uses ObjectRenderer's; see view.h
25          */
26         class ObjectRenderer
27         {
28                 public:
29                         /** Construct the ObjectRenderer **/
30                         ObjectRenderer(const ObjectType & type, const char * vert_glsl_file, const char * frag_glsl_file, const char * geom_glsl_file = "");
31                         virtual ~ObjectRenderer() {}
32
33                         /**
34                          * Use the GPU to render the objects - GLSL shader approach
35                          * This way is definitely faster, but subject to the GPU's limitations on precision
36                          */
37                         virtual void RenderUsingGPU();
38
39                         /** 
40                          * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
41                          * This way is definitely slower, but gives us more control over the number representations than a GPU
42                          */
43
44                         struct CPURenderTarget
45                         {
46                                 uint8_t * pixels;
47                                 int64_t w;
48                                 int64_t h;
49                         };
50                         struct PixelBounds
51                         {
52                                 int64_t x; int64_t y; int64_t w; int64_t h;
53                                 PixelBounds(const Rect & bounds) : x(bounds.x), y(bounds.y), w(bounds.w), h(bounds.h) {}
54                         };
55
56                         static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
57
58
59                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
60
61
62                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target) = 0;
63                         
64                         
65                         
66                         const ObjectType m_type; /** Type of objects **/
67                 protected:
68                         friend class View; //View is a friendly fellow in the world of IPDF
69                         void PrepareBuffers(unsigned max_size);
70                         void FinaliseBuffers();
71                         void AddObjectToBuffers(unsigned index);                        
72                 
73                         /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
74                         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);
75
76                         ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
77                         GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
78                         std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
79                         BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
80         };
81
82         /** Renderer for filled rectangles **/
83         class RectFilledRenderer : public ObjectRenderer
84         {
85                 public:
86                         RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
87                         virtual ~RectFilledRenderer() {}
88                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
89         };
90         /** Renderer for outlined rectangles **/
91         class RectOutlineRenderer : public ObjectRenderer
92         {
93                 public:
94                         RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
95                         virtual ~RectOutlineRenderer() {}
96                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
97         };
98         /** Renderer for filled circles **/
99         class CircleFilledRenderer : public ObjectRenderer
100         {
101                 public:
102                         CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
103                         virtual ~CircleFilledRenderer() {}
104                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
105         };
106
107         /** Renderer for bezier curves **/
108         class BezierRenderer : public ObjectRenderer
109         {
110                 public:
111                         BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
112                         virtual ~BezierRenderer() {}
113                         virtual void RenderUsingGPU(); 
114                         virtual void RenderUsingCPU(const Objects & objects, const View & view, const CPURenderTarget & target);
115                         void PrepareBezierGPUBuffer(const Objects & objects);
116                 private:
117                         GraphicsBuffer m_bezier_coeffs;
118                         GraphicsBuffer m_bezier_ids;
119                         struct GPUBezierCoeffs
120                         {
121                                 float x0, y0;
122                                 float x1, y1;
123                                 float x2, y2;
124                         };
125
126                         GLuint m_bezier_buffer_texture;
127                         GLuint m_bezier_id_buffer_texture;
128
129         };
130 }
131
132 #endif //_OBJECT_RENDERER_H

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