Compile with float/double again.
[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 #include <cstdint>
14
15 #define BEZIER_CPU_DECASTELJAU
16
17 namespace IPDF
18 {
19         class View;
20         /**
21          * Abstract Base class representing how a particular type of object will be rendered
22          * Includes GPU rendering and CPU rendering
23          * For GPU rendering, pass GLSL shader source files to constructor in the constructor of a base class
24          *      To leave unimplemented, just pass NULL filename strings
25          * For CPU rendering, implement RenderUsingCPU in the derived class
26          *  To leave unimplemented, just call ObjectRenderer::RenderUsingCPU in the derived class
27          * The View class uses ObjectRenderer's; see view.h
28          */
29         class ObjectRenderer
30         {
31                 public:
32                         /** Construct the ObjectRenderer **/
33                         ObjectRenderer(const ObjectType & type, const char * vert_glsl_file="", const char * frag_glsl_file="", const char * geom_glsl_file = "");
34                         virtual ~ObjectRenderer() {}
35
36                         /**
37                          * Use the GPU to render the objects - GLSL shader approach
38                          * This way is definitely faster, but subject to the GPU's limitations on precision
39                          */
40                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id);
41
42                         /** 
43                          * Use the CPU to render the objects - "make a bitmap and convert it to a texture" approach
44                          * This way is definitely slower, but gives us more control over the number representations than a GPU
45                          */
46
47                         struct CPURenderTarget
48                         {
49                                 uint8_t * pixels;
50                                 int64_t w;
51                                 int64_t h;
52                                 
53                                 
54                                 
55                         };
56                         
57                         static Colour GetColour(const CPURenderTarget & target, int64_t x, int64_t y)
58                         {
59                                 int64_t index = 4*(x+y*target.w);
60                                 if (index < 0 || index >= 4*(target.w*target.h))
61                                         return Colour(0,0,0,0);
62                                 return Colour(target.pixels[index+0],target.pixels[index+1],target.pixels[index+2],target.pixels[index+3]);
63                         }
64                         
65                         static void SetColour(const CPURenderTarget & target, int64_t x, int64_t y, const Colour & c)
66                         {
67                                 int64_t index = 4*(x+y*target.w);
68                                 if (index < 0 || index >= 4*(target.w*target.h))
69                                         return;
70                                 
71                                 target.pixels[index+0] = c.r;
72                                 target.pixels[index+1] = c.g;
73                                 target.pixels[index+2] = c.b;
74                                 target.pixels[index+3] = c.a;
75                         }
76                         
77                         struct PixelBounds
78                         {
79                                 int64_t x; int64_t y; int64_t w; int64_t h;
80                                 PixelBounds(const Rect & bounds);
81                         };
82                         
83                         typedef std::pair<int64_t, int64_t> PixelPoint;
84
85                         static Rect CPURenderBounds(const Rect & bounds, const View & view, const CPURenderTarget & target);
86                         static PixelPoint CPUPointLocation(const Vec2 & point, const View & view, const CPURenderTarget & target);
87
88                         static void SaveBMP(const CPURenderTarget & target, const char * filename);
89
90
91                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) = 0;
92                         
93                         
94                         
95                         const ObjectType m_type; /** Type of objects **/
96                 protected:
97                         friend class View; //View is a friendly fellow in the world of IPDF
98                         void PrepareBuffers(unsigned max_size);
99                         void FinaliseBuffers();
100                         void AddObjectToBuffers(unsigned index);                        
101                 
102                         /** Helper for CPU rendering that will render a line using Bresenham's algorithm. Do not use the transpose argument. **/
103                         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);
104                         
105                         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));
106
107                         ShaderProgram m_shader_program; /** GLSL shaders for GPU **/
108                         GraphicsBuffer m_ibo; /** Index Buffer Object for GPU rendering **/
109                         std::vector<unsigned> m_indexes; /** Index vector for CPU rendering **/
110                         BufferBuilder<uint32_t> * m_buffer_builder; /** A BufferBuilder is temporarily used when preparing the ibo and std::vector **/
111         };
112
113         /** Renderer for filled rectangles **/
114         class RectFilledRenderer : public ObjectRenderer
115         {
116                 public:
117                         RectFilledRenderer() : ObjectRenderer(RECT_FILLED, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl","shaders/rect_filled_geom.glsl") {}
118                         virtual ~RectFilledRenderer() {}
119                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
120         };
121         /** Renderer for outlined rectangles **/
122         class RectOutlineRenderer : public ObjectRenderer
123         {
124                 public:
125                         RectOutlineRenderer() : ObjectRenderer(RECT_OUTLINE, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/rect_outline_geom.glsl") {}
126                         virtual ~RectOutlineRenderer() {}
127                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
128         };
129         /** Renderer for filled circles **/
130         class CircleFilledRenderer : public ObjectRenderer
131         {
132                 public:
133                         CircleFilledRenderer() : ObjectRenderer(CIRCLE_FILLED, "shaders/rect_vert.glsl", "shaders/circle_frag.glsl", "shaders/circle_filled_geom.glsl") {}
134                         virtual ~CircleFilledRenderer() {}
135                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
136         };
137
138         /** Renderer for bezier curves **/
139         class BezierRenderer : public ObjectRenderer
140         {
141                 public:
142                         BezierRenderer() : ObjectRenderer(BEZIER, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbuf_geom.glsl") {}
143                         virtual ~BezierRenderer() {}
144                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id); 
145                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
146                         void PrepareBezierGPUBuffer(Objects & objects);
147                         
148                         static void RenderBezierOnCPU(const Bezier & relative, const Rect & bounds, const View & view, const CPURenderTarget & target, const Colour & c=Colour(0,0,0,255));
149                         
150                 private:
151                         GraphicsBuffer m_bezier_coeffs;
152                         GraphicsBuffer m_bezier_ids;
153                         struct GPUBezierCoeffs
154                         {
155                                 float x0, y0;
156                                 float x1, y1;
157                                 float x2, y2;
158                                 float x3, y3;
159                         };
160
161                         GLuint m_bezier_buffer_texture;
162                         GLuint m_bezier_id_buffer_texture;
163
164         };
165         
166         /** Renderer for filled paths **/
167         class PathRenderer : public ObjectRenderer
168         {
169                 public:
170                         PathRenderer() : ObjectRenderer(PATH, "shaders/rect_vert.glsl", "shaders/rect_frag.glsl", "shaders/bezier_texbug_geom.glsl") {}
171                         virtual ~PathRenderer() {}
172                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id);
173                         // do nothing on GPU
174                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
175
176         };
177
178         class FakeRenderer : public ObjectRenderer
179         {
180                 public:
181                         FakeRenderer() : ObjectRenderer(PATH,NULL,NULL,NULL) {}
182                         ~FakeRenderer() {}
183                         virtual void RenderUsingCPU(Objects & objects, const View & view, const CPURenderTarget & target, unsigned first_obj_id, unsigned last_obj_id) {}
184                         virtual void RenderUsingGPU(unsigned first_obj_id, unsigned last_obj_id) {}
185         };
186         
187 }
188
189 #endif //_OBJECT_RENDERER_H

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