Refactor Rendering of Objects (prepare for CPU rendering)
[ipdf/code.git] / src / view.h
1 #ifndef _VIEW_H
2 #define _VIEW_H
3
4 #include "ipdf.h"
5 #include "document.h"
6 #include "framebuffer.h"
7 #include "objectrenderer.h"
8
9 #define USE_GPU_TRANSFORM true
10 #define USE_GPU_RENDERING true
11
12 namespace IPDF
13 {
14         /**
15          * The View class manages a rectangular view into the document.
16          * It is responsible for coordinate transforms and rendering the document.
17          * ObjectRenderer's for each type of Object should be created in the constructor.
18          */
19         class View
20         {
21                 public:
22                         View(Document & document, const Rect & bounds = Rect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f));
23                         virtual ~View();
24
25                         void Render(int width = 0, int height = 0);
26                         
27                         void Translate(Real x, Real y);
28                         void ScaleAroundPoint(Real x, Real y, Real scale_amount);
29                         
30                         Rect TransformToViewCoords(const Rect& inp) const;
31                         
32                         const Rect& GetBounds() const { return m_bounds; }
33                         
34                         const bool UsingGPUTransform() const { return m_use_gpu_transform; } // whether view transform calculated on CPU or GPU
35                         const bool UsingGPURendering() const { return m_use_gpu_rendering; } // whether GPU shaders are used or CPU rendering
36                         void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); m_bounds_dirty = true; m_buffer_dirty = true; }
37                         void ToggleGPURendering() { m_use_gpu_rendering = (!m_use_gpu_rendering); m_bounds_dirty = true; m_buffer_dirty = true; }
38                 
39                 private:
40                         struct GPUObjBounds
41                         {
42                                 float x0, y0;
43                                 float x1, y1;
44                         };
45
46                         void PrepareRender(); // call when m_render_dirty is true
47                         void UpdateObjBoundsVBO(); // call when m_buffer_dirty is true
48
49                         bool m_use_gpu_transform;
50                         bool m_use_gpu_rendering;
51                         bool m_bounds_dirty; // the view bounds has changed (occurs when changing view)
52                         bool m_buffer_dirty; // the object bounds have changed (also occurs when changing view, but only when not using GPU transforms)
53                         bool m_render_dirty; // the document has changed (occurs when document first loaded)
54                         Document & m_document;
55                         FrameBuffer m_cached_display;
56                         Rect m_bounds;
57                         Colour m_colour;
58
59                         // Stores the view bounds.
60                         GraphicsBuffer m_bounds_ubo; //bounds_dirty means this one has changed
61                         // Stores the bounds for _all_ objects.
62                         GraphicsBuffer m_objbounds_vbo; //buffer_dirty means this one has changed
63
64                         // ObjectRenderers to be initialised in constructor
65                         // Trust me it will be easier to generalise things this way. Even though there are pointers.
66                         std::vector<ObjectRenderer*> m_object_renderers; 
67                         
68         };
69 }
70
71 #endif //_VIEW_H

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