2 #include "bufferbuilder.h"
6 #ifndef CONTROLPANEL_DISABLED
7 #include "controlpanel.h"
8 #endif //CONTROLPANEL_DISABLED
11 #ifdef TRANSFORM_BEZIERS_TO_PATH
12 #ifndef TRANSFORM_OBJECTS_NOT_VIEW
13 //#error Cannot TRANSFORM_BEZIERS_TO_PATH _without_ TRANSFORM_OBJECTS_NOT_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
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)
34 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
36 screen.SetView(this); // oh dear...
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
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();
52 for (int i = RECT_FILLED; i <= PATH; ++i)
53 m_object_renderers[i] = new FakeRenderer();
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
63 #ifndef QUADTREE_DISABLED
64 m_quadtree_max_depth = 2;
65 m_current_quadtree_node = document.GetQuadTree().root_id;
71 * Frees memory used by ObjectRenderers
75 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
77 delete m_object_renderers[i]; // delete's match new's in constructor
79 m_object_renderers.clear();
80 delete [] m_cpu_rendering_pixels;
85 * @param x, y - Amount to translate
87 void View::Translate(Real x, Real y)
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
97 m_document.TranslateObjects(-x, -y, type);
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());
108 * @param bounds - New bounds
110 void View::SetBounds(const Rect & bounds)
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;
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
126 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
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;
137 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
138 ObjectType type = NUMBER_OF_OBJECT_TYPES;
139 #ifdef TRANSFORM_BEZIERS_TO_PATH
142 m_document.ScaleObjectsAboutPoint(x, y, scale_amount, type);
144 VReal vx = m_bounds.w * VReal(x);
145 VReal vy = m_bounds.h * VReal(y);
149 VReal top = vy - m_bounds.y;
150 VReal left = vx - m_bounds.x;
153 left *= scale_amount;
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());
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
170 Rect View::TransformToViewCoords(const Rect& inp) const
172 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
175 return TransformRectCoordinates(m_bounds.Convert<Real>(), inp);
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
185 void View::Render(int width, int height)
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)
194 m_cached_display.Create(width, height);
195 m_bounds_dirty = true;
198 // View bounds have not changed; blit the FrameBuffer as it is
199 if (!m_bounds_dirty && m_lazy_rendering)
201 m_cached_display.UnBind();
202 m_cached_display.Blit();
206 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
207 m_cached_display.Clear();
209 #ifndef QUADTREE_DISABLED
210 // I'm going to write this out in comments, so hopefully then I'll understand it. :/
212 // This code looks at the current bounds and tries to work out how they need to change
213 // to keep the view looking at the correct quadtree node.
215 // The idea is that the width/height of the view bounds are always 0.5<=wh<=1.0. We then always
216 // try to keep the bottom-right corner of the node on-screen, changing nodes to suit. Why bottom-right,
217 // you may ask. It's an excellent question, with a dubious, hand-wavey answer: because we're manipulating
218 // the bounds, it was easier to do it that way. (The top-left corner of the bounds are within the main
220 if (m_bounds_dirty || !m_lazy_rendering)
222 // If we're too far zoomed out, become the parent of the current node.
223 if ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
225 // If a parent node exists, we'll become it.
226 //TODO: Generate a new parent node if none exists, and work out when to change child_type
227 // away from QTC_UNKNOWN
228 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
230 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
231 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
235 // If we have a parent... (This prevents some crashes, but should disappear.)
236 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
238 // If the current node is off the left-hand side of the screen...
239 while (m_bounds.x > 1)
241 //... the current node becomes the node to its right.
242 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
243 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
245 while (m_bounds.y > 1)
247 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
248 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
250 while (m_bounds.x < 0)
252 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
253 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
255 while (m_bounds.y < 0)
257 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
258 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
262 // 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.)
263 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
265 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
267 // We want to reparent into a child node, but none exist. Get the document to create one.
268 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
269 m_render_dirty = true;
271 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
272 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
274 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
276 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
278 // We want to reparent into a child node, but none exist. Get the document to create one.
279 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
280 m_render_dirty = true;
282 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
283 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
285 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
287 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
289 // We want to reparent into a child node, but none exist. Get the document to create one.
290 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
291 m_render_dirty = true;
293 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
294 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
296 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
298 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
300 // We want to reparent into a child node, but none exist. Get the document to create one.
301 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
302 m_render_dirty = true;
304 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
305 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
308 // Otherwise, we'll arbitrarily select the bottom-right.
309 // TODO: Perhaps select based on greatest area?
310 if (m_bounds.w < 0.5 || m_bounds.h < 0.5)
312 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
314 // We want to reparent into a child node, but none exist. Get the document to create one.
315 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
316 m_render_dirty = true;
318 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
319 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
323 m_screen.DebugFontPrintF("Current View QuadTree");
324 QuadTreeIndex overlay = m_current_quadtree_node;
325 while (overlay != -1)
327 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
328 m_document.GetQuadTree().nodes[overlay].object_end);
329 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
331 m_screen.DebugFontPrintF("\n");
332 m_screen.DebugFontPrintF("Left: %d, Right: %d, Up: %d, Down: %d\n",
333 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, 0),
334 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, 0),
335 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, 0),
336 m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, 0));
339 Rect view_top_bounds = m_bounds;
340 QuadTreeIndex tmp = m_current_quadtree_node;
343 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
344 tmp = m_document.GetQuadTree().nodes[tmp].parent;
346 m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
349 if (!m_use_gpu_rendering)
351 // Dynamically resize CPU rendering target pixels if needed
352 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
354 delete [] m_cpu_rendering_pixels;
355 m_cpu_rendering_pixels = new uint8_t[width*height*4];
356 if (m_cpu_rendering_pixels == NULL)
357 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
359 // Clear CPU rendering pixels
360 for (int i = 0; i < width*height*4; ++i)
361 m_cpu_rendering_pixels[i] = 255;
363 #ifdef QUADTREE_DISABLED
364 RenderRange(width, height, 0, m_document.ObjectCount());
366 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
368 if (!m_use_gpu_rendering)
370 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
371 // Debug for great victory (do something similar for GPU and compare?)
372 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
374 m_cached_display.UnBind(); // resets render target to the screen
375 m_cached_display.Blit(); // blit FrameBuffer to screen
376 m_buffer_dirty = false;
379 #ifndef CONTROLPANEL_DISABLED
380 // The powers that be suggest that this may be causing of the segfaults.
381 //ControlPanel::Update();
382 #endif //CONTROLPANEL_DISABLED
383 //Debug("Completed Render");
387 #ifndef QUADTREE_DISABLED
388 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
390 Rect old_bounds = m_bounds;
391 if (node == QUADTREE_EMPTY) return;
392 if (!remaining_depth) return;
393 //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
394 m_bounds_dirty = true;
395 m_render_dirty = m_buffer_dirty = true;
396 QuadTreeIndex overlay = node;
399 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
400 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
403 if (m_bounds.Intersects(Rect(1,1,1,1)))
405 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
406 m_bounds_dirty = true;
407 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
409 m_bounds = old_bounds;
410 if (m_bounds.Intersects(Rect(1,0,1,1)))
412 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
413 m_bounds_dirty = true;
414 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
416 m_bounds = old_bounds;
417 if (m_bounds.Intersects(Rect(0,1,1,1)))
419 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
420 m_bounds_dirty = true;
421 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
423 m_bounds = old_bounds;
424 m_bounds_dirty = true;
427 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
428 m_bounds_dirty = true;
429 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
430 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
431 m_bounds_dirty = true;
432 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
433 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
434 m_bounds_dirty = true;
435 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
436 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
437 m_bounds_dirty = true;
438 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
439 m_bounds = old_bounds;
440 m_bounds_dirty = true;
445 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
447 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
448 if (m_render_dirty) // document has changed
451 if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
453 if (m_use_gpu_rendering)
454 UpdateObjBoundsVBO(first_obj, last_obj);
457 if (m_use_gpu_transform)
459 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
460 //Debug("Transform objects, not view");
461 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
462 0.0f, 0.0f, float(width), float(height)};
464 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)),
465 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
467 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
471 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
472 0.0f, 0.0f, float(width), float(height)};
473 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
475 m_bounds_dirty = false;
479 if (m_use_gpu_rendering)
481 if (m_colour.a < 1.0f)
484 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
486 m_objbounds_vbo.Bind();
488 glEnableVertexAttribArray(0);
489 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
491 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
493 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
496 glDisableVertexAttribArray(0);
497 if (m_colour.a < 1.0f)
502 else // Rasterise on CPU then blit texture to GPU
505 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
507 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
513 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
515 //m_objbounds_vbo.Invalidate();
516 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
517 m_objbounds_vbo.SetName("Object Bounds VBO");
519 #ifndef TRANSFORM_OBJECTS_NOT_VIEW
520 if (m_use_gpu_transform)
522 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
525 #endif //TRANSFORM_OBJECTS_NOT_VIEW
527 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
529 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
531 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());
533 #ifndef TRANSFORM_BEZIERS_TO_PATH
534 for (unsigned id = first_obj; id < last_obj; ++id)
537 if (m_use_gpu_transform)
539 obj_bounds = m_document.m_objects.bounds[id];
543 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
545 GPUObjBounds gpu_bounds = {
548 Float(obj_bounds.x + obj_bounds.w),
549 Float(obj_bounds.y + obj_bounds.h)
552 obj_bounds_builder.Add(gpu_bounds);
555 for (unsigned i = 0; i < m_document.m_objects.paths.size(); ++i)
557 Path & path = m_document.m_objects.paths[i];
558 Rect & pbounds = path.GetBounds(m_document.m_objects); // Not very efficient...
559 for (unsigned id = path.m_start; id <= path.m_end; ++id)
561 if (id < first_obj || id >= last_obj)
564 Rect obj_bounds = m_document.m_objects.bounds[id];
566 obj_bounds.x *= pbounds.w;
567 obj_bounds.x += pbounds.x;
568 obj_bounds.y *= pbounds.h;
569 obj_bounds.y += pbounds.y;
570 obj_bounds.w *= pbounds.w;
571 obj_bounds.h *= pbounds.h;
573 if (!m_use_gpu_transform)
574 obj_bounds = TransformToViewCoords(obj_bounds);
575 GPUObjBounds gpu_bounds = {
576 ClampFloat(obj_bounds.x),
577 ClampFloat(obj_bounds.y),
578 ClampFloat(obj_bounds.x + obj_bounds.w),
579 ClampFloat(obj_bounds.y + obj_bounds.h)
581 obj_bounds_builder.Add(gpu_bounds);
582 //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());
584 GPUObjBounds p_gpu_bounds = {
585 ClampFloat(pbounds.x),
586 ClampFloat(pbounds.y),
587 ClampFloat(pbounds.x + pbounds.w),
588 ClampFloat(pbounds.y + pbounds.h)
590 obj_bounds_builder.Add(p_gpu_bounds);
593 m_objbounds_vbo.UnMap();
596 * Prepare the document for rendering
597 * Will be called on View::Render if m_render_dirty is set
598 * (Called at least once, on the first Render)
600 void View::PrepareRender()
602 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
603 // Prepare bounds vbo
604 if (UsingGPURendering())
606 m_bounds_ubo.Invalidate();
607 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
608 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
609 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
612 // Instead of having each ObjectRenderer go through the whole document
613 // we initialise them, go through the document once adding to the appropriate Renderers
614 // and then finalise them
615 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
617 // Prepare the buffers
618 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
620 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
623 // Add objects from Document to buffers
624 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
626 ObjectType type = m_document.m_objects.types[id];
627 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
628 // (Also, Wow I just actually used std::vector::at())
629 // (Also, I just managed to make it throw an exception because I'm a moron)
630 //Debug("Object of type %d", type);
634 // Finish the buffers
635 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
637 m_object_renderers[i]->FinaliseBuffers();
639 if (UsingGPURendering())
641 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
643 m_render_dirty = false;
646 void View::SaveCPUBMP(const char * filename)
648 bool prev = UsingGPURendering();
649 SetGPURendering(false);
651 ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
652 SetGPURendering(prev);
655 void View::SaveGPUBMP(const char * filename)
657 bool prev = UsingGPURendering();
658 SetGPURendering(true);
660 m_screen.ScreenShot(filename);
661 SetGPURendering(prev);