38b2c17a63edfd5fa51f016e5a2ff56d06127b4a
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2 #include "bufferbuilder.h"
3
4 #include "gl_core44.h"
5
6 using namespace IPDF;
7 using namespace std;
8
9 /**
10  * Constructs a view
11  * Allocates memory for ObjectRenderers
12  * @param document - The document to associate the View with
13  * @param bounds - Initial bounds of the View
14  * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
15  */
16 View::View(Document & document, const Rect & bounds, const Colour & colour)
17         : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true), 
18                 m_render_dirty(true), m_document(document), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(), 
19                 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES)
20 {
21         Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
22
23         // Create ObjectRenderers - new's match delete's in View::~View
24         // Ok, look, this may seem disgusting, but go look at View::PrepareRender before you murder me
25         m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
26         m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
27         m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
28
29         // To add rendering for a new type of object;
30         // 1. Add enum to ObjectType in ipdf.h
31         // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
32         // 3. Add it here
33         // 4. Profit
34 }
35
36 /**
37  * Destroy a view
38  * Frees memory used by ObjectRenderers
39  */
40 View::~View()
41 {
42         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
43         {
44                 //delete m_object_renderers[i];
45         }
46         m_object_renderers.clear();
47 }
48
49 /**
50  * Translate the view
51  * @param x, y - Amount to translate
52  */
53 void View::Translate(Real x, Real y)
54 {
55         x *= m_bounds.w;
56         y *= m_bounds.h;
57         m_bounds.x += x;
58         m_bounds.y += y;
59         Debug("View Bounds => %s", m_bounds.Str().c_str());
60         if (!m_use_gpu_transform)
61                 m_buffer_dirty = true;
62         m_bounds_dirty = true;
63 }
64
65 /**
66  * Scale the View at a point
67  * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
68  * @param scale_amount - Amount to scale by
69  */
70 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
71 {
72         // x and y are coordinates in the window
73         // Convert to local coords.
74         x *= m_bounds.w;
75         y *= m_bounds.h;
76         x += m_bounds.x;
77         y += m_bounds.y;
78         
79         Real top = y - m_bounds.y;
80         Real left = x - m_bounds.x;
81         
82         top *= scale_amount;
83         left *= scale_amount;
84         
85         m_bounds.x = x - left;
86         m_bounds.y = y - top;
87         m_bounds.w *= scale_amount;
88         m_bounds.h *= scale_amount;
89         Debug("View Bounds => %s", m_bounds.Str().c_str());
90         if (!m_use_gpu_transform)
91                 m_buffer_dirty = true;
92         m_bounds_dirty = true;
93 }
94
95 /**
96  * Transform a point in the document to a point relative to the top left corner of the view
97  * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
98  * @param inp - Input Rect {x,y,w,h} in the document
99  * @returns output Rect {x,y,w,h} in the View
100  */
101 Rect View::TransformToViewCoords(const Rect& inp) const
102 {
103         Rect out;
104         out.x = (inp.x - m_bounds.x) / m_bounds.w;
105         out.y = (inp.y - m_bounds.y) / m_bounds.h;
106         out.w = inp.w / m_bounds.w;
107         out.h = inp.h / m_bounds.h;
108         return out;
109 }
110
111 /**
112  * Render the view
113  * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
114  * Otherwise just Blits the cached FrameBuffer
115  * @param width - Width of View to render
116  * @param height - Height of View to render
117  */
118 void View::Render(int width, int height)
119 {
120         // View dimensions have changed (ie: Window was resized)
121         if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
122         {
123                 m_cached_display.Create(width, height);
124                 m_bounds_dirty = true;
125         }
126
127         // View bounds have not changed; blit the FrameBuffer as it is
128         if (!m_bounds_dirty)
129         {
130                 m_cached_display.UnBind();
131                 m_cached_display.Blit();
132                 return;
133         }
134
135         // Bind FrameBuffer for rendering, and clear it
136         m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
137         m_cached_display.Clear();
138
139
140         if (m_render_dirty) // document has changed
141                 PrepareRender();
142
143         if (m_buffer_dirty) // object bounds have changed
144                 UpdateObjBoundsVBO();
145
146         if (m_use_gpu_transform)
147         {
148                 GLfloat glbounds[] = {static_cast<GLfloat>(Float(m_bounds.x)), static_cast<GLfloat>(Float(m_bounds.y)), static_cast<GLfloat>(Float(m_bounds.w)), static_cast<GLfloat>(Float(m_bounds.h))};
149                 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
150         }
151         else
152         {
153                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
154                 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
155         }
156         m_bounds_dirty = false;
157
158
159         // Render using GPU
160         if (m_use_gpu_rendering) 
161         {
162                 if (m_colour.a < 1.0f)
163                 {
164                         glEnable(GL_BLEND);
165                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166                 }
167                 m_objbounds_vbo.Bind();
168                 m_bounds_ubo.Bind();
169                 glEnableVertexAttribArray(0);
170                 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
171         
172                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
173                 {
174                         m_object_renderers[i]->RenderUsingGPU();
175                 }
176                 
177                 glDisableVertexAttribArray(0);
178                 if (m_colour.a < 1.0f)
179                 {
180                         glDisable(GL_BLEND);
181                 }
182         }
183         else // Rasterise on CPU then blit texture to GPU
184         {
185                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
186                 {
187                         m_object_renderers[i]->RenderUsingCPU();
188                 }
189         }
190         m_cached_display.UnBind(); // resets render target to the screen
191         m_cached_display.Blit(); // blit FrameBuffer to screen
192 }
193
194 void View::UpdateObjBoundsVBO()
195 {
196         Debug("Called");
197         m_objbounds_vbo.Invalidate();
198         m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
199         if (m_use_gpu_transform)
200         {
201                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
202         }
203         else
204         {
205                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
206         }
207         m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
208
209         BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
210
211         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
212         {
213                 Rect obj_bounds;
214                 if (m_use_gpu_transform)
215                 {
216                         obj_bounds = m_document.m_objects.bounds[id];
217                 }
218                 else
219                 {
220                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
221                 }
222                 GPUObjBounds gpu_bounds = {
223                         (float)Float(obj_bounds.x),
224                         (float)Float(obj_bounds.y),
225                         (float)Float(obj_bounds.x + obj_bounds.w),
226                         (float)Float(obj_bounds.y + obj_bounds.h)
227                 };
228                 obj_bounds_builder.Add(gpu_bounds);
229
230         }
231         m_objbounds_vbo.UnMap();
232         m_buffer_dirty = false;
233 }
234 /**
235  * Prepare the document for rendering
236  * Will be called on View::Render if m_render_dirty is set
237  * (Called at least once, on the first Render)
238  */
239 void View::PrepareRender()
240 {
241         // Prepare bounds vbo
242         m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
243         m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
244         
245         // Instead of having each ObjectRenderer go through the whole document
246         //  we initialise them, go through the document once adding to the appropriate Renderers
247         //  and then finalise them
248         // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
249
250         // Prepare the buffers
251         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
252         {
253                 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
254         }
255
256         // Add objects from Document to buffers
257         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
258         {
259                 ObjectType type = m_document.m_objects.types[id];
260                 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
261                 // (Also, Wow I just actually used std::vector::at())
262         }
263
264         // Finish the buffers
265         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
266         {
267                 m_object_renderers[i]->FinaliseBuffers();
268         }       
269         m_render_dirty = false;
270 }

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