]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/render_area_2d.cxx
GPS data validity clean-up; it was a mess, now it's more robust. Thanks to Dave Luff...
[flightgear.git] / src / Instrumentation / render_area_2d.cxx
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 - david.luff@nottingham.ac.uk
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 static const float dummy_normals[][3] = {{0.0f, 0.0f, 0.0f},
32                                          {0.0f, 0.0f, 0.0f},
33                                          {0.0f, 0.0f, 0.0f},
34                                          {0.0f, 0.0f, 0.0f}};
35
36 RA2DPrimitive::RA2DPrimitive() {
37         invert = false;
38         debug = false;
39 }
40                                                                                  
41 RenderArea2D::RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy) {
42         _logx = logx;
43         _logy = logy;
44         _sizex = sizex;
45         _sizey = sizey;
46         _posx = posx;
47         _posy = posy;
48         _clipx1 = 0;
49         _clipx2 = _logx - 1;
50         _clipy1 = 0;
51         _clipy2 = _logy - 1;
52         
53         _backgroundColor[0] = 0.0;
54         _backgroundColor[1] = 0.0;
55         _backgroundColor[2] = 0.0;
56         _backgroundColor[3] = 1.0;
57         _pixelColor[0] = 1.0;
58         _pixelColor[1] = 0.0;
59         _pixelColor[2] = 0.0;
60         _pixelColor[3] = 1.0;
61         
62         _ra2d_debug = false;
63 }
64
65 void RenderArea2D::draw(osg::State& state) {
66         
67         static osg::ref_ptr<osg::StateSet> renderArea2DStateSet;
68         if(!renderArea2DStateSet.valid()) {
69                 renderArea2DStateSet = new osg::StateSet;
70                 renderArea2DStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
71                 renderArea2DStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
72         }
73         
74         state.pushStateSet(renderArea2DStateSet.get());
75         state.apply();
76         state.setActiveTextureUnit(0);
77         state.setClientActiveTextureUnit(0);
78         
79         // DCL - the 2 lines below are copied verbatim from the hotspot drawing code.
80         // I am not sure if they are needed here or not.
81         glPushAttrib(GL_ENABLE_BIT);
82         glDisable(GL_COLOR_MATERIAL);
83         
84         // FIXME - disabling all clip planes causes bleed-through through the splash screen.
85         glDisable(GL_CLIP_PLANE0);
86         glDisable(GL_CLIP_PLANE1);
87         glDisable(GL_CLIP_PLANE2);
88         glDisable(GL_CLIP_PLANE3);
89
90         oldDrawBackground();
91         
92         for(unsigned int i = 0; i < drawing_list.size(); ++i) {
93                 RA2DPrimitive prim = drawing_list[i];
94                 switch(prim.type) {
95                 case RA2D_LINE:
96                         oldDrawLine(prim.x1, prim.y1, prim.x2, prim.y2);
97                         break;
98                 case RA2D_QUAD:
99                         if(prim.debug) {
100                                 //cout << "Clipping = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
101                                 //cout << "Drawing quad " << prim.x1 << ", " << prim.y1 << " to " << prim.x2 << ", " << prim.y2 << '\n';
102                         }
103                         oldDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
104                         break;
105                 case RA2D_PIXEL:
106                         oldDrawPixel(prim.x1, prim.y1, prim.invert);
107                         break;
108                 }
109         }
110         
111         glPopAttrib();
112         
113         state.popStateSet();
114         state.apply();
115         state.setActiveTextureUnit(0);
116         state.setClientActiveTextureUnit(0);
117 }
118
119 // Set clipping region in logical units
120 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
121         _clipx1 = x1;
122         _clipx2 = x2;
123         _clipy1 = y1;
124         _clipy2 = y2;
125         //cout << "Set clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
126 }
127
128 // Set clip region to be the same as the rendered area (default)
129 void RenderArea2D::ResetClipRegion() {
130         _clipx1 = 0;
131         _clipx2 = _logx - 1;
132         _clipy1 = 0;
133         _clipy2 = _logy - 1;
134         //cout << "Reset clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
135 }
136
137 void RenderArea2D::SetPosition(int posx, int posy) {
138         _posx = posx;
139         _posy = posy;
140 }
141
142 void RenderArea2D::SetLogicalSize(int logx, int logy) {
143         _logx = logx;
144         _logy = logy;
145 }
146
147 void RenderArea2D::SetActualSize(int sizex, int sizey) {
148         _sizex = sizex;
149         _sizey = sizey;
150 }
151
152 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
153         // Clipping is currently performed in oldDrawPixel - could clip here instead though.
154
155         RA2DPrimitive prim;
156         prim.x1 = x;
157         prim.y1 = y;
158         prim.x2 = 0;
159         prim.y2 = 0;
160         prim.type = RA2D_PIXEL;
161         prim.invert = invert;
162         drawing_list.push_back(prim);
163 }
164
165 void RenderArea2D::oldDrawPixel(int x, int y, bool invert) {
166         // Clip
167         if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
168         
169         // Scale to position within background
170         float fx1 = (float)x, fy1 = (float)y;
171         float rx = (float)_sizex / (float)_logx;
172         float ry = (float)_sizey / (float)_logy;
173         fx1 *= rx;
174         fy1 *= ry;
175         float fx2 = fx1 + rx;
176         float fy2 = fy1 + ry;
177         
178         // Translate to final position
179         fx1 += (float)_posx;
180         fx2 += (float)_posx;
181         fy1 += (float)_posy;
182         fy2 += (float)_posy;
183         
184         //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
185         
186         doSetColor(invert ? _backgroundColor : _pixelColor);
187         sgVec2 corners[4];
188         sgSetVec2(corners[0], fx1, fy1);
189         sgSetVec2(corners[1], fx2, fy1);
190         sgSetVec2(corners[2], fx2, fy2);
191         sgSetVec2(corners[3], fx1, fy2);
192         //cout << "Drawing pixel, x,y is " << x << ", " << y << ", fx is [x1,x2,y1,y2] " << fx1 << ", " << fx2 << ", " << fy1 << ", " << fy2 << '\n';
193         doDrawQuad(&corners[0], dummy_normals);
194 }
195
196 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
197         RA2DPrimitive prim;
198         prim.x1 = x1;
199         prim.y1 = y1;
200         prim.x2 = x2;
201         prim.y2 = y2;
202         prim.type = RA2D_LINE;
203         prim.invert = false;
204         drawing_list.push_back(prim);
205 }
206
207 void RenderArea2D::oldDrawLine(int x1, int y1, int x2, int y2) {
208         // Crude implementation of Bresenham line drawing algorithm.
209         
210         // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
211         if(x2 < x1) {
212                 int tmp_x = x1;
213                 int tmp_y = y1;
214                 x1 = x2;
215                 y1 = y2;
216                 x2 = tmp_x;
217                 y2 = tmp_y;
218         }
219         
220         bool flip_y = (y1 > y2 ? true : false);
221         int dx = x2 - x1;
222         int dy = (flip_y ? y1 - y2 : y2 - y1); 
223         if(dx > dy) {
224                 // push the x dir
225                 int y = y1;
226                 int yn = dx/2;
227                 for(int x=x1; x<=x2; ++x) {
228                         DrawPixel(x, y);
229                         yn += dy;
230                         if(yn >= dx) {
231                                 yn -= dx;
232                                 y = (flip_y ? y - 1 : y + 1);
233                         }
234                 }
235         } else {
236                 // push the y dir
237                 int x = x1;
238                 int xn = dy/2;
239                 // Must be a more elegant way to roll the next two cases into one!
240                 if(flip_y) {
241                         for(int y=y1; y>=y2; --y) {
242                                 DrawPixel(x, y);
243                                 xn += dx;
244                                 if(xn >= dy) {
245                                         xn -= dy;
246                                         x++;
247                                 }
248                         }
249                 } else {
250                         for(int y=y1; y<=y2; ++y) {
251                                 DrawPixel(x, y);
252                                 xn += dx;
253                                 if(xn >= dy) {
254                                         xn -= dy;
255                                         x++;
256                                 }
257                         }
258                 }
259         }
260 }
261
262 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
263         // Clip and sanity-check.
264         if(x1 > x2) {
265                 int x = x2;
266                 x2 = x1;
267                 x1 = x;
268         }
269         if(y1 > y2) {
270                 int y = y2;
271                 y2 = y1;
272                 y1 = y;
273         }
274         x1 = x1 < _clipx1 ? _clipx1 : x1;
275         if(x1 > _clipx2) { return; }
276         x2 = x2 > _clipx2 ? _clipx2 : x2;
277         if(x2 < _clipx1) { return; }
278         y1 = y1 < _clipy1 ? _clipy1 : y1;
279         if(y1 > _clipy2) { return; }
280         y2 = y2 > _clipy2 ? _clipy2 : y2;
281         if(y2 < _clipy1) { return; }
282         
283         RA2DPrimitive prim;
284         prim.x1 = x1;
285         prim.y1 = y1;
286         prim.x2 = x2;
287         prim.y2 = y2;
288         prim.type = RA2D_QUAD;
289         prim.invert = invert;
290         if(_ra2d_debug) prim.debug = true;
291         drawing_list.push_back(prim);
292 }
293
294 void RenderArea2D::oldDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
295         // Scale to position within background
296         float fx1 = (float)x1, fy1 = (float)y1;
297         float fx2 = (float)x2, fy2 = (float)y2;
298         float rx = (float)_sizex / (float)_logx;
299         float ry = (float)_sizey / (float)_logy;
300         fx1 *= rx;
301         fy1 *= ry;
302         fx2 *= rx;
303         fy2 *= ry;
304         
305         fx2 += rx;
306         fy2 += ry;
307         
308         // Translate to final position
309         fx1 += (float)_posx;
310         fx2 += (float)_posx;
311         fy1 += (float)_posy;
312         fy2 += (float)_posy;
313         
314         //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
315         
316         doSetColor(invert ? _backgroundColor : _pixelColor);
317         sgVec2 corners[4];
318         sgSetVec2(corners[0], fx1, fy1);
319         sgSetVec2(corners[1], fx2, fy1);
320         sgSetVec2(corners[2], fx2, fy2);
321         sgSetVec2(corners[3], fx1, fy2);
322         doDrawQuad(&corners[0], dummy_normals);
323 }
324
325 void RenderArea2D::DrawBackground() {
326         // TODO
327 }
328
329 void RenderArea2D::oldDrawBackground() {
330         doSetColor(_backgroundColor);
331         sgVec2 corners[4];
332         sgSetVec2(corners[0], (float)_posx, (float)_posy);
333         sgSetVec2(corners[1], (float)(_posx + _sizex), (float)_posy);
334         sgSetVec2(corners[2], (float)(_posx + _sizex), (float)(_posy + _sizey));
335         sgSetVec2(corners[3], (float)_posx, (float)(_posy + _sizey));
336         doDrawQuad(&corners[0], dummy_normals);
337 }
338
339 void RenderArea2D::Flush() {
340         drawing_list.clear();
341 }
342
343 // -----------------------------------------
344 //
345 // Actual drawing routines copied from Atlas
346 //
347 // -----------------------------------------
348
349 void RenderArea2D::doSetColor( const float *rgba ) {
350   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
351   glColor4fv( rgba );
352 }
353
354 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals ) {
355   glBegin(GL_QUADS);
356   glNormal3fv( normals[0] ); glVertex2fv( p[0] );
357   glNormal3fv( normals[1] ); glVertex2fv( p[1] );
358   glNormal3fv( normals[2] ); glVertex2fv( p[2] );
359   glNormal3fv( normals[3] ); glVertex2fv( p[3] );
360   glEnd();
361 }
362
363 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals, const sgVec4 *color ) {
364   glBegin(GL_QUADS);
365     glColor4fv( color[0] );glNormal3fv( normals[0] ); glVertex2fv( p[0] );
366     glColor4fv( color[1] );glNormal3fv( normals[1] ); glVertex2fv( p[1] );
367     glColor4fv( color[2] );glNormal3fv( normals[2] ); glVertex2fv( p[2] );
368     glColor4fv( color[3] );glNormal3fv( normals[3] ); glVertex2fv( p[3] );
369   glEnd();
370 }