9 "#extension GL_ARB_shading_language_420pack : require\n"\
10 "#extension GL_ARB_explicit_attrib_location : require\n"\
12 "layout(std140, binding=0) uniform ViewBounds\n"\
14 "\tfloat bounds_x;\n"\
15 "\tfloat bounds_y;\n"\
16 "\tfloat bounds_w;\n"\
17 "\tfloat bounds_h;\n"\
20 "layout(location = 0) in vec2 position;\n"\
24 "\tvec2 transformed_position;\n"\
25 "\ttransformed_position.x = (position.x - bounds_x) / bounds_w;\n"\
26 "\ttransformed_position.y = (position.y - bounds_y) / bounds_h;\n"\
27 "\t// Transform to clip coordinates (-1,1, -1,1).\n"\
28 "\tgl_Position.x = (transformed_position.x*2) - 1;\n"\
29 "\tgl_Position.y = 1 - (transformed_position.y*2);\n"\
30 "\tgl_Position.z = 0.0;\n"\
31 "\tgl_Position.w = 1.0;\n"\
37 "out vec4 output_colour;\n"\
39 "uniform vec4 colour;\n"\
43 "\toutput_colour = colour;\n"\
46 void View::Translate(Real x, Real y)
52 Debug("View Bounds => %s", m_bounds.Str().c_str());
53 if (!m_use_gpu_transform)
55 m_buffer_dirty = true;
57 m_bounds_dirty = true;
60 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
62 // x and y are coordinates in the window
63 // Convert to local coords.
69 //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
71 Real top = y - m_bounds.y;
72 Real left = x - m_bounds.x;
77 m_bounds.x = x - left;
79 m_bounds.w *= scaleAmt;
80 m_bounds.h *= scaleAmt;
81 Debug("View Bounds => %s", m_bounds.Str().c_str());
82 if (!m_use_gpu_transform)
83 m_buffer_dirty = true;
84 m_bounds_dirty = true;
87 Rect View::TransformToViewCoords(const Rect& inp) const
90 out.x = (inp.x - m_bounds.x) / m_bounds.w;
91 out.y = (inp.y - m_bounds.y) / m_bounds.h;
93 out.w = inp.w / m_bounds.w;
94 out.h = inp.h / m_bounds.h;
100 //TODO: Implement this with OpenGL 3.1+
102 // Draw some grid lines at fixed pixel positions
103 glMatrixMode(GL_PROJECTION);
105 glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
106 glMatrixMode(GL_MODELVIEW);
109 glColor4f(0.9,0.9,0.9,0.1);
110 const float num_lines = 50.0;
111 for (float i = 0; i < num_lines; ++i)
114 glVertex2f(i*(1.0/num_lines), 0.0);
115 glVertex2f(i*(1.0/num_lines), 1.0);
118 glVertex2f(0.0,i*(1.0/num_lines));
119 glVertex2f(1.0,i*(1.0/num_lines));
126 void View::Render(int width, int height)
128 if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
130 m_cached_display.Create(width, height);
131 m_bounds_dirty = true;
136 m_cached_display.UnBind();
137 m_cached_display.Blit();
140 m_cached_display.Bind();
141 m_cached_display.Clear();
148 if (m_use_gpu_transform)
150 GLfloat glbounds[] = {Float(m_bounds.x), Float(m_bounds.y), Float(m_bounds.w), Float(m_bounds.h)};
151 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
155 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
156 m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
159 m_bounds_dirty = false;
161 if (m_colour.a < 1.0f)
164 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166 m_vertex_buffer.Bind();
167 m_index_buffer.Bind();
170 glEnable(GL_PRIMITIVE_RESTART);
171 glPrimitiveRestartIndex(0xFFFFFFFF);
172 glEnableVertexAttribArray(0);
173 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
174 glDrawElements(GL_TRIANGLE_STRIP, m_rendered_filled * 5, GL_UNSIGNED_INT, 0);
175 glDrawElements(GL_LINE_LOOP, m_rendered_outline*5, GL_UNSIGNED_INT,(void*)(sizeof(uint32_t)*m_rendered_filled*5));
176 glDisableVertexAttribArray(0);
177 glDisable(GL_PRIMITIVE_RESTART);
178 if (m_colour.a < 1.0f)
182 m_cached_display.UnBind();
183 m_cached_display.Blit();
187 void View::ReRender()
189 static bool debug_output_done = false;
190 if (!debug_output_done)
192 //m_document.DebugDumpObjects();
193 debug_output_done = true;
195 // TODO: Error check here.
196 m_rect_shader.AttachVertexProgram(RECT_VERT);
197 m_rect_shader.AttachFragmentProgram(RECT_FRAG);
198 m_rect_shader.Link();
200 glUniform4f(m_rect_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
202 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
203 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
205 m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
206 m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
207 m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
209 m_vertex_buffer.Upload(m_document.ObjectCount() * 8 * sizeof(float), NULL);
210 m_index_buffer.Upload(m_document.ObjectCount() * 5 * sizeof(uint32_t), NULL);
212 m_rendered_filled = m_rendered_outline = 0;
214 if (m_use_gpu_transform)
216 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
220 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
224 //DrawGrid(); // Draw the gridlines
228 float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
229 uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
231 uint32_t currentIndex = 0;
232 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
234 if (m_document.m_objects.types[id] != RECT_FILLED)
237 if (m_use_gpu_transform)
239 obj_bounds = m_document.m_objects.bounds[id];
243 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
245 *vertexData = Float(obj_bounds.x); vertexData++;
246 *vertexData = Float(obj_bounds.y); vertexData++;
247 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
248 *vertexData = Float(obj_bounds.y); vertexData++;
249 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
250 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
251 *vertexData = Float(obj_bounds.x); vertexData++;
252 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
254 *indexData = currentIndex; indexData++;
255 *indexData = currentIndex+1; indexData++;
256 *indexData = currentIndex+3; indexData++;
257 *indexData = currentIndex+2; indexData++;
258 *indexData = 0xFFFFFFFF; // Primitive restart.
265 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
267 if (m_document.m_objects.types[id] != RECT_OUTLINE)
270 if (m_use_gpu_transform)
272 obj_bounds = m_document.m_objects.bounds[id];
276 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
278 *vertexData = Float(obj_bounds.x); vertexData++;
279 *vertexData = Float(obj_bounds.y); vertexData++;
280 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
281 *vertexData = Float(obj_bounds.y); vertexData++;
282 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
283 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
284 *vertexData = Float(obj_bounds.x); vertexData++;
285 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
287 *indexData = currentIndex; indexData++;
288 *indexData = currentIndex+1; indexData++;
289 *indexData = currentIndex+2; indexData++;
290 *indexData = currentIndex+3; indexData++;
291 *indexData = 0xFFFFFFFF; // Primitive restart.
294 m_rendered_outline++;
296 m_vertex_buffer.UnMap();
297 m_index_buffer.UnMap();
299 m_buffer_dirty = false;