Support doing coordinate transforms on the CPU
[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 }
15
16 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
17 {
18         // Convert to local coords.
19         x *= m_bounds.w;
20         y *= m_bounds.h;
21         x += m_bounds.x;
22         y += m_bounds.y;
23         
24         Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
25         
26         Real top = y - m_bounds.y;
27         Real left = x - m_bounds.x;
28         
29         top *= scaleAmt;
30         left *= scaleAmt;
31         
32         m_bounds.x = x - left;
33         m_bounds.y = y - top;
34         m_bounds.w *= scaleAmt;
35         m_bounds.h *= scaleAmt;
36 }
37
38 Rect View::TransformToViewCoords(const Rect& inp) const
39 {
40         Rect out;
41         out.x = (inp.x - m_bounds.x) / m_bounds.w;
42         out.y = (inp.y - m_bounds.y) / m_bounds.h;
43
44         out.w = inp.w / m_bounds.w;
45         out.h = inp.h / m_bounds.h;
46         return out;
47 }
48
49 void View::DrawGrid()
50 {
51         // Draw some grid lines at fixed pixel positions
52         glMatrixMode(GL_PROJECTION);
53         glLoadIdentity();
54         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
55         glMatrixMode(GL_MODELVIEW);
56         glLoadIdentity();
57
58         glColor4f(0.9,0.9,0.9,0.1);
59         const float num_lines = 50.0;
60         for (float i = 0; i < num_lines; ++i)
61         {
62                 glBegin(GL_LINES);
63                 glVertex2f(i*(1.0/num_lines), 0.0);
64                 glVertex2f(i*(1.0/num_lines), 1.0);
65                 glEnd();
66                 glBegin(GL_LINES);
67                 glVertex2f(0.0,i*(1.0/num_lines));
68                 glVertex2f(1.0,i*(1.0/num_lines));
69                 glEnd();
70         
71         }
72 }
73
74 void View::Render()
75 {
76         static bool debug_output_done = false;
77         if (!debug_output_done)
78         {
79                 m_document.DebugDumpObjects();
80                 debug_output_done = true;
81         }
82
83
84         //DrawGrid(); // Draw the gridlines
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         glMatrixMode(GL_MODELVIEW);
97         glLoadIdentity();
98
99         if (m_colour.a < 1.0f)
100         {
101                 glEnable(GL_BLEND);
102                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
103         }
104         glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
105         glBegin(GL_QUADS);
106         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
107         {
108                 if (m_document.m_objects.types[id] != RECT_FILLED)
109                         continue;
110                 Rect obj_bounds;
111                 if (m_use_gpu_transform)
112                 {
113                         obj_bounds = m_document.m_objects.bounds[id];
114                 }
115                 else
116                 {
117                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
118                 }
119                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
120                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
121                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
122                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
123         }
124         glEnd();
125
126         
127         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
128         {
129                 if (m_document.m_objects.types[id] != RECT_OUTLINE)
130                         continue;
131                 Rect obj_bounds;
132                 if (m_use_gpu_transform)
133                 {
134                         obj_bounds = m_document.m_objects.bounds[id];
135                 }
136                 else
137                 {
138                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
139                 }
140                 glBegin(GL_LINE_LOOP);
141                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
142                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
143                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
144                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
145                 glEnd();
146         }
147
148         if (m_colour.a < 1.0f)
149         {
150                 glDisable(GL_BLEND);
151         }
152
153 }

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