c5a18413c908334343a73a291f6dd4db842b341a
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2
3 #include "SDL_opengl.h"
4
5 using namespace IPDF;
6 using namespace std;
7
8 void View::Translate(Real x, Real y)
9 {
10         x *= m_bounds.w;
11         y *= m_bounds.h;
12         m_bounds.x += x;
13         m_bounds.y += y;
14         Debug("View Bounds => %s", m_bounds.Str().c_str());
15         if (!m_use_gpu_transform)
16                 m_bounds_dirty = true;
17 }
18
19 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
20 {
21         // x and y are coordinates in the window
22         // Convert to local coords.
23         x *= m_bounds.w;
24         y *= m_bounds.h;
25         x += m_bounds.x;
26         y += m_bounds.y;
27         
28         //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
29         
30         Real top = y - m_bounds.y;
31         Real left = x - m_bounds.x;
32         
33         top *= scaleAmt;
34         left *= scaleAmt;
35         
36         m_bounds.x = x - left;
37         m_bounds.y = y - top;
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;
43 }
44
45 Rect View::TransformToViewCoords(const Rect& inp) const
46 {
47         Rect out;
48         out.x = (inp.x - m_bounds.x) / m_bounds.w;
49         out.y = (inp.y - m_bounds.y) / m_bounds.h;
50
51         out.w = inp.w / m_bounds.w;
52         out.h = inp.h / m_bounds.h;
53         return out;
54 }
55
56 void View::DrawGrid()
57 {
58         // Draw some grid lines at fixed pixel positions
59         glMatrixMode(GL_PROJECTION);
60         glLoadIdentity();
61         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
62         glMatrixMode(GL_MODELVIEW);
63         glLoadIdentity();
64
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)
68         {
69                 glBegin(GL_LINES);
70                 glVertex2f(i*(1.0/num_lines), 0.0);
71                 glVertex2f(i*(1.0/num_lines), 1.0);
72                 glEnd();
73                 glBegin(GL_LINES);
74                 glVertex2f(0.0,i*(1.0/num_lines));
75                 glVertex2f(1.0,i*(1.0/num_lines));
76                 glEnd();
77         
78         }
79 }
80
81 void glPrimitiveRestartIndex(GLuint index);
82
83 void View::Render()
84 {
85         
86         glMatrixMode(GL_PROJECTION);
87         glLoadIdentity();
88         if (m_use_gpu_transform)
89         {
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);
91         }
92         else
93         {
94                 glOrtho(0,1,1,0,-1,1);
95         }
96
97         if (m_bounds_dirty)
98                 ReRender();
99
100         glMatrixMode(GL_MODELVIEW);
101         glLoadIdentity();
102         if (m_colour.a < 1.0f)
103         {
104                 glEnable(GL_BLEND);
105                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106         }
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)
118         {
119                 glDisable(GL_BLEND);
120         }
121
122
123 }
124
125 void View::ReRender()
126 {
127         static bool debug_output_done = false;
128         if (!debug_output_done)
129         {
130                 //m_document.DebugDumpObjects();
131                 debug_output_done = true;
132
133                 m_vertex_buffer.SetType(GraphicsBuffer::BufferTypeVertex);
134                 m_index_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
135                 m_index_buffer.SetType(GraphicsBuffer::BufferTypeIndex);
136
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);
139         }
140         m_rendered_filled = m_rendered_outline = 0;
141         
142         if (m_use_gpu_transform)
143         {
144                 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
145         }
146         else
147         {
148                 m_vertex_buffer.SetUsage(GraphicsBuffer::BufferUsageDynamicDraw);
149         }
150
151
152         //DrawGrid(); // Draw the gridlines
153
154
155
156         float *vertexData = (float*)m_vertex_buffer.Map(false, true, true);
157         uint32_t *indexData = (uint32_t*)m_index_buffer.Map(false, true, true);
158
159         uint32_t currentIndex = 0;
160         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
161         {
162                 if (m_document.m_objects.types[id] != RECT_FILLED)
163                         continue;
164                 Rect obj_bounds;
165                 if (m_use_gpu_transform)
166                 {
167                         obj_bounds = m_document.m_objects.bounds[id];
168                 }
169                 else
170                 {
171                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
172                 }
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++;
181
182                 *indexData = currentIndex; indexData++;
183                 *indexData = currentIndex+1; indexData++;
184                 *indexData = currentIndex+3; indexData++;
185                 *indexData = currentIndex+2; indexData++;
186                 *indexData = 0xFFFFFFFF; // Primitive restart.
187                 indexData++;
188                 currentIndex += 4;
189                 m_rendered_filled++;
190
191         }
192         
193         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
194         {
195                 if (m_document.m_objects.types[id] != RECT_OUTLINE)
196                         continue;
197                 Rect obj_bounds;
198                 if (m_use_gpu_transform)
199                 {
200                         obj_bounds = m_document.m_objects.bounds[id];
201                 }
202                 else
203                 {
204                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
205                 }
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++;
214
215                 *indexData = currentIndex; indexData++;
216                 *indexData = currentIndex+1; indexData++;
217                 *indexData = currentIndex+2; indexData++;
218                 *indexData = currentIndex+3; indexData++;
219                 *indexData = 0xFFFFFFFF; // Primitive restart.
220                 indexData++;
221                 currentIndex += 4;
222                 m_rendered_outline++;
223         }
224         m_vertex_buffer.UnMap();
225         m_index_buffer.UnMap();
226
227         m_bounds_dirty = false;
228
229 }

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