Tester for Beziers
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2
3 #include "gl_core44.h"
4
5 using namespace IPDF;
6 using namespace std;
7 #define RECT_VERT \
8         "#version 140\n"\
9         "#extension GL_ARB_shading_language_420pack : require\n"\
10         "#extension GL_ARB_explicit_attrib_location : require\n"\
11         "\n"\
12         "layout(std140, binding=0) uniform ViewBounds\n"\
13         "{\n"\
14         "\tfloat bounds_x;\n"\
15         "\tfloat bounds_y;\n"\
16         "\tfloat bounds_w;\n"\
17         "\tfloat bounds_h;\n"\
18         "};\n"\
19         "\n"\
20         "layout(location = 0) in vec2 position;\n"\
21         "\n"\
22         "void main()\n"\
23         "{\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"\
32         "}\n"
33
34 #define RECT_FRAG \
35         "#version 140\n"\
36         "\n"\
37         "out vec4 output_colour;\n"\
38         "\n"\
39         "uniform vec4 colour;\n"\
40         "\n"\
41         "void main()\n"\
42         "{\n"\
43         "\toutput_colour = colour;\n"\
44         "}\n"
45
46 void View::Translate(Real x, Real y)
47 {
48         x *= m_bounds.w;
49         y *= m_bounds.h;
50         m_bounds.x += x;
51         m_bounds.y += y;
52         Debug("View Bounds => %s", m_bounds.Str().c_str());
53         if (!m_use_gpu_transform)
54         {
55                 m_buffer_dirty = true;
56         }
57         m_bounds_dirty = true;
58 }
59
60 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
61 {
62         // x and y are coordinates in the window
63         // Convert to local coords.
64         x *= m_bounds.w;
65         y *= m_bounds.h;
66         x += m_bounds.x;
67         y += m_bounds.y;
68         
69         //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
70         
71         Real top = y - m_bounds.y;
72         Real left = x - m_bounds.x;
73         
74         top *= scaleAmt;
75         left *= scaleAmt;
76         
77         m_bounds.x = x - left;
78         m_bounds.y = y - top;
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;
85 }
86
87 Rect View::TransformToViewCoords(const Rect& inp) const
88 {
89         Rect out;
90         out.x = (inp.x - m_bounds.x) / m_bounds.w;
91         out.y = (inp.y - m_bounds.y) / m_bounds.h;
92
93         out.w = inp.w / m_bounds.w;
94         out.h = inp.h / m_bounds.h;
95         return out;
96 }
97
98 void View::DrawGrid()
99 {
100         //TODO: Implement this with OpenGL 3.1+
101 #if 0
102         // Draw some grid lines at fixed pixel positions
103         glMatrixMode(GL_PROJECTION);
104         glLoadIdentity();
105         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
106         glMatrixMode(GL_MODELVIEW);
107         glLoadIdentity();
108
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)
112         {
113                 glBegin(GL_LINES);
114                 glVertex2f(i*(1.0/num_lines), 0.0);
115                 glVertex2f(i*(1.0/num_lines), 1.0);
116                 glEnd();
117                 glBegin(GL_LINES);
118                 glVertex2f(0.0,i*(1.0/num_lines));
119                 glVertex2f(1.0,i*(1.0/num_lines));
120                 glEnd();
121         
122         }
123 #endif
124 }
125
126 void View::Render(int width, int height)
127 {
128         if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
129         {
130                 m_cached_display.Create(width, height);
131                 m_bounds_dirty = true;
132         }
133
134         if (!m_bounds_dirty)
135         {
136                 m_cached_display.UnBind();
137                 m_cached_display.Blit();
138                 return;
139         }
140         m_cached_display.Bind();
141         m_cached_display.Clear();
142
143         if (m_buffer_dirty)
144                 ReRender();
145
146         if (m_bounds_dirty)
147         {
148                 if (m_use_gpu_transform)
149                 {
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);
152                 }
153                 else
154                 {
155                         GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
156                         m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
157                 }
158         }
159         m_bounds_dirty = false;
160
161         if (m_colour.a < 1.0f)
162         {
163                 glEnable(GL_BLEND);
164                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165         }
166         m_vertex_buffer.Bind();
167         m_index_buffer.Bind();
168         m_bounds_ubo.Bind();
169         m_rect_shader.Use();
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)
179         {
180                 glDisable(GL_BLEND);
181         }
182         m_cached_display.UnBind();
183         m_cached_display.Blit();
184
185 }
186
187 void View::ReRender()
188 {
189         static bool debug_output_done = false;
190         if (!debug_output_done)
191         {
192                 //m_document.DebugDumpObjects();
193                 debug_output_done = true;
194
195                 // TODO: Error check here.
196                 m_rect_shader.AttachVertexProgram(RECT_VERT);
197                 m_rect_shader.AttachFragmentProgram(RECT_FRAG);
198                 m_rect_shader.Link();
199                 m_rect_shader.Use();
200                 glUniform4f(m_rect_shader.GetUniformLocation("colour"), m_colour.r, m_colour.g, m_colour.b, m_colour.a);
201
202                 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
203                 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
204
205                 m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
206                 m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
207                 m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
208
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);
211         }
212         m_rendered_filled = m_rendered_outline = 0;
213         
214         if (m_use_gpu_transform)
215         {
216                 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
217         }
218         else
219         {
220                 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
221         }
222
223
224         //DrawGrid(); // Draw the gridlines
225
226
227
228         float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
229         uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
230
231         uint32_t currentIndex = 0;
232         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
233         {
234                 if (m_document.m_objects.types[id] != RECT_FILLED)
235                         continue;
236                 Rect obj_bounds;
237                 if (m_use_gpu_transform)
238                 {
239                         obj_bounds = m_document.m_objects.bounds[id];
240                 }
241                 else
242                 {
243                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
244                 }
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++;
253
254                 *indexData = currentIndex; indexData++;
255                 *indexData = currentIndex+1; indexData++;
256                 *indexData = currentIndex+3; indexData++;
257                 *indexData = currentIndex+2; indexData++;
258                 *indexData = 0xFFFFFFFF; // Primitive restart.
259                 indexData++;
260                 currentIndex += 4;
261                 m_rendered_filled++;
262
263         }
264         
265         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
266         {
267                 if (m_document.m_objects.types[id] != RECT_OUTLINE)
268                         continue;
269                 Rect obj_bounds;
270                 if (m_use_gpu_transform)
271                 {
272                         obj_bounds = m_document.m_objects.bounds[id];
273                 }
274                 else
275                 {
276                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
277                 }
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++;
286
287                 *indexData = currentIndex; indexData++;
288                 *indexData = currentIndex+1; indexData++;
289                 *indexData = currentIndex+2; indexData++;
290                 *indexData = currentIndex+3; indexData++;
291                 *indexData = 0xFFFFFFFF; // Primitive restart.
292                 indexData++;
293                 currentIndex += 4;
294                 m_rendered_outline++;
295         }
296         m_vertex_buffer.UnMap();
297         m_index_buffer.UnMap();
298
299         m_buffer_dirty = false;
300
301 }

UCC git Repository :: git.ucc.asn.au