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

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