3 #include "SDL_opengl.h"
8 void View::Translate(Real x, Real y)
14 Debug("View Bounds => %s", m_bounds.Str().c_str());
15 if (!m_use_gpu_transform)
16 m_bounds_dirty = true;
19 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
21 // x and y are coordinates in the window
22 // Convert to local coords.
28 //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
30 Real top = y - m_bounds.y;
31 Real left = x - m_bounds.x;
36 m_bounds.x = x - left;
38 m_bounds.w *= scaleAmt;
39 m_bounds.h *= scaleAmt;
40 Debug("View Bounds => %s", m_bounds.Str().c_str());
41 if (!m_use_gpu_transform)
42 m_bounds_dirty = true;
45 Rect View::TransformToViewCoords(const Rect& inp) const
48 out.x = (inp.x - m_bounds.x) / m_bounds.w;
49 out.y = (inp.y - m_bounds.y) / m_bounds.h;
51 out.w = inp.w / m_bounds.w;
52 out.h = inp.h / m_bounds.h;
58 // Draw some grid lines at fixed pixel positions
59 glMatrixMode(GL_PROJECTION);
61 glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
62 glMatrixMode(GL_MODELVIEW);
65 glColor4f(0.9,0.9,0.9,0.1);
66 const float num_lines = 50.0;
67 for (float i = 0; i < num_lines; ++i)
70 glVertex2f(i*(1.0/num_lines), 0.0);
71 glVertex2f(i*(1.0/num_lines), 1.0);
74 glVertex2f(0.0,i*(1.0/num_lines));
75 glVertex2f(1.0,i*(1.0/num_lines));
81 void glPrimitiveRestartIndex(GLuint index);
86 glMatrixMode(GL_PROJECTION);
88 if (m_use_gpu_transform)
90 glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f);
94 glOrtho(0,1,1,0,-1,1);
100 glMatrixMode(GL_MODELVIEW);
102 if (m_colour.a < 1.0f)
105 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
107 glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
108 m_vertex_buffer.Bind();
109 m_index_buffer.Bind();
110 glEnable(GL_PRIMITIVE_RESTART);
111 glPrimitiveRestartIndex(0xFFFFFFFF);
112 glVertexPointer(2, GL_FLOAT, 0, 0);
113 glEnableClientState(GL_VERTEX_ARRAY);
114 glDrawElements(GL_TRIANGLE_STRIP, m_rendered_filled * 5, GL_UNSIGNED_INT, 0);
115 glDrawElements(GL_LINE_LOOP, m_rendered_outline*5, GL_UNSIGNED_INT,(void*)(sizeof(uint32_t)*m_rendered_filled*5));
116 glDisable(GL_PRIMITIVE_RESTART);
117 if (m_colour.a < 1.0f)
125 void View::ReRender()
127 static bool debug_output_done = false;
128 if (!debug_output_done)
130 m_document.DebugDumpObjects();
131 debug_output_done = true;
133 m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
134 m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
135 m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
137 m_vertex_buffer.Upload(m_document.ObjectCount() * 8 * sizeof(float), NULL);
138 m_index_buffer.Upload(m_document.ObjectCount() * 5 * sizeof(uint32_t), NULL);
140 m_rendered_filled = m_rendered_outline = 0;
142 if (m_use_gpu_transform)
144 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
148 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
152 //DrawGrid(); // Draw the gridlines
156 float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
157 uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
159 uint32_t currentIndex = 0;
160 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
162 if (m_document.m_objects.types[id] != RECT_FILLED)
165 if (m_use_gpu_transform)
167 obj_bounds = m_document.m_objects.bounds[id];
171 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
173 *vertexData = Float(obj_bounds.x); vertexData++;
174 *vertexData = Float(obj_bounds.y); vertexData++;
175 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
176 *vertexData = Float(obj_bounds.y); vertexData++;
177 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
178 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
179 *vertexData = Float(obj_bounds.x); vertexData++;
180 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
182 *indexData = currentIndex; indexData++;
183 *indexData = currentIndex+1; indexData++;
184 *indexData = currentIndex+3; indexData++;
185 *indexData = currentIndex+2; indexData++;
186 *indexData = 0xFFFFFFFF; // Primitive restart.
193 for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
195 if (m_document.m_objects.types[id] != RECT_OUTLINE)
198 if (m_use_gpu_transform)
200 obj_bounds = m_document.m_objects.bounds[id];
204 obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
206 *vertexData = Float(obj_bounds.x); vertexData++;
207 *vertexData = Float(obj_bounds.y); vertexData++;
208 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
209 *vertexData = Float(obj_bounds.y); vertexData++;
210 *vertexData = Float(obj_bounds.x) + Float(obj_bounds.w); vertexData++;
211 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
212 *vertexData = Float(obj_bounds.x); vertexData++;
213 *vertexData = Float(obj_bounds.y) + Float(obj_bounds.h); vertexData++;
215 *indexData = currentIndex; indexData++;
216 *indexData = currentIndex+1; indexData++;
217 *indexData = currentIndex+2; indexData++;
218 *indexData = currentIndex+3; indexData++;
219 *indexData = 0xFFFFFFFF; // Primitive restart.
222 m_rendered_outline++;
224 m_vertex_buffer.UnMap();
225 m_index_buffer.UnMap();
227 m_bounds_dirty = false;