Merge branch terrible and branch awful
[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 #include "path.h"
9 #include "transformationtype.h"
10
11 #define USE_GPU_TRANSFORM false 
12 #define USE_GPU_RENDERING true
13 #define USE_SHADING !(USE_GPU_RENDERING) && true
14
15
16 #include "gmprat.h"
17
18
19 namespace IPDF
20 {
21         typedef TRect<VReal> VRect;
22         
23         class Screen;
24         /**
25          * The View class manages a rectangular view into the document.
26          * It is responsible for coordinate transforms and rendering the document.
27          * ObjectRenderer's for each type of Object should be created in the constructor.
28          */
29         class View
30         {
31                 public:
32                         View(Document & document, Screen & screen, const VRect & bounds = VRect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f));
33                         virtual ~View();
34
35                         void Render(int width = 0, int height = 0);
36                         
37                         void Translate(Real x, Real y);
38                         void ScaleAroundPoint(Real x, Real y, Real scale_amount);
39                         void SetBounds(const Rect & new_bounds);
40                         
41                         Rect TransformToViewCoords(const Rect& inp) const;
42                         
43                         const VRect& GetBounds() const { return m_bounds; }
44                         
45                         
46                         const bool UsingGPUTransform() const { return m_use_gpu_transform; } // whether view transform calculated on CPU or GPU
47                         const bool UsingGPURendering() const { return m_use_gpu_rendering; } // whether GPU shaders are used or CPU rendering
48                         void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); m_bounds_dirty = true; m_buffer_dirty = true; }
49                         void ToggleGPURendering() { m_use_gpu_rendering = (!m_use_gpu_rendering); m_bounds_dirty = true; m_buffer_dirty = true; }
50                         void SetGPUTransform(bool state) {m_use_gpu_transform = state; m_bounds_dirty = true; m_buffer_dirty = true;}
51                         
52                         void SetGPURendering(bool state) {m_use_gpu_rendering = state; m_bounds_dirty = true; m_buffer_dirty = true;}
53
54                         bool ShowingBezierBounds() const {return m_show_bezier_bounds;} // render bounds rectangles
55                         void ShowBezierBounds(bool state) {m_show_bezier_bounds = state; m_bounds_dirty = true; m_buffer_dirty = true;}
56                         bool ShowingBezierType() const {return m_show_bezier_type;}
57                         void ShowBezierType(bool state) {m_show_bezier_type = state; m_bounds_dirty = true; m_buffer_dirty = true;}
58                         bool ShowingFillPoints() const {return m_show_fill_points;}
59                         void ShowFillPoints(bool state) {m_show_fill_points = state; m_bounds_dirty = true; m_buffer_dirty = true;}
60                         bool ShowingFillBounds() const {return m_show_fill_bounds;}
61                         void ShowFillBounds(bool state) {m_show_fill_bounds = true;}
62                         
63                         bool PerformingShading() const {return m_perform_shading;}
64                         void PerformShading(bool state) {m_perform_shading = state; m_bounds_dirty = true; m_buffer_dirty = true;}
65
66                         void ForceBoundsDirty() {m_bounds_dirty = true;}                
67                         void ForceBufferDirty() {m_buffer_dirty = true;}                
68                         void ForceRenderDirty() {m_render_dirty = true;}
69                         
70                         void QueryGPUBounds(const char * filename, const char * mode="r");
71                         
72                         void SetLazyRendering(bool state = true) {m_lazy_rendering = state;}
73                         bool UsingLazyRendering() const {return m_lazy_rendering;}
74                         
75                         void SaveBMP(const char * filename) {if (UsingGPURendering()) SaveGPUBMP(filename); else SaveCPUBMP(filename);}
76                         
77                         void SaveCPUBMP(const char * filename);
78                         void SaveGPUBMP(const char * filename);
79
80                         Document & Doc() {return m_document;}
81 #ifndef QUADTREE_DISABLED
82                         QuadTreeIndex GetCurrentQuadtreeNode() { return m_current_quadtree_node; }
83 #endif
84
85                 private:
86                         struct GPUObjBounds
87                         {
88                                 float x0, y0;
89                                 float x1, y1;
90                         } __attribute__((packed));
91                         
92                         
93
94                         void PrepareRender(); // call when m_render_dirty is true
95                         void UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj); // call when m_buffer_dirty is true
96
97                         void RenderRange(int width, int height, unsigned first_obj, unsigned last_obj);
98
99                         bool m_use_gpu_transform;
100                         bool m_use_gpu_rendering;
101                         bool m_bounds_dirty; // the view bounds has changed (occurs when changing view)
102                         bool m_buffer_dirty; // the object bounds have changed (also occurs when changing view, but only when not using GPU transforms)
103                         bool m_render_dirty; // the document has changed (occurs when document first loaded)
104                         Document & m_document;
105                         Screen & m_screen;
106                         FrameBuffer m_cached_display;
107                         VRect m_bounds;
108                         Colour m_colour;
109
110                         // Stores the view bounds.
111                         GraphicsBuffer m_bounds_ubo; //bounds_dirty means this one has changed
112                         // Stores the bounds for _all_ objects.
113                         GraphicsBuffer m_objbounds_vbo; //buffer_dirty means this one has changed
114
115                         // ObjectRenderers to be initialised in constructor
116                         // Trust me it will be easier to generalise things this way. Even though there are pointers.
117                         std::vector<ObjectRenderer*> m_object_renderers; 
118                         uint8_t * m_cpu_rendering_pixels; // pixels to be used for CPU rendering
119
120                         
121                         // shading
122                         bool m_perform_shading;
123                         
124                         // Debug rendering
125                         bool m_show_bezier_bounds;
126                         bool m_show_bezier_type;
127                         bool m_show_fill_points;
128                         bool m_show_fill_bounds;
129                         
130                         bool m_lazy_rendering;// don't redraw frames unless we need to
131                         
132                         FILE * m_query_gpu_bounds_on_next_frame;
133
134
135 #ifndef QUADTREE_DISABLED
136                         QuadTreeIndex m_current_quadtree_node;  // The highest node we will traverse.
137                         int m_quadtree_max_depth;               // The maximum quadtree depth.
138                         void RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth);
139
140 #endif
141         };
142 }
143
144 #endif //_VIEW_H

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