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