2 #include "bufferbuilder.h"
11 * Allocates memory for ObjectRenderers
12 * @param document - The document to associate the View with
13 * @param bounds - Initial bounds of the View
14 * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
16 View::View(Document & document, const Rect & bounds, const Colour & colour)
17 : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true),
18 m_render_dirty(true), m_document(document), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(),
19 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES)
21 Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
23 // Create ObjectRenderers - new's match delete's in View::~View
24 // Ok, look, this may seem disgusting, but go look at View::PrepareRender before you murder me
25 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
26 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
27 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
29 // To add rendering for a new type of object;
30 // 1. Add enum to ObjectType in ipdf.h
31 // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
38 * Frees memory used by ObjectRenderers
42 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
44 //delete m_object_renderers[i];
46 m_object_renderers.clear();
51 * @param x, y - Amount to translate
53 void View::Translate(Real x, Real y)
59 Debug("View Bounds => %s", m_bounds.Str().c_str());
60 if (!m_use_gpu_transform)
61 m_buffer_dirty = true;
62 m_bounds_dirty = true;
66 * Scale the View at a point
67 * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
68 * @param scale_amount - Amount to scale by
70 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
72 // x and y are coordinates in the window
73 // Convert to local coords.
79 Real top = y - m_bounds.y;
80 Real left = x - m_bounds.x;
85 m_bounds.x = x - left;
87 m_bounds.w *= scale_amount;
88 m_bounds.h *= scale_amount;
89 Debug("View Bounds => %s", m_bounds.Str().c_str());
90 if (!m_use_gpu_transform)
91 m_buffer_dirty = true;
92 m_bounds_dirty = true;
96 * Transform a point in the document to a point relative to the top left corner of the view
97 * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
98 * @param inp - Input Rect {x,y,w,h} in the document
99 * @returns output Rect {x,y,w,h} in the View
101 Rect View::TransformToViewCoords(const Rect& inp) const
104 out.x = (inp.x - m_bounds.x) / m_bounds.w;
105 out.y = (inp.y - m_bounds.y) / m_bounds.h;
106 out.w = inp.w / m_bounds.w;
107 out.h = inp.h / m_bounds.h;
113 * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
114 * Otherwise just Blits the cached FrameBuffer
115 * @param width - Width of View to render
116 * @param height - Height of View to render
118 void View::Render(int width, int height)
120 // View dimensions have changed (ie: Window was resized)
121 if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
123 m_cached_display.Create(width, height);
124 m_bounds_dirty = true;
127 // View bounds have not changed; blit the FrameBuffer as it is
130 m_cached_display.UnBind();
131 m_cached_display.Blit();
135 // Bind FrameBuffer for rendering, and clear it
136 m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
137 m_cached_display.Clear();
140 if (m_render_dirty) // document has changed
143 if (m_buffer_dirty) // object bounds have changed
144 UpdateObjBoundsVBO();
146 if (m_use_gpu_transform)
148 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))};
149 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
153 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
154 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
156 m_bounds_dirty = false;
160 if (m_use_gpu_rendering)
162 if (m_colour.a < 1.0f)
165 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167 m_objbounds_vbo.Bind();
169 glEnableVertexAttribArray(0);
170 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
172 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
174 m_object_renderers[i]->RenderUsingGPU();
177 glDisableVertexAttribArray(0);
178 if (m_colour.a < 1.0f)
183 else // Rasterise on CPU then blit texture to GPU
185 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
187 m_object_renderers[i]->RenderUsingCPU();
190 m_cached_display.UnBind(); // resets render target to the screen
191 m_cached_display.Blit(); // blit FrameBuffer to screen
194 void View::UpdateObjBoundsVBO()
197 m_objbounds_vbo.Invalidate();
198 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
199 if (m_use_gpu_transform)
201 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
205 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
207 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
209 BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
211 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
214 if (m_use_gpu_transform)
216 obj_bounds = m_document.m_objects.bounds[id];
220 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
222 GPUObjBounds gpu_bounds = {
223 (float)Float(obj_bounds.x),
224 (float)Float(obj_bounds.y),
225 (float)Float(obj_bounds.x + obj_bounds.w),
226 (float)Float(obj_bounds.y + obj_bounds.h)
228 obj_bounds_builder.Add(gpu_bounds);
231 m_objbounds_vbo.UnMap();
232 m_buffer_dirty = false;
235 * Prepare the document for rendering
236 * Will be called on View::Render if m_render_dirty is set
237 * (Called at least once, on the first Render)
239 void View::PrepareRender()
241 // Prepare bounds vbo
242 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
243 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
245 // Instead of having each ObjectRenderer go through the whole document
246 // we initialise them, go through the document once adding to the appropriate Renderers
247 // and then finalise them
248 // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
250 // Prepare the buffers
251 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
253 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
256 // Add objects from Document to buffers
257 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
259 ObjectType type = m_document.m_objects.types[id];
260 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
261 // (Also, Wow I just actually used std::vector::at())
264 // Finish the buffers
265 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
267 m_object_renderers[i]->FinaliseBuffers();
269 m_render_dirty = false;