]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/render_area_2d.cxx
Merge branch 'kln89' into next
[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 - 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     _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     DoDrawBackground();
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             DoDrawLine(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             DoDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
99             break;
100         case RA2D_PIXEL:
101             DoDrawPixel(prim.x1, prim.y1, prim.invert);
102             break;
103         }
104     }
105     
106     drawing_list.clear();
107     
108     glPopAttrib();
109     
110     state.popStateSet();
111     state.apply();
112     state.setActiveTextureUnit(0);
113     state.setClientActiveTextureUnit(0);
114 }
115
116 void RenderArea2D::Flush() {
117     drawing_list.clear();
118 }
119
120 // Set clipping region in logical units
121 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
122     _clipx1 = x1;
123     _clipx2 = x2;
124     _clipy1 = y1;
125     _clipy2 = y2;
126     //cout << "Set clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
127 }
128
129 // Set clip region to be the same as the rendered area (default)
130 void RenderArea2D::ResetClipRegion() {
131     _clipx1 = 0;
132     _clipx2 = _logx - 1;
133     _clipy1 = 0;
134     _clipy2 = _logy - 1;
135     //cout << "Reset clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
136 }
137
138 void RenderArea2D::SetPosition(int posx, int posy) {
139     _posx = posx;
140     _posy = posy;
141 }
142
143 void RenderArea2D::SetLogicalSize(int logx, int logy) {
144     _logx = logx;
145     _logy = logy;
146 }
147
148 void RenderArea2D::SetActualSize(int sizex, int sizey) {
149     _sizex = sizex;
150     _sizey = sizey;
151 }
152
153 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
154     // Clip
155     if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
156
157     RA2DPrimitive prim;
158     prim.x1 = x;
159     prim.y1 = y;
160     prim.x2 = 0;
161     prim.y2 = 0;
162     prim.type = RA2D_PIXEL;
163     prim.invert = invert;
164     drawing_list.push_back(prim);
165 }
166
167 void RenderArea2D::DoDrawPixel(int x, int y, bool invert) {
168     // Clip.  In theory this shouldn't be necessary, since all input is clipped before adding
169     // to the drawing list, but it ensures that any errors in clipping lines etc will only 
170     // spill over the clip area within the instrument, and still be clipped from straying
171     // outside the instrument.
172     if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
173     
174     // Scale to position within background
175     float fx1 = (float)x, fy1 = (float)y;
176     float rx = (float)_sizex / (float)_logx;
177     float ry = (float)_sizey / (float)_logy;
178     fx1 *= rx;
179     fy1 *= ry;
180     float fx2 = fx1 + rx;
181     float fy2 = fy1 + ry;
182     
183     // Translate to final position
184     fx1 += (float)_posx;
185     fx2 += (float)_posx;
186     fy1 += (float)_posy;
187     fy2 += (float)_posy;
188     
189     //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
190     
191     SetRenderColor(invert ? _backgroundColor : _pixelColor);
192     SGVec2f corners[4] = {
193         SGVec2f(fx1, fy1),
194         SGVec2f(fx2, fy1),
195         SGVec2f(fx2, fy2),
196         SGVec2f(fx1, fy2)
197     };
198     RenderQuad(corners);
199 }
200
201 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
202     // We need to clip the line to the current region before storing it in the drawing 
203     // list, since when we come to actually draw it the clip region may have changed.
204
205     // Liang-Barsky clipping algorithm
206     int p[4], q[4];
207     float u1 = 0.0f, u2 = 1.0f;
208     p[0] = -(x2 - x1); q[0] = (x1 - _clipx1);
209     p[1] =  (x2 - x1); q[1] = (_clipx2 - x1);
210     p[2] = -(y2 - y1); q[2] = (y1 - _clipy1);
211     p[3] =  (y2 - y1); q[3] = (_clipy2 - y1);
212     
213     for(int i=0; i<4; ++i) {
214         if(p[i] == 0) {
215             if(q[i] < 0) {
216                 // Then we have a trivial rejection of a line parallel to a clip plane
217                 // completely outside the clip region.
218                 return;
219             }
220         } else if(p[i] < 0) {
221             float r = (float)q[i]/(float)p[i];
222             u1 = (u1 > r ? u1 : r);
223         } else {        // p[i] > 0
224             float r = (float)q[i]/(float)p[i];
225             u2 = (u2 < r ? u2 : r);
226         }
227         if(u1 > u2) {
228             // Then the line is completely outside the clip area.
229             return;
230         }
231     }
232     
233     float fx1 = x1 + u1 * (float)(x2 - x1);
234     float fy1 = y1 + u1 * (float)(y2 - y1);
235     float fx2 = x1 + u2 * (float)(x2 - x1);
236     float fy2 = y1 + u2 * (float)(y2 - y1);
237     x1 = (int)(fx1 + 0.5);
238     y1 = (int)(fy1 + 0.5);
239     x2 = (int)(fx2 + 0.5);
240     y2 = (int)(fy2 + 0.5);
241     
242     RA2DPrimitive prim;
243     prim.x1 = x1;
244     prim.y1 = y1;
245     prim.x2 = x2;
246     prim.y2 = y2;
247     prim.type = RA2D_LINE;
248     prim.invert = false;
249     drawing_list.push_back(prim);
250 }
251
252 void RenderArea2D::DoDrawLine(int x1, int y1, int x2, int y2) {
253     // Crude implementation of Bresenham line drawing algorithm.
254     
255     // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
256     if(x2 < x1) {
257         int tmp_x = x1;
258         int tmp_y = y1;
259         x1 = x2;
260         y1 = y2;
261         x2 = tmp_x;
262         y2 = tmp_y;
263     }
264     
265     bool flip_y = (y1 > y2 ? true : false);
266     int dx = x2 - x1;
267     int dy = (flip_y ? y1 - y2 : y2 - y1); 
268     if(dx > dy) {
269         // push the x dir
270         int y = y1;
271         int yn = dx/2;
272         for(int x=x1; x<=x2; ++x) {
273             DoDrawPixel(x, y);
274             yn += dy;
275             if(yn >= dx) {
276                 yn -= dx;
277                 y = (flip_y ? y - 1 : y + 1);
278             }
279         }
280     } else {
281         // push the y dir
282         int x = x1;
283         int xn = dy/2;
284         // Must be a more elegant way to roll the next two cases into one!
285         if(flip_y) {
286             for(int y=y1; y>=y2; --y) {
287                 DoDrawPixel(x, y);
288                 xn += dx;
289                 if(xn >= dy) {
290                     xn -= dy;
291                     x++;
292                 }
293             }
294         } else {
295             for(int y=y1; y<=y2; ++y) {
296                 DoDrawPixel(x, y);
297                 xn += dx;
298                 if(xn >= dy) {
299                     xn -= dy;
300                     x++;
301                 }
302             }
303         }
304     }
305 }
306
307 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
308     // Force the input to be ordered with x1 < x2 and y1 < y2.
309     if(x1 > x2) {
310         int x = x2;
311         x2 = x1;
312         x1 = x;
313     }
314     if(y1 > y2) {
315         int y = y2;
316         y2 = y1;
317         y1 = y;
318     }
319     
320     // Clip the input to the current drawing region.
321     x1 = x1 < _clipx1 ? _clipx1 : x1;
322     if(x1 > _clipx2) { return; }
323     x2 = x2 > _clipx2 ? _clipx2 : x2;
324     if(x2 < _clipx1) { return; }
325     y1 = y1 < _clipy1 ? _clipy1 : y1;
326     if(y1 > _clipy2) { return; }
327     y2 = y2 > _clipy2 ? _clipy2 : y2;
328     if(y2 < _clipy1) { return; }
329     
330     RA2DPrimitive prim;
331     prim.x1 = x1;
332     prim.y1 = y1;
333     prim.x2 = x2;
334     prim.y2 = y2;
335     prim.type = RA2D_QUAD;
336     prim.invert = invert;
337     if(_ra2d_debug) prim.debug = true;
338     drawing_list.push_back(prim);
339 }
340
341 void RenderArea2D::DoDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
342     // Scale to position within background
343     float fx1 = (float)x1, fy1 = (float)y1;
344     float fx2 = (float)x2, fy2 = (float)y2;
345     float rx = (float)_sizex / (float)_logx;
346     float ry = (float)_sizey / (float)_logy;
347     fx1 *= rx;
348     fy1 *= ry;
349     fx2 *= rx;
350     fy2 *= ry;
351     
352     fx2 += rx;
353     fy2 += ry;
354     
355     // Translate to final position
356     fx1 += (float)_posx;
357     fx2 += (float)_posx;
358     fy1 += (float)_posy;
359     fy2 += (float)_posy;
360     
361     //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
362     
363     SetRenderColor(invert ? _backgroundColor : _pixelColor);
364     SGVec2f corners[4] = {
365         SGVec2f(fx1, fy1),
366         SGVec2f(fx2, fy1),
367         SGVec2f(fx2, fy2),
368         SGVec2f(fx1, fy2)
369     };
370     RenderQuad(corners);
371 }
372
373 void RenderArea2D::DrawBackground() {
374     // Currently a NO-OP
375 }
376
377 void RenderArea2D::DoDrawBackground() {
378     SetRenderColor(_backgroundColor);
379     SGVec2f corners[4] = {
380         SGVec2f(_posx, _posy),
381         SGVec2f(_posx + _sizex, _posy),
382         SGVec2f(_posx + _sizex, _posy + _sizey),
383         SGVec2f(_posx, _posy + _sizey)                                                                  
384     };
385   
386     RenderQuad(corners);
387 }
388
389 // -----------------------------------------
390 //
391 // Actual drawing routines copied from Atlas
392 //
393 // -----------------------------------------
394
395 void RenderArea2D::SetRenderColor( const float *rgba ) {
396     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
397     glColor4fv( rgba );
398 }
399
400 void RenderArea2D::RenderQuad( const SGVec2f *p) {
401     glBegin(GL_QUADS);
402         glNormal3f(0.0f, 0.0f, 0.0f);
403         glVertex2fv( p[0].data() );
404         glVertex2fv( p[1].data() );
405         glVertex2fv( p[2].data() );
406         glVertex2fv( p[3].data() );
407     glEnd();
408 }
409
410 void RenderArea2D::RenderQuad( const SGVec2f *p, const SGVec4f *color ) {
411     glBegin(GL_QUADS);
412         glNormal3f(0.0f, 0.0f, 0.0f);
413         glColor4fv( color[0].data() ); glVertex2fv( p[0].data() );
414         glColor4fv( color[1].data() ); glVertex2fv( p[1].data() );
415         glColor4fv( color[2].data() ); glVertex2fv( p[2].data() );
416         glColor4fv( color[3].data() ); glVertex2fv( p[3].data() );
417     glEnd();
418 }