2 #include "bufferbuilder.h"
8 #define RECT_VERT "shaders/rect_vert.glsl"
9 #define RECT_FRAG "shaders/rect_frag.glsl"
10 #define RECT_OUTLINE_GEOM "shaders/rect_outline_geom.glsl"
11 #define RECT_FILLED_GEOM "shaders/rect_filled_geom.glsl"
12 #define CIRCLE_FILLED_GEOM "shaders/circle_filled_geom.glsl"
13 #define CIRCLE_FRAG "shaders/circle_frag.glsl"
15 void View::Translate(Real x, Real y)
21 Debug("View Bounds => %s", m_bounds.Str().c_str());
22 if (!m_use_gpu_transform)
24 m_buffer_dirty = true;
26 m_bounds_dirty = true;
29 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
31 // x and y are coordinates in the window
32 // Convert to local coords.
38 //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
40 Real top = y - m_bounds.y;
41 Real left = x - m_bounds.x;
46 m_bounds.x = x - left;
48 m_bounds.w *= scaleAmt;
49 m_bounds.h *= scaleAmt;
50 Debug("View Bounds => %s", m_bounds.Str().c_str());
51 if (!m_use_gpu_transform)
52 m_buffer_dirty = true;
53 m_bounds_dirty = true;
56 Rect View::TransformToViewCoords(const Rect& inp) const
59 out.x = (inp.x - m_bounds.x) / m_bounds.w;
60 out.y = (inp.y - m_bounds.y) / m_bounds.h;
62 out.w = inp.w / m_bounds.w;
63 out.h = inp.h / m_bounds.h;
69 //TODO: Implement this with OpenGL 3.1+
71 // Draw some grid lines at fixed pixel positions
72 glMatrixMode(GL_PROJECTION);
74 glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
75 glMatrixMode(GL_MODELVIEW);
78 glColor4f(0.9,0.9,0.9,0.1);
79 const float num_lines = 50.0;
80 for (float i = 0; i < num_lines; ++i)
83 glVertex2f(i*(1.0/num_lines), 0.0);
84 glVertex2f(i*(1.0/num_lines), 1.0);
87 glVertex2f(0.0,i*(1.0/num_lines));
88 glVertex2f(1.0,i*(1.0/num_lines));
95 void View::Render(int width, int height)
97 if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
99 m_cached_display.Create(width, height);
100 m_bounds_dirty = true;
105 m_cached_display.UnBind();
106 m_cached_display.Blit();
109 m_cached_display.Bind();
110 m_cached_display.Clear();
112 if (!m_render_inited)
116 UpdateObjBoundsVBO();
120 if (m_use_gpu_transform)
122 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))};
123 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
127 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
128 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
131 m_bounds_dirty = false;
133 if (m_colour.a < 1.0f)
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 m_objbounds_vbo.Bind();
140 glEnableVertexAttribArray(0);
141 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
144 m_circle_filled_shader.Use();
146 glDrawElements(GL_LINES, m_rendered_circle*2, GL_UNSIGNED_INT, 0);
149 m_rect_filled_shader.Use();
151 glDrawElements(GL_LINES, m_rendered_filled*2, GL_UNSIGNED_INT, 0);
153 // Rectangle Outlines
154 m_rect_outline_shader.Use();
155 m_outline_ibo.Bind();
156 glDrawElements(GL_LINES, m_rendered_outline*2, GL_UNSIGNED_INT, 0);
158 glDisableVertexAttribArray(0);
159 if (m_colour.a < 1.0f)
163 m_cached_display.UnBind();
164 m_cached_display.Blit();
174 void View::UpdateObjBoundsVBO()
176 m_objbounds_vbo.Invalidate();
177 m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
178 if (m_use_gpu_transform)
180 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
184 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
186 m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
188 BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
190 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
193 if (m_use_gpu_transform)
195 obj_bounds = m_document.m_objects.bounds[id];
199 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
201 GPUObjBounds gpu_bounds = {
202 (float)Float(obj_bounds.x),
203 (float)Float(obj_bounds.y),
204 (float)Float(obj_bounds.x + obj_bounds.w),
205 (float)Float(obj_bounds.y + obj_bounds.h)
207 obj_bounds_builder.Add(gpu_bounds);
210 m_objbounds_vbo.UnMap();
211 m_buffer_dirty = false;
214 void View::PrepareRender()
216 // TODO: Error check here.
218 m_rect_outline_shader.AttachShaderPrograms(RECT_OUTLINE_GEOM, RECT_VERT, RECT_FRAG);
219 m_rect_outline_shader.Link();
220 m_rect_outline_shader.Use();
221 glUniform4f(m_rect_outline_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
223 m_rect_filled_shader.AttachShaderPrograms(RECT_FILLED_GEOM, RECT_VERT, RECT_FRAG);
224 m_rect_filled_shader.Link();
225 m_rect_filled_shader.Use();
226 glUniform4f(m_rect_filled_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
228 m_circle_filled_shader.AttachShaderPrograms(CIRCLE_FILLED_GEOM, RECT_VERT, CIRCLE_FRAG);
229 m_circle_filled_shader.Link();
230 m_circle_filled_shader.Use();
231 glUniform4f(m_circle_filled_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
233 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
234 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
236 m_outline_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
237 m_outline_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
238 m_outline_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
239 BufferBuilder<uint32_t> outline_builder(m_outline_ibo.Map(false, true, true), m_outline_ibo.GetSize());
241 m_filled_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
242 m_filled_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
243 m_filled_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
244 BufferBuilder<uint32_t> filled_builder(m_filled_ibo.Map(false, true, true), m_filled_ibo.GetSize());
246 m_circle_ibo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
247 m_circle_ibo.SetType(GraphicsBuffer::BufferTypeIndex);
248 m_circle_ibo.Resize(m_document.ObjectCount() * 2 * sizeof(uint32_t));
249 BufferBuilder<uint32_t> circle_builder(m_circle_ibo.Map(false, true, true), m_circle_ibo.GetSize());
251 m_rendered_filled = m_rendered_outline = m_rendered_circle = 0;
252 uint32_t currentIndex = 0;
253 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
255 if (m_document.m_objects.types[id] == RECT_OUTLINE)
257 outline_builder.Add(currentIndex++);
258 outline_builder.Add(currentIndex++);
259 m_rendered_outline++;
261 else if (m_document.m_objects.types[id] == RECT_FILLED)
263 filled_builder.Add(currentIndex++);
264 filled_builder.Add(currentIndex++);
269 circle_builder.Add(currentIndex++);
270 circle_builder.Add(currentIndex++);
275 m_outline_ibo.UnMap();
276 m_filled_ibo.UnMap();
277 m_circle_ibo.UnMap();
279 m_render_inited = true;