Debian Codegraph @

src/Cockpit/render_area_2d.cxx

raw symbols SCIP
1 // RenderArea2D.cxx - a class to manage 2D polygon-based drawing
2 // for a complex instrument (eg. GPS).
3 //
4 // Written by David Luff, started 2005.
5 //
6 // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include "render_area_2d.hxx"
30
31 RA2DPrimitive::RA2DPrimitive() {
32 invert = false;
33 debug = false;
34 }
35
36 RenderArea2D::RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy) {
37 _logx = logx;
38 _logy = logy;
39 _sizex = sizex;
40 _sizey = sizey;
41 _posx = posx;
42 _posy = posy;
43 _clipx1 = 0;
44 _clipx2 = _logx - 1;
45 _clipy1 = 0;
46 _clipy2 = _logy - 1;
47
48 // Default to black background / white text.
49 _backgroundColor[0] = 0.0;
50 _backgroundColor[1] = 0.0;
51 _backgroundColor[2] = 0.0;
52 _backgroundColor[3] = 1.0;
53 _pixelColor[0] = 1.0;
54 _pixelColor[1] = 1.0;
55 _pixelColor[2] = 1.0;
56 _pixelColor[3] = 1.0;
57
58 _ra2d_debug = false;
59 }
60
61 void RenderArea2D::Draw(osg::State& state) {
62
63 static osg::ref_ptr<osg::StateSet> renderArea2DStateSet;
64 if(!renderArea2DStateSet.valid()) {
65 renderArea2DStateSet = new osg::StateSet;
66 renderArea2DStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
67 renderArea2DStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
68 }
69
70 state.pushStateSet(renderArea2DStateSet.get());
71 state.apply();
72 state.setActiveTextureUnit(0);
73 state.setClientActiveTextureUnit(0);
74
75 // DCL - the 2 lines below are copied verbatim from the hotspot drawing code.
76 // I am not sure if they are needed here or not.
77 glPushAttrib(GL_ENABLE_BIT);
78 glDisable(GL_COLOR_MATERIAL);
79
80 // FIXME - disabling all clip planes causes bleed-through through the splash screen.
81 glDisable(GL_CLIP_PLANE0);
82 glDisable(GL_CLIP_PLANE1);
83 glDisable(GL_CLIP_PLANE2);
84 glDisable(GL_CLIP_PLANE3);
85
86 DoDrawBackground();
87
88 for(unsigned int i = 0; i < drawing_list.size(); ++i) {
89 RA2DPrimitive prim = drawing_list[i];
90 switch(prim.type) {
91 case RA2D_LINE:
92 DoDrawLine(prim.x1, prim.y1, prim.x2, prim.y2);
93 break;
94 case RA2D_QUAD:
95 if(prim.debug) {
96 //cout << "Clipping = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
97 //cout << "Drawing quad " << prim.x1 << ", " << prim.y1 << " to " << prim.x2 << ", " << prim.y2 << '\n';
98 }
99 DoDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
100 break;
101 case RA2D_PIXEL:
102 DoDrawPixel(prim.x1, prim.y1, prim.invert);
103 break;
104 }
105 }
106
107 drawing_list.clear();
108
109 glPopAttrib();
110
111 state.popStateSet();
112 state.apply();
113 state.setActiveTextureUnit(0);
114 state.setClientActiveTextureUnit(0);
115 }
116
117 void RenderArea2D::Flush() {
118 drawing_list.clear();
119 }
120
121 void RenderArea2D::SetPixelColor(const float* rgba) {
122 _pixelColor[0] = rgba[0];
123 _pixelColor[1] = rgba[1];
124 _pixelColor[2] = rgba[2];
125 _pixelColor[3] = rgba[3];
126 }
127
128 // Set clipping region in logical units
129 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
130 _clipx1 = x1;
131 _clipx2 = x2;
132 _clipy1 = y1;
133 _clipy2 = y2;
134 //cout << "Set clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
135 }
136
137 // Set clip region to be the same as the rendered area (default)
138 void RenderArea2D::ResetClipRegion() {
139 _clipx1 = 0;
140 _clipx2 = _logx - 1;
141 _clipy1 = 0;
142 _clipy2 = _logy - 1;
143 //cout << "Reset clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
144 }
145
146 void RenderArea2D::SetPosition(int posx, int posy) {
147 _posx = posx;
148 _posy = posy;
149 }
150
151 void RenderArea2D::SetLogicalSize(int logx, int logy) {
152 _logx = logx;
153 _logy = logy;
154 }
155
156 void RenderArea2D::SetActualSize(int sizex, int sizey) {
157 _sizex = sizex;
158 _sizey = sizey;
159 }
160
161 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
162 // Clip
163 if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
164
165 RA2DPrimitive prim;
166 prim.x1 = x;
167 prim.y1 = y;
168 prim.x2 = 0;
169 prim.y2 = 0;
170 prim.type = RA2D_PIXEL;
171 prim.invert = invert;
172 drawing_list.push_back(prim);
173 }
174
175 void RenderArea2D::DoDrawPixel(int x, int y, bool invert) {
176 // Clip. In theory this shouldn't be necessary, since all input is clipped before adding
177 // to the drawing list, but it ensures that any errors in clipping lines etc will only
178 // spill over the clip area within the instrument, and still be clipped from straying
179 // outside the instrument.
180 if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
181
182 // Scale to position within background
183 float fx1 = (float)x, fy1 = (float)y;
184 float rx = (float)_sizex / (float)_logx;
185 float ry = (float)_sizey / (float)_logy;
186 fx1 *= rx;
187 fy1 *= ry;
188 float fx2 = fx1 + rx;
189 float fy2 = fy1 + ry;
190
191 // Translate to final position
192 fx1 += (float)_posx;
193 fx2 += (float)_posx;
194 fy1 += (float)_posy;
195 fy2 += (float)_posy;
196
197 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
198
199 SetRenderColor(invert ? _backgroundColor : _pixelColor);
200 SGVec2f corners[4] = {
201 SGVec2f(fx1, fy1),
202 SGVec2f(fx2, fy1),
203 SGVec2f(fx2, fy2),
204 SGVec2f(fx1, fy2)
205 };
206 RenderQuad(corners);
207 }
208
209 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
210 // We need to clip the line to the current region before storing it in the drawing
211 // list, since when we come to actually draw it the clip region may have changed.
212
213 // Liang-Barsky clipping algorithm
214 int p[4], q[4];
215 float u1 = 0.0f, u2 = 1.0f;
216 p[0] = -(x2 - x1); q[0] = (x1 - _clipx1);
217 p[1] = (x2 - x1); q[1] = (_clipx2 - x1);
218 p[2] = -(y2 - y1); q[2] = (y1 - _clipy1);
219 p[3] = (y2 - y1); q[3] = (_clipy2 - y1);
220
221 for(int i=0; i<4; ++i) {
222 if(p[i] == 0) {
223 if(q[i] < 0) {
224 // Then we have a trivial rejection of a line parallel to a clip plane
225 // completely outside the clip region.
226 return;
227 }
228 } else if(p[i] < 0) {
229 float r = (float)q[i]/(float)p[i];
230 u1 = (u1 > r ? u1 : r);
231 } else { // p[i] > 0
232 float r = (float)q[i]/(float)p[i];
233 u2 = (u2 < r ? u2 : r);
234 }
235 if(u1 > u2) {
236 // Then the line is completely outside the clip area.
237 return;
238 }
239 }
240
241 float fx1 = x1 + u1 * (float)(x2 - x1);
242 float fy1 = y1 + u1 * (float)(y2 - y1);
243 float fx2 = x1 + u2 * (float)(x2 - x1);
244 float fy2 = y1 + u2 * (float)(y2 - y1);
245 x1 = (int)(fx1 + 0.5);
246 y1 = (int)(fy1 + 0.5);
247 x2 = (int)(fx2 + 0.5);
248 y2 = (int)(fy2 + 0.5);
249
250 RA2DPrimitive prim;
251 prim.x1 = x1;
252 prim.y1 = y1;
253 prim.x2 = x2;
254 prim.y2 = y2;
255 prim.type = RA2D_LINE;
256 prim.invert = false;
257 drawing_list.push_back(prim);
258 }
259
260 void RenderArea2D::DoDrawLine(int x1, int y1, int x2, int y2) {
261 // Crude implementation of Bresenham line drawing algorithm.
262
263 // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
264 if(x2 < x1) {
265 int tmp_x = x1;
266 int tmp_y = y1;
267 x1 = x2;
268 y1 = y2;
269 x2 = tmp_x;
270 y2 = tmp_y;
271 }
272
273 bool flip_y = (y1 > y2 ? true : false);
274 int dx = x2 - x1;
275 int dy = (flip_y ? y1 - y2 : y2 - y1);
276 if(dx > dy) {
277 // push the x dir
278 int y = y1;
279 int yn = dx/2;
280 for(int x=x1; x<=x2; ++x) {
281 DoDrawPixel(x, y);
282 yn += dy;
283 if(yn >= dx) {
284 yn -= dx;
285 y = (flip_y ? y - 1 : y + 1);
286 }
287 }
288 } else {
289 // push the y dir
290 int x = x1;
291 int xn = dy/2;
292 // Must be a more elegant way to roll the next two cases into one!
293 if(flip_y) {
294 for(int y=y1; y>=y2; --y) {
295 DoDrawPixel(x, y);
296 xn += dx;
297 if(xn >= dy) {
298 xn -= dy;
299 x++;
300 }
301 }
302 } else {
303 for(int y=y1; y<=y2; ++y) {
304 DoDrawPixel(x, y);
305 xn += dx;
306 if(xn >= dy) {
307 xn -= dy;
308 x++;
309 }
310 }
311 }
312 }
313 }
314
315 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
316 // Force the input to be ordered with x1 < x2 and y1 < y2.
317 if(x1 > x2) {
318 int x = x2;
319 x2 = x1;
320 x1 = x;
321 }
322 if(y1 > y2) {
323 int y = y2;
324 y2 = y1;
325 y1 = y;
326 }
327
328 // Clip the input to the current drawing region.
329 x1 = x1 < _clipx1 ? _clipx1 : x1;
330 if(x1 > _clipx2) { return; }
331 x2 = x2 > _clipx2 ? _clipx2 : x2;
332 if(x2 < _clipx1) { return; }
333 y1 = y1 < _clipy1 ? _clipy1 : y1;
334 if(y1 > _clipy2) { return; }
335 y2 = y2 > _clipy2 ? _clipy2 : y2;
336 if(y2 < _clipy1) { return; }
337
338 RA2DPrimitive prim;
339 prim.x1 = x1;
340 prim.y1 = y1;
341 prim.x2 = x2;
342 prim.y2 = y2;
343 prim.type = RA2D_QUAD;
344 prim.invert = invert;
345 if(_ra2d_debug) prim.debug = true;
346 drawing_list.push_back(prim);
347 }
348
349 void RenderArea2D::DoDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
350 // Scale to position within background
351 float fx1 = (float)x1, fy1 = (float)y1;
352 float fx2 = (float)x2, fy2 = (float)y2;
353 float rx = (float)_sizex / (float)_logx;
354 float ry = (float)_sizey / (float)_logy;
355 fx1 *= rx;
356 fy1 *= ry;
357 fx2 *= rx;
358 fy2 *= ry;
359
360 fx2 += rx;
361 fy2 += ry;
362
363 // Translate to final position
364 fx1 += (float)_posx;
365 fx2 += (float)_posx;
366 fy1 += (float)_posy;
367 fy2 += (float)_posy;
368
369 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
370
371 SetRenderColor(invert ? _backgroundColor : _pixelColor);
372 SGVec2f corners[4] = {
373 SGVec2f(fx1, fy1),
374 SGVec2f(fx2, fy1),
375 SGVec2f(fx2, fy2),
376 SGVec2f(fx1, fy2)
377 };
378 RenderQuad(corners);
379 }
380
381 void RenderArea2D::DrawBackground() {
382 // Currently a NO-OP
383 }
384
385 void RenderArea2D::DoDrawBackground() {
386 SetRenderColor(_backgroundColor);
387 SGVec2f corners[4] = {
388 SGVec2f(_posx, _posy),
389 SGVec2f(_posx + _sizex, _posy),
390 SGVec2f(_posx + _sizex, _posy + _sizey),
391 SGVec2f(_posx, _posy + _sizey)
392 };
393
394 RenderQuad(corners);
395 }
396
397 // -----------------------------------------
398 //
399 // Actual drawing routines copied from Atlas
400 //
401 // -----------------------------------------
402
403 void RenderArea2D::SetRenderColor( const float *rgba ) {
404 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
405 glColor4fv( rgba );
406 }
407
408 void RenderArea2D::RenderQuad( const SGVec2f *p) {
409 glBegin(GL_QUADS);
410 glNormal3f(0.0f, 0.0f, 0.0f);
411 glVertex2fv( p[0].data() );
412 glVertex2fv( p[1].data() );
413 glVertex2fv( p[2].data() );
414 glVertex2fv( p[3].data() );
415 glEnd();
416 }
417
418 void RenderArea2D::RenderQuad( const SGVec2f *p, const SGVec4f *color ) {
419 glBegin(GL_QUADS);
420 glNormal3f(0.0f, 0.0f, 0.0f);
421 glColor4fv( color[0].data() ); glVertex2fv( p[0].data() );
422 glColor4fv( color[1].data() ); glVertex2fv( p[1].data() );
423 glColor4fv( color[2].data() ); glVertex2fv( p[2].data() );
424 glColor4fv( color[3].data() ); glVertex2fv( p[3].data() );
425 glEnd();
426 }