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

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