2 #include "bufferbuilder.h"
6 #ifndef CONTROLPANEL_DISABLED
7 #include "controlpanel.h"
8 #endif //CONTROLPANEL_DISABLED
15 * Allocates memory for ObjectRenderers
16 * @param document - The document to associate the View with
17 * @param bounds - Initial bounds of the View
18 * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
20 View::View(Document & document, Screen & screen, const Rect & bounds, const Colour & colour)
21 : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true),
22 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(),
23 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
24 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
25 m_show_fill_points(false), m_show_fill_bounds(false)
27 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
29 screen.SetView(this); // oh dear...
31 // Create ObjectRenderers - new's match delete's in View::~View
32 //TODO: Don't forget to put new renderers here or things will be segfaultastic
33 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
34 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
35 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
36 m_object_renderers[BEZIER] = new BezierRenderer();
37 m_object_renderers[PATH] = new PathRenderer();
39 // To add rendering for a new type of object;
40 // 1. Add enum to ObjectType in ipdf.h
41 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
46 #ifndef QUADTREE_DISABLED
47 m_quadtree_max_depth = 2;
48 m_current_quadtree_node = document.GetQuadTree().root_id;
54 * Frees memory used by ObjectRenderers
58 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
60 delete m_object_renderers[i]; // delete's match new's in constructor
62 m_object_renderers.clear();
63 delete [] m_cpu_rendering_pixels;
68 * @param x, y - Amount to translate
70 void View::Translate(Real x, Real y)
76 //Debug("View Bounds => %s", m_bounds.Str().c_str());
77 if (!m_use_gpu_transform)
78 m_buffer_dirty = true;
79 m_bounds_dirty = true;
84 * @param bounds - New bounds
86 void View::SetBounds(const Rect & bounds)
88 m_bounds.x = bounds.x;
89 m_bounds.y = bounds.y;
90 m_bounds.w = bounds.w;
91 m_bounds.h = bounds.h;
92 if (!m_use_gpu_transform)
93 m_buffer_dirty = true;
94 m_bounds_dirty = true;
98 * Scale the View at a point
99 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
100 * @param scale_amount - Amount to scale by
102 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
104 // x and y are coordinates in the window
105 // Convert to local coords.
111 Real top = y - m_bounds.y;
112 Real left = x - m_bounds.x;
115 left *= scale_amount;
117 m_bounds.x = x - left;
118 m_bounds.y = y - top;
119 m_bounds.w *= scale_amount;
120 m_bounds.h *= scale_amount;
121 //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());
122 if (!m_use_gpu_transform)
123 m_buffer_dirty = true;
124 m_bounds_dirty = true;
128 * Transform a point in the document to a point relative to the top left corner of the view
129 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
130 * @param inp - Input Rect {x,y,w,h} in the document
131 * @returns output Rect {x,y,w,h} in the View
133 Rect View::TransformToViewCoords(const Rect& inp) const
136 out.x = (inp.x - m_bounds.x) / m_bounds.w;
137 out.y = (inp.y - m_bounds.y) / m_bounds.h;
138 out.w = inp.w / m_bounds.w;
139 out.h = inp.h / m_bounds.h;
145 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
146 * Otherwise just Blits the cached FrameBuffer
147 * @param width - Width of View to render
148 * @param height - Height of View to render
150 void View::Render(int width, int height)
152 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
153 // View dimensions have changed (ie: Window was resized)
154 int prev_width = m_cached_display.GetWidth();
155 int prev_height = m_cached_display.GetHeight();
156 if (width != prev_width || height != prev_height)
158 m_cached_display.Create(width, height);
159 m_bounds_dirty = true;
162 // View bounds have not changed; blit the FrameBuffer as it is
165 m_cached_display.UnBind();
166 m_cached_display.Blit();
170 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
171 m_cached_display.Clear();
173 #ifndef QUADTREE_DISABLED
176 if ( false && (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))
178 //TODO: Generate a new parent node.
179 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
181 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
182 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
185 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
187 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
189 // We want to reparent into a child node, but none exist. Get the document to create one.
190 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
191 m_render_dirty = true;
193 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
194 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
196 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
198 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
200 // We want to reparent into a child node, but none exist. Get the document to create one.
201 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
202 m_render_dirty = true;
204 m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
205 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
207 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
209 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
211 // We want to reparent into a child node, but none exist. Get the document to create one.
212 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
213 m_render_dirty = true;
215 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
216 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
218 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
220 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
222 // We want to reparent into a child node, but none exist. Get the document to create one.
223 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
224 m_render_dirty = true;
226 m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
227 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
230 m_screen.DebugFontPrintF("Current View QuadTree Node: %d (objs: %d -> %d)\n", m_current_quadtree_node, m_document.GetQuadTree().nodes[m_current_quadtree_node].object_begin,
231 m_document.GetQuadTree().nodes[m_current_quadtree_node].object_end);
233 Rect view_top_bounds = m_bounds;
234 QuadTreeIndex tmp = m_current_quadtree_node;
237 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
238 tmp = m_document.GetQuadTree().nodes[tmp].parent;
240 m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
243 if (!m_use_gpu_rendering)
245 // Dynamically resize CPU rendering target pixels if needed
246 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
248 delete [] m_cpu_rendering_pixels;
249 m_cpu_rendering_pixels = new uint8_t[width*height*4];
250 if (m_cpu_rendering_pixels == NULL)
251 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
253 // Clear CPU rendering pixels
254 for (int i = 0; i < width*height*4; ++i)
255 m_cpu_rendering_pixels[i] = 255;
257 #ifdef QUADTREE_DISABLED
258 RenderRange(width, height, 0, m_document.ObjectCount());
260 RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
262 if (!m_use_gpu_rendering)
264 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
265 // Debug for great victory (do something similar for GPU and compare?)
266 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
268 m_cached_display.UnBind(); // resets render target to the screen
269 m_cached_display.Blit(); // blit FrameBuffer to screen
270 m_buffer_dirty = false;
273 #ifndef CONTROLPANEL_DISABLED
274 ControlPanel::Update();
275 #endif //CONTROLPANEL_DISABLED
276 //Debug("Completed Render");
280 #ifndef QUADTREE_DISABLED
281 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
283 Rect old_bounds = m_bounds;
284 if (node == QUADTREE_EMPTY) return;
285 if (!remaining_depth) return;
286 //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
287 m_bounds_dirty = true;
288 RenderRange(width, height, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
290 if (m_bounds.Intersects(Rect(-1,-1,1,1)))
292 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
293 m_bounds_dirty = true;
294 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1), remaining_depth - 1);
296 if (m_bounds.Intersects(Rect(-1,0,1,1)))
298 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
299 m_bounds_dirty = true;
300 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0), remaining_depth - 1);
302 if (m_bounds.Intersects(Rect(-1,1,1,1)))
304 m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
305 m_bounds_dirty = true;
306 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1), remaining_depth - 1);
308 if (m_bounds.Intersects(Rect(0,-1,1,1)))
310 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
311 m_bounds_dirty = true;
312 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1), remaining_depth - 1);
314 if (m_bounds.Intersects(Rect(0,1,1,1)))
316 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
317 m_bounds_dirty = true;
318 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1), remaining_depth - 1);
320 if (m_bounds.Intersects(Rect(1,-1,1,1)))
322 m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
323 m_bounds_dirty = true;
324 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1), remaining_depth - 1);
326 if (m_bounds.Intersects(Rect(1,0,1,1)))
328 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
329 m_bounds_dirty = true;
330 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0), remaining_depth - 1);
332 if (m_bounds.Intersects(Rect(1,1,1,1)))
334 m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
335 m_bounds_dirty = true;
336 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1), remaining_depth - 1);
338 m_bounds = old_bounds;
339 m_bounds_dirty = true;
342 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
343 m_bounds_dirty = true;
344 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
345 m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
346 m_bounds_dirty = true;
347 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
348 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
349 m_bounds_dirty = true;
350 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
351 m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
352 m_bounds_dirty = true;
353 RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
354 m_bounds = old_bounds;
355 m_bounds_dirty = true;
360 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
362 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
363 if (m_render_dirty) // document has changed
366 if (m_buffer_dirty || m_bounds_dirty) // object bounds have changed
367 UpdateObjBoundsVBO(first_obj, last_obj);
369 if (m_use_gpu_transform)
371 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)),
372 0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
373 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
377 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
378 0.0f, 0.0f, float(width), float(height)};
379 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
381 m_bounds_dirty = false;
385 if (m_use_gpu_rendering)
387 if (m_colour.a < 1.0f)
390 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
392 m_objbounds_vbo.Bind();
394 glEnableVertexAttribArray(0);
395 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
397 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
399 m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
402 glDisableVertexAttribArray(0);
403 if (m_colour.a < 1.0f)
408 else // Rasterise on CPU then blit texture to GPU
411 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
413 m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
419 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
421 //m_objbounds_vbo.Invalidate();
422 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
423 m_objbounds_vbo.SetName("Object Bounds VBO");
424 if (m_use_gpu_transform)
426 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
430 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
432 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
434 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());
436 for (unsigned id = first_obj; id < last_obj; ++id)
439 if (m_use_gpu_transform)
441 obj_bounds = m_document.m_objects.bounds[id];
445 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
447 GPUObjBounds gpu_bounds = {
448 (float)Float(obj_bounds.x),
449 (float)Float(obj_bounds.y),
450 (float)Float(obj_bounds.x + obj_bounds.w),
451 (float)Float(obj_bounds.y + obj_bounds.h)
453 obj_bounds_builder.Add(gpu_bounds);
456 m_objbounds_vbo.UnMap();
459 * Prepare the document for rendering
460 * Will be called on View::Render if m_render_dirty is set
461 * (Called at least once, on the first Render)
463 void View::PrepareRender()
465 Debug("Recreate buffers with %u objects", m_document.ObjectCount());
466 // Prepare bounds vbo
467 m_bounds_ubo.Invalidate();
468 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
469 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
470 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
472 // Instead of having each ObjectRenderer go through the whole document
473 // we initialise them, go through the document once adding to the appropriate Renderers
474 // and then finalise them
475 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
477 // Prepare the buffers
478 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
480 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
483 // Add objects from Document to buffers
484 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
486 ObjectType type = m_document.m_objects.types[id];
487 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
488 // (Also, Wow I just actually used std::vector::at())
489 // (Also, I just managed to make it throw an exception because I'm a moron)
490 //Debug("Object of type %d", type);
493 // Finish the buffers
494 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
496 m_object_renderers[i]->FinaliseBuffers();
498 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
499 m_render_dirty = false;