fd26b134c319633410cf324c17b663287c0f6359
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2 #include "bufferbuilder.h"
3
4 #include "gl_core44.h"
5
6 using namespace IPDF;
7 using namespace std;
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"
14
15 void View::Translate(Real x, Real y)
16 {
17         x *= m_bounds.w;
18         y *= m_bounds.h;
19         m_bounds.x += x;
20         m_bounds.y += y;
21         Debug("View Bounds => %s", m_bounds.Str().c_str());
22         if (!m_use_gpu_transform)
23         {
24                 m_buffer_dirty = true;
25         }
26         m_bounds_dirty = true;
27 }
28
29 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
30 {
31         // x and y are coordinates in the window
32         // Convert to local coords.
33         x *= m_bounds.w;
34         y *= m_bounds.h;
35         x += m_bounds.x;
36         y += m_bounds.y;
37         
38         //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
39         
40         Real top = y - m_bounds.y;
41         Real left = x - m_bounds.x;
42         
43         top *= scaleAmt;
44         left *= scaleAmt;
45         
46         m_bounds.x = x - left;
47         m_bounds.y = y - top;
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;
54 }
55
56 Rect View::TransformToViewCoords(const Rect& inp) const
57 {
58         Rect out;
59         out.x = (inp.x - m_bounds.x) / m_bounds.w;
60         out.y = (inp.y - m_bounds.y) / m_bounds.h;
61
62         out.w = inp.w / m_bounds.w;
63         out.h = inp.h / m_bounds.h;
64         return out;
65 }
66
67 void View::DrawGrid()
68 {
69         //TODO: Implement this with OpenGL 3.1+
70 #if 0
71         // Draw some grid lines at fixed pixel positions
72         glMatrixMode(GL_PROJECTION);
73         glLoadIdentity();
74         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
75         glMatrixMode(GL_MODELVIEW);
76         glLoadIdentity();
77
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)
81         {
82                 glBegin(GL_LINES);
83                 glVertex2f(i*(1.0/num_lines), 0.0);
84                 glVertex2f(i*(1.0/num_lines), 1.0);
85                 glEnd();
86                 glBegin(GL_LINES);
87                 glVertex2f(0.0,i*(1.0/num_lines));
88                 glVertex2f(1.0,i*(1.0/num_lines));
89                 glEnd();
90         
91         }
92 #endif
93 }
94
95 void View::Render(int width, int height)
96 {
97         if (width != m_cached_display.GetWidth() || height != m_cached_display.GetHeight())
98         {
99                 m_cached_display.Create(width, height);
100                 m_bounds_dirty = true;
101         }
102
103         if (!m_bounds_dirty)
104         {
105                 m_cached_display.UnBind();
106                 m_cached_display.Blit();
107                 return;
108         }
109         m_cached_display.Bind();
110         m_cached_display.Clear();
111
112         if (!m_render_inited)
113                 PrepareRender();
114
115         if (m_buffer_dirty)
116                 UpdateObjBoundsVBO();
117
118         if (m_bounds_dirty)
119         {
120                 if (m_use_gpu_transform)
121                 {
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);
124                 }
125                 else
126                 {
127                         GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f};
128                         m_bounds_ubo.Upload(sizeof(float)*4, glbounds);
129                 }
130         }
131         m_bounds_dirty = false;
132
133         if (m_colour.a < 1.0f)
134         {
135                 glEnable(GL_BLEND);
136                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137         }
138         m_objbounds_vbo.Bind();
139         m_bounds_ubo.Bind();
140         glEnableVertexAttribArray(0);
141         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
142
143         // Filled Circles
144         m_circle_filled_shader.Use();
145         m_circle_ibo.Bind();
146         glDrawElements(GL_LINES, m_rendered_circle*2, GL_UNSIGNED_INT, 0);
147
148         // Filled Rectangles
149         m_rect_filled_shader.Use();
150         m_filled_ibo.Bind();
151         glDrawElements(GL_LINES, m_rendered_filled*2, GL_UNSIGNED_INT, 0);
152
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);
157
158         glDisableVertexAttribArray(0);
159         if (m_colour.a < 1.0f)
160         {
161                 glDisable(GL_BLEND);
162         }
163         m_cached_display.UnBind();
164         m_cached_display.Blit();
165
166 }
167
168 struct GPUObjBounds
169 {
170         float x0, y0;
171         float x1, y1;
172 };
173
174 void View::UpdateObjBoundsVBO()
175 {
176         m_objbounds_vbo.Invalidate();
177         m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
178         if (m_use_gpu_transform)
179         {
180                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
181         }
182         else
183         {
184                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
185         }
186         m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
187
188         BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.Map(false, true, true), m_objbounds_vbo.GetSize());
189
190         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
191         {
192                 Rect obj_bounds;
193                 if (m_use_gpu_transform)
194                 {
195                         obj_bounds = m_document.m_objects.bounds[id];
196                 }
197                 else
198                 {
199                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
200                 }
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)
206                 };
207                 obj_bounds_builder.Add(gpu_bounds);
208
209         }
210         m_objbounds_vbo.UnMap();
211         m_buffer_dirty = false;
212 }
213
214 void View::PrepareRender()
215 {
216         // TODO: Error check here.
217
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);
222
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);
227
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);
232
233         m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
234         m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
235
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()); 
240
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());
245
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());
250
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)
254         {
255                 if (m_document.m_objects.types[id] == RECT_OUTLINE)
256                 {
257                         outline_builder.Add(currentIndex++);
258                         outline_builder.Add(currentIndex++);
259                         m_rendered_outline++;
260                 }
261                 else if (m_document.m_objects.types[id] == RECT_FILLED)
262                 {
263                         filled_builder.Add(currentIndex++);
264                         filled_builder.Add(currentIndex++);
265                         m_rendered_filled++;
266                 }
267                 else
268                 {
269                         circle_builder.Add(currentIndex++);
270                         circle_builder.Add(currentIndex++);
271                         m_rendered_circle++;
272                 }
273
274         }
275         m_outline_ibo.UnMap();
276         m_filled_ibo.UnMap();
277         m_circle_ibo.UnMap();
278
279         m_render_inited = true;
280 }

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