Critics are panning the quadtree's panning.
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2 #include "bufferbuilder.h"
3 #include "screen.h"
4 #include "gl_core44.h"
5
6 #ifndef CONTROLPANEL_DISABLED
7         #include "controlpanel.h"
8 #endif //CONTROLPANEL_DISABLED
9
10 using namespace IPDF;
11 using namespace std;
12
13 /**
14  * Constructs a view
15  * Allocates memory for ObjectRenderers
16  * @param document - The document to associate the View with
17  * @param bounds - Initial bounds of the View
18  * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
19  */
20 View::View(Document & document, Screen & screen, const Rect & bounds, const Colour & colour)
21         : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true), 
22                 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(), 
23                 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
24                 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
25                 m_show_fill_points(false), m_show_fill_bounds(false), m_lazy_rendering(true)
26 {
27         Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
28
29         screen.SetView(this); // oh dear...
30
31         
32
33         // Create ObjectRenderers - new's match delete's in View::~View
34         //TODO: Don't forget to put new renderers here or things will be segfaultastic
35         if (screen.Valid())
36         {
37                 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
38                 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
39                 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
40                 m_object_renderers[BEZIER] = new BezierRenderer();
41                 m_object_renderers[PATH] = new PathRenderer();
42         }
43         else
44         {
45                 for (int i = RECT_FILLED; i <= PATH; ++i)
46                         m_object_renderers[i] = new FakeRenderer();
47         }
48
49         // To add rendering for a new type of object;
50         // 1. Add enum to ObjectType in ipdf.h
51         // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
52         // 3. Add it here
53         // 4. Profit
54
55
56 #ifndef QUADTREE_DISABLED
57         m_quadtree_max_depth = 2;
58         m_current_quadtree_node = document.GetQuadTree().root_id;
59 #endif
60 }
61
62 /**
63  * Destroy a view
64  * Frees memory used by ObjectRenderers
65  */
66 View::~View()
67 {
68         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
69         {
70                 delete m_object_renderers[i]; // delete's match new's in constructor
71         }
72         m_object_renderers.clear();
73         delete [] m_cpu_rendering_pixels;
74 }
75
76 /**
77  * Translate the view
78  * @param x, y - Amount to translate
79  */
80 void View::Translate(Real x, Real y)
81 {
82         if (!m_use_gpu_transform)
83                 m_buffer_dirty = true;
84         m_bounds_dirty = true;
85         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
86         m_document.TranslateObjects(-x, -y);
87         #endif
88         x *= m_bounds.w;
89         y *= m_bounds.h;
90         m_bounds.x += x;
91         m_bounds.y += y;
92         //Debug("View Bounds => %s", m_bounds.Str().c_str());
93
94         
95 }
96
97 /**
98  * Set View bounds
99  * @param bounds - New bounds
100  */
101 void View::SetBounds(const Rect & bounds)
102 {
103         m_bounds.x = bounds.x;
104         m_bounds.y = bounds.y;
105         m_bounds.w = bounds.w;
106         m_bounds.h = bounds.h;
107         if (!m_use_gpu_transform)
108                 m_buffer_dirty = true;
109         m_bounds_dirty = true;
110 }
111
112 /**
113  * Scale the View at a point
114  * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
115  * @param scale_amount - Amount to scale by
116  */
117 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
118 {
119         
120         // (x0, y0, w, h) -> (x*w - (x*w - x0)*s, y*h - (y*h - y0)*s, w*s, h*s)
121         // x and y are coordinates in the window
122         // Convert to local coords.
123         if (!m_use_gpu_transform)
124                 m_buffer_dirty = true;
125         m_bounds_dirty = true;
126         
127         
128         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
129         m_document.ScaleObjectsAboutPoint(x, y, scale_amount);
130         #endif
131         x *= m_bounds.w;
132         y *= m_bounds.h;
133         x += m_bounds.x;
134         y += m_bounds.y;
135         
136         Real top = y - m_bounds.y;
137         Real left = x - m_bounds.x;
138         
139         top *= scale_amount;
140         left *= scale_amount;
141         
142         m_bounds.x = x - left;
143         m_bounds.y = y - top;
144         m_bounds.w *= scale_amount;
145         m_bounds.h *= scale_amount;
146         //Debug("Scale at {%s, %s} by %s View Bounds => %s", x.Str().c_str(), y.Str().c_str(), scale_amount.Str().c_str(), m_bounds.Str().c_str());
147         
148         
149 }
150
151 /**
152  * Transform a point in the document to a point relative to the top left corner of the view
153  * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
154  * @param inp - Input Rect {x,y,w,h} in the document
155  * @returns output Rect {x,y,w,h} in the View
156  */
157 Rect View::TransformToViewCoords(const Rect& inp) const
158 {
159         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
160                 return inp;
161         #endif
162         Rect out;
163         out.x = (inp.x - m_bounds.x) / m_bounds.w;
164         out.y = (inp.y - m_bounds.y) / m_bounds.h;
165         out.w = inp.w / m_bounds.w;
166         out.h = inp.h / m_bounds.h;
167         return out;
168 }
169
170 /**
171  * Render the view
172  * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
173  * Otherwise just Blits the cached FrameBuffer
174  * @param width - Width of View to render
175  * @param height - Height of View to render
176  */
177 void View::Render(int width, int height)
178 {
179         if (!m_screen.Valid()) return;
180         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
181         // View dimensions have changed (ie: Window was resized)
182         int prev_width = m_cached_display.GetWidth();
183         int prev_height = m_cached_display.GetHeight();
184         if (width != prev_width || height != prev_height)
185         {
186                 m_cached_display.Create(width, height);
187                 m_bounds_dirty = true;
188         }
189
190         // View bounds have not changed; blit the FrameBuffer as it is
191         if (!m_bounds_dirty && m_lazy_rendering)
192         {
193                 m_cached_display.UnBind();
194                 m_cached_display.Blit();
195                 glPopDebugGroup();
196                 return;
197         }
198         m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
199         m_cached_display.Clear();
200
201 #ifndef QUADTREE_DISABLED
202         if (m_bounds_dirty || !m_lazy_rendering)
203         {
204                 if ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
205                 {
206                         //TODO: Generate a new parent node.
207                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
208                         {
209                                 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
210                                 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
211                         }
212                 }
213
214                 // TODO: Support generating new parent nodes.
215                 if (false && m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
216                 {
217                         if (m_bounds.x < -0.5)
218                         {
219                                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
220                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
221                         }
222                         if (m_bounds.y < -0.5)
223                         {
224                                 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
225                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
226                         }
227                         if (m_bounds.w + m_bounds.x > 0.5)
228                         {
229                                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
230                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
231                         }
232                         if (m_bounds.h + m_bounds.y > 0.5)
233                         {
234                                 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
235                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
236                         }
237                 }
238
239                 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
240                 {
241                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
242                         {
243                                 // We want to reparent into a child node, but none exist. Get the document to create one.
244                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
245                                 m_render_dirty = true;
246                         }
247                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
248                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
249                 }
250                 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
251                 {
252                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
253                         {
254                                 // We want to reparent into a child node, but none exist. Get the document to create one.
255                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
256                                 m_render_dirty = true;
257                         }
258                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
259                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
260                 }
261                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
262                 {
263                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
264                         {
265                                 // We want to reparent into a child node, but none exist. Get the document to create one.
266                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
267                                 m_render_dirty = true;
268                         }
269                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
270                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
271                 }
272                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
273                 {
274                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
275                         {
276                                 // We want to reparent into a child node, but none exist. Get the document to create one.
277                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
278                                 m_render_dirty = true;
279                         }
280                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
281                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
282                 }
283         }
284
285         m_screen.DebugFontPrintF("Current View QuadTree");
286         QuadTreeIndex overlay = m_current_quadtree_node;
287         while (overlay != -1)
288         {
289                 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
290                                         m_document.GetQuadTree().nodes[overlay].object_end);
291                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
292         }
293         m_screen.DebugFontPrintF("\n");
294
295         Rect view_top_bounds = m_bounds;
296         QuadTreeIndex tmp = m_current_quadtree_node;
297         while (tmp != -1)
298         {
299                 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
300                 tmp = m_document.GetQuadTree().nodes[tmp].parent;
301         }
302         m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
303 #endif
304
305         if (!m_use_gpu_rendering)
306         {
307                 // Dynamically resize CPU rendering target pixels if needed
308                 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
309                 {
310                         delete [] m_cpu_rendering_pixels;
311                         m_cpu_rendering_pixels = new uint8_t[width*height*4];
312                         if (m_cpu_rendering_pixels == NULL)
313                                 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
314                 }
315                 // Clear CPU rendering pixels
316                 for (int i = 0; i < width*height*4; ++i)
317                         m_cpu_rendering_pixels[i] = 255;
318         }
319 #ifdef QUADTREE_DISABLED
320         RenderRange(width, height, 0, m_document.ObjectCount());
321 #else
322         RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
323 #endif
324         if (!m_use_gpu_rendering)
325         {
326                 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
327                 // Debug for great victory (do something similar for GPU and compare?)
328                 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
329         }
330         m_cached_display.UnBind(); // resets render target to the screen
331         m_cached_display.Blit(); // blit FrameBuffer to screen
332         m_buffer_dirty = false;
333         glPopDebugGroup();
334         
335 #ifndef CONTROLPANEL_DISABLED
336         ControlPanel::Update();
337 #endif //CONTROLPANEL_DISABLED
338         //Debug("Completed Render");
339         
340 }
341
342 #ifndef QUADTREE_DISABLED
343 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
344 {
345         Rect old_bounds = m_bounds;
346         if (node == QUADTREE_EMPTY) return;
347         if (!remaining_depth) return;
348         //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
349         m_bounds_dirty = true;
350         QuadTreeIndex overlay = node;
351         while(overlay != -1)
352         {
353                 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
354                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
355         }
356
357         if (m_bounds.Intersects(Rect(-1,-1,1,1)))
358         {
359                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
360                 m_bounds_dirty = true;
361                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1, &m_document), remaining_depth - 1);
362         }
363         m_bounds = old_bounds;
364         if (m_bounds.Intersects(Rect(-1,0,1,1)))
365         {
366                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
367                 m_bounds_dirty = true;
368                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0, &m_document), remaining_depth - 1);
369         }
370         m_bounds = old_bounds;
371         if (m_bounds.Intersects(Rect(-1,1,1,1)))
372         {
373                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
374                 m_bounds_dirty = true;
375                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1, &m_document), remaining_depth - 1);
376         }
377         m_bounds = old_bounds;
378         if (m_bounds.Intersects(Rect(0,-1,1,1)))
379         {
380                 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
381                 m_bounds_dirty = true;
382                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1, &m_document), remaining_depth - 1);
383         }
384         m_bounds = old_bounds;
385         if (m_bounds.Intersects(Rect(0,1,1,1)))
386         {
387                 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
388                 m_bounds_dirty = true;
389                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
390         }
391         m_bounds = old_bounds;
392         if (m_bounds.Intersects(Rect(1,-1,1,1)))
393         {
394                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
395                 m_bounds_dirty = true;
396                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1, &m_document), remaining_depth - 1);
397         }
398         m_bounds = old_bounds;
399         if (m_bounds.Intersects(Rect(1,0,1,1)))
400         {
401                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
402                 m_bounds_dirty = true;
403                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
404         }
405         m_bounds = old_bounds;
406         if (m_bounds.Intersects(Rect(1,1,1,1)))
407         {
408                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
409                 m_bounds_dirty = true;
410                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
411         }
412         m_bounds = old_bounds;
413         m_bounds_dirty = true;
414
415 #if 0
416         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
417         m_bounds_dirty = true;
418         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
419         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
420         m_bounds_dirty = true;
421         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
422         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
423         m_bounds_dirty = true;
424         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
425         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
426         m_bounds_dirty = true;
427         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
428         m_bounds = old_bounds;
429         m_bounds_dirty = true;
430 #endif
431 }
432 #endif
433
434 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
435 {
436         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
437         if (m_render_dirty) // document has changed
438                 PrepareRender();
439
440         if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
441         {
442                 if (m_use_gpu_rendering)
443                         UpdateObjBoundsVBO(first_obj, last_obj);
444         }
445
446         if (m_use_gpu_transform)
447         {
448                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
449                                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
450                                         0.0f, 0.0f, float(width), float(height)};
451                 #else
452                 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)),
453                                         0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
454                 #endif
455                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
456         }
457         else
458         {
459                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
460                                         0.0f, 0.0f, float(width), float(height)};
461                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
462         }
463         m_bounds_dirty = false;
464
465
466         // Render using GPU
467         if (m_use_gpu_rendering) 
468         {
469                 if (m_colour.a < 1.0f)
470                 {
471                         glEnable(GL_BLEND);
472                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
473                 }
474                 m_objbounds_vbo.Bind();
475                 m_bounds_ubo.Bind();
476                 glEnableVertexAttribArray(0);
477                 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
478         
479                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
480                 {
481                         m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
482                 }
483                 
484                 glDisableVertexAttribArray(0);
485                 if (m_colour.a < 1.0f)
486                 {
487                         glDisable(GL_BLEND);
488                 }
489         }
490         else // Rasterise on CPU then blit texture to GPU
491         {
492
493                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
494                 {
495                         m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
496                 }
497         }
498         glPopDebugGroup();
499 }
500
501 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
502 {
503         //m_objbounds_vbo.Invalidate();
504         m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
505         m_objbounds_vbo.SetName("Object Bounds VBO");
506         if (m_use_gpu_transform)
507         {
508                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
509         }
510         else
511         {
512                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
513         }
514         m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
515
516         BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.MapRange(first_obj*sizeof(GPUObjBounds), (last_obj-first_obj)*sizeof(GPUObjBounds), false, true, true), m_objbounds_vbo.GetSize());
517
518         for (unsigned id = first_obj; id < last_obj; ++id)
519         {
520                 Rect obj_bounds;
521                 if (m_use_gpu_transform)
522                 {
523                         obj_bounds = m_document.m_objects.bounds[id];
524                 }
525                 else
526                 {
527                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
528                 }
529                 GPUObjBounds gpu_bounds = {
530                         (float)Float(obj_bounds.x),
531                         (float)Float(obj_bounds.y),
532                         (float)Float(obj_bounds.x + obj_bounds.w),
533                         (float)Float(obj_bounds.y + obj_bounds.h)
534                 };
535                 obj_bounds_builder.Add(gpu_bounds);
536
537         }
538         m_objbounds_vbo.UnMap();
539 }
540 /**
541  * Prepare the document for rendering
542  * Will be called on View::Render if m_render_dirty is set
543  * (Called at least once, on the first Render)
544  */
545 void View::PrepareRender()
546 {
547         Debug("Recreate buffers with %u objects", m_document.ObjectCount());
548         // Prepare bounds vbo
549         if (UsingGPURendering())
550         {
551                 m_bounds_ubo.Invalidate();
552                 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
553                 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
554                 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
555         }
556         
557         // Instead of having each ObjectRenderer go through the whole document
558         //  we initialise them, go through the document once adding to the appropriate Renderers
559         //  and then finalise them
560         // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
561
562         // Prepare the buffers
563         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
564         {
565                 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
566         }
567
568         // Add objects from Document to buffers
569         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
570         {
571                 ObjectType type = m_document.m_objects.types[id];
572                 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
573                 // (Also, Wow I just actually used std::vector::at())
574                 // (Also, I just managed to make it throw an exception because I'm a moron)
575                 //Debug("Object of type %d", type);
576         }
577
578
579         // Finish the buffers
580         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
581         {
582                 m_object_renderers[i]->FinaliseBuffers();
583         }
584         if (UsingGPURendering())
585                 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
586         m_render_dirty = false;
587 }
588
589 void View::SaveCPUBMP(const char * filename)
590 {
591         bool prev = UsingGPURendering();
592         SetGPURendering(false);
593         Render(800, 600);
594         ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
595         SetGPURendering(prev);
596 }
597
598 void View::SaveGPUBMP(const char * filename)
599 {
600         bool prev = UsingGPURendering();
601         SetGPURendering(true);
602         Render(800,600);
603         m_screen.ScreenShot(filename);
604         SetGPURendering(prev);  
605 }

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