+ if (!m_use_gpu_rendering)
+ {
+ // Dynamically resize CPU rendering target pixels if needed
+ if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
+ {
+ delete [] m_cpu_rendering_pixels;
+ m_cpu_rendering_pixels = new uint8_t[width*height*4];
+ if (m_cpu_rendering_pixels == NULL)
+ Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
+ }
+ // Clear CPU rendering pixels
+ for (int i = 0; i < width*height*4; ++i)
+ m_cpu_rendering_pixels[i] = 255;
+ }
+#ifdef QUADTREE_DISABLED
+ RenderRange(width, height, 0, m_document.ObjectCount());
+#else
+ // Make sure we update the gpu buffers properly.
+ if (m_document.m_document_dirty)
+ {
+ m_render_dirty = m_buffer_dirty = true;
+ m_document.m_document_dirty = false;
+ }
+ RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
+#endif
+ if (!m_use_gpu_rendering)
+ {
+ m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
+ // Debug for great victory (do something similar for GPU and compare?)
+ //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
+ }
+ m_cached_display.UnBind(); // resets render target to the screen
+ m_cached_display.Blit(); // blit FrameBuffer to screen
+ m_buffer_dirty = false;
+ glPopDebugGroup();
+
+#ifndef CONTROLPANEL_DISABLED
+ // The powers that be suggest that this may be causing of the segfaults.
+ //ControlPanel::Update();
+#endif //CONTROLPANEL_DISABLED
+ //Debug("Completed Render");
+
+}
+
+#ifndef QUADTREE_DISABLED
+void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
+{
+ Rect old_bounds = m_bounds;
+ if (node == QUADTREE_EMPTY) return;
+ if (!remaining_depth) return;
+ m_bounds_dirty = true;
+ QuadTreeIndex overlay = node;
+ while(overlay != -1)
+ {
+ //Debug("Rendering QT node %d, (overlay %d, objs: %d -- %d)\n", node, overlay, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
+ if (m_document.GetQuadTree().nodes[overlay].render_dirty)
+ m_buffer_dirty = m_render_dirty = true;
+ RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
+ const_cast<bool&>(m_document.GetQuadTree().nodes[overlay].render_dirty) = false;
+ overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
+ }
+
+ if (m_bounds.Intersects(Rect(1,1,1,1)))
+ {
+ m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
+ }
+ m_bounds = old_bounds;
+ if (m_bounds.Intersects(Rect(1,0,1,1)))
+ {
+ m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
+ }
+ m_bounds = old_bounds;
+ if (m_bounds.Intersects(Rect(0,1,1,1)))
+ {
+ m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
+ }
+ m_bounds = old_bounds;
+ m_bounds_dirty = true;
+
+#if 0
+ m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
+ m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
+ m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
+ m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
+ m_bounds_dirty = true;
+ RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
+ m_bounds = old_bounds;
+ m_bounds_dirty = true;
+#endif
+}
+#endif
+
+void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
+{
+ // We don't want to render an empty range,
+ // so don't waste time setting up everything.
+ if (first_obj == last_obj) return;
+ PROFILE_SCOPE("View::RenderRange");
+ glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
+
+ g_profiler.AddCounter("Objects Rendered", last_obj-first_obj);
+
+ if (m_render_dirty) // document has changed
+ PrepareRender();
+
+
+ if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
+ {
+ if (m_use_gpu_rendering)
+ UpdateObjBoundsVBO(first_obj, last_obj);
+ }