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

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